Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 39700b9a8e772dd8c0d3219575a1d9948a665d8e (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
   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
}

/*

POSIX:

	struct tm {
		int tm_sec;         // seconds
		int tm_min;         // minutes
		int tm_hour;        // hours
		int tm_mday;        // day of the month
		int tm_mon;         // month
		int tm_year;        // year
		int tm_wday;        // day of the week
		int tm_yday;        // day in the year
		int tm_isdst;       // daylight saving time
	};

WIN32:

	typedef struct _SYSTEMTIME {
		WORD wYear;
		WORD wMonth;
		WORD wDayOfWeek;
		WORD wDay;
		WORD wHour;
		WORD wMinute;
		WORD wSecond;
		WORD wMilliseconds;
	} SYSTEMTIME, *PSYSTEMTIME;

*/

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
	SYSTEMTIME localtime;
	::GetLocalTime(&localtime);

	return ((unsigned long)(localtime.wSecond + localtime.wMinute*60 + localtime.wHour*3600));
#endif
}

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

void get_datetime(int &year, int & month, int & day, int & hours, int & minutes, int &seconds)
{
#ifndef _WIN32
	struct ::tm localtime;
	time_t epochtime = ::time(0);
	::localtime_r(&epochtime, &localtime);

	year = localtime.tm_year + 1900;
	month = localtime.tm_mon + 1;
	day = localtime.tm_mday;

	hours = localtime.tm_hour;
	minutes = localtime.tm_min;
	seconds = localtime.tm_sec;
#else
	SYSTEMTIME localtime;
	::GetLocalTime(&localtime);

	year = localtime.wYear;
	month = localtime.wMonth;
	day = localtime.wDay;

	hours = localtime.wHour;
	minutes = localtime.wMinute;
	seconds = localtime.wSecond;
#endif
}

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

}