Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 01c9e3b8b6cca02d8ccafc63b69f129e2c6a6166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
   sys/sys.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/


#ifdef _WIN32

#include <windows.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>

#else

#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>

#endif

#include <stdio.h>
#include <stdlib.h>
#include <string>

#include "sys/sys.h"

namespace sys
{

bool file_exists(const std::string &filename)
{
#ifdef _WIN32
	struct ::_stat path_stat;
	memset(&path_stat, 0, sizeof(struct ::_stat));
	
	if (::_stat(filename.c_str(), &path_stat) != 0) {
		return false;
	}
	
	if (path_stat.st_mode & _S_IFDIR) {
		return false;
	}
	return true;
#else
	struct stat path_stat;
	memset(&path_stat, 0, sizeof(path_stat));
	
	if (stat(filename.c_str(), &path_stat) != 0) {
		return false;
	}
	
	if (path_stat.st_mode & S_IFDIR) {
		return false;
	}
	
	return true;
#endif
}


bool directory_exists(const std::string &path)
{
#ifdef _WIN32
	struct ::_stat path_stat;
	memset(&path_stat, 0, sizeof(struct ::_stat));
	
	if (::_stat(path.c_str(), &path_stat) != 0) {
		return false;
	}
	
	if (path_stat.st_mode & _S_IFDIR) {
		return true;
	}
	return false;
#else
	struct stat path_stat;
	memset(&path_stat, 0, sizeof(path_stat));
	
	if (stat(path.c_str(), &path_stat) != 0) {
		return false;
	}
	
	if (path_stat.st_mode & S_IFDIR) {
		return true;
	}
	
	return false;
#endif
}

void mkdir(const std::string &path)
{
#ifdef _WIN32
	std::string p(path);
	for (size_t i = 0; i < p.size(); i++)
		if (p[i] == '/') p[i] = '\\';
	if (p.size() && (p[p.size()-1] == '\\'))
		p.erase(p.size() -1, 1);
		
	if (_mkdir(p.c_str()) != 0) {
		con_warn << "Could not create directory '" << p << "'" << std::endl;
	}
#else
	
	::mkdir(path.c_str(), 0777);
#endif
}

void signal(int signum, signalfunc handler)
{
#ifndef _WIN32
	struct sigaction sa;
	
	sa.sa_sigaction = 0;
	memset(&sa.sa_mask, 0 ,sizeof(sigset_t));
	sa.sa_flags = 0;
	sa.sa_handler = handler;
	
	::sigaction(signum, &sa, 0);
#endif
}

unsigned long time()
{
#ifndef _WIN32
	struct ::tm localtime;
	time_t epochtime = ::time(0);
	::localtime_r(&epochtime, &localtime);
	return ((unsigned long)(localtime.tm_sec + localtime.tm_min*60 + localtime.tm_hour*3600));
#else
	return 0;
#endif
}

void sleep(float seconds)
{
#ifndef _WIN32
	::usleep((useconds_t)(seconds * 1000000.0f));
#else
	Sleep((DWORD)(seconds*1000.0f));
#endif
}

void quit(int status)
{
	::exit(status);
}

}