/* 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 #include #include #include #else #include #include #include #include #include #include #endif #include #include #include #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); } }