/* PIP - Platform Independent Primitives Global includes Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef PIINCLUDES_H #define PIINCLUDES_H #define PIP_VERSION 0x000300 #define PIP_VERSION_MAJOR (PIP_VERSION & 0xFF0000) >> 16 #define PIP_VERSION_MINOR (PIP_VERSION & 0xFF00) >> 8 #define PIP_VERSION_REVISION PIP_VERSION & 0xFF #define PIP_VERSION_SUFFIX "" #if WIN32 || WIN64 || _WIN32 || _WIN64 || __WIN32__ || __WIN64__ # define WINDOWS #endif #if __QNX__ || __QNXNTO__ # define QNX #endif #if __FreeBSD__ # define FREE_BSD #endif #if __APPLE__ || __MACH__ # define MAC_OS #endif #ifndef WINDOWS # ifndef QNX # ifndef FREE_BSD # ifndef MAC_OS # define LINUX # endif # endif # endif #endif #if __GNUC__ # define CC_GCC # define CC_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__) # if CC_GCC_VERSION > 0x025F // > 2.95 # ifdef LINUX # define HAS_LOCALE # endif # endif #elif _MSC_VER # define CC_VC #else # define CC_OTHER #endif #ifdef WINDOWS # ifdef CC_GCC # define typeof __typeof # endif #else # define typeof __typeof__ #endif #include #ifdef CC_GCC # include #endif #include #include #ifndef QNX # include # include # include #else # include # include #endif #include #include #include #include #include #include #include //#include #include #include #include #include #include #include #include #include #include #include #include #ifdef WINDOWS typedef int socklen_t; # include # include # include # ifdef CC_VC # define SHUT_RDWR 2 # pragma comment(lib, "Ws2_32.lib") # pragma comment(lib, "Iphlpapi.lib") # else # define SHUT_RDWR SD_BOTH # endif # include # include # include #else # include # include # include # include # include # include #endif #ifdef MAC_OS # include # include # include # include # define environ (*_NSGetEnviron()) typedef long time_t; extern clock_serv_t __pi_mac_clock; #endif #ifndef QNX # ifndef WINDOWS # ifndef FREE_BSD # ifndef MAC_OS # define environ __environ # endif # endif # endif #endif #if !defined(WINDOWS) && !defined(MAC_OS) # define PIP_TIMER_RT #endif #ifdef FREE_BSD extern char ** environ; #endif #include "pimonitor.h" extern PIMonitor piMonitor; #define FOREVER for (;;) #define FOREVER_WAIT FOREVER msleep(1); typedef long long llong; typedef unsigned char uchar; typedef unsigned short int ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned long long ullong; typedef long double ldouble; using std::cout; using std::cin; using std::endl; using std::flush; using std::vector; using std::list; using std::queue; using std::deque; using std::stack; using std::set; using std::map; using std::string; #ifndef QNX using std::wstring; //# ifndef WINDOWS //static locale_t currentLocale_t = 0; //# endif #else typedef std::basic_string wstring; #endif #ifdef HAS_LOCALE static locale_t currentLocale_t = 0; #endif template inline void piSwap(T & f, T & s) {T t = f; f = s; s = t;} template inline int piRound(const T & v) {return int(v >= T(0.) ? v + T(0.5) : v - T(0.5));} template inline int piFloor(const T & v) {return v < T(0) ? int(v) - 1 : int(v);} template inline int piCeil(const T & v) {return v < T(0) ? int(v) : int(v) + 1;} template inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);} template inline T piMin(const T & f, const T & s) {return (f > s) ? s : f;} template inline T piMin(const T & f, const T & s, const T & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);} template inline T piMax(const T & f, const T & s) {return (f < s) ? s : f;} template inline T piMax(const T & f, const T & s, const T & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);} template inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));} #define piRoundf piRound #define piRoundd piRound #define piFloorf piFloor #define piFloord piFloor #define piCeilf piCeil #define piCeild piCeil #define piAbss piAbs #define piAbsi piAbs #define piAbsl piAbs #define piAbsll piAbs #define piAbsf piAbs #define piAbsd piAbs #define piMins piMin #define piMini piMin #define piMinl piMin #define piMinll piMin #define piMinf piMin #define piMind piMin #define piMaxs piMax #define piMaxi piMax #define piMaxl piMax #define piMaxll piMax #define piMaxf piMax #define piMaxd piMax #define piClamps piClamp #define piClampi piClamp #define piClampl piClamp #define piClampll piClamp #define piClampf piClamp #define piClampd piClamp extern bool isPIInit; extern bool piDebug; extern string ifconfigPath; #define piCout if (piDebug) cout class PIInit { public: PIInit() { if (isPIInit) return; isPIInit = true; #ifndef WINDOWS sigset_t ss; sigemptyset(&ss); sigaddset(&ss, SIGALRM); if (pthread_sigmask(SIG_BLOCK, &ss, 0) == -1) { //cout << "[PITimer] sigaction error: " << errorString() << endl; //return 0; } ifconfigPath = "/bin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/sbin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/usr/bin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/usr/sbin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = ""; } } } } #else WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif //piDebug = true; #ifdef HAS_LOCALE //cout << "has locale" << endl; if (currentLocale_t != 0) { freelocale(currentLocale_t); currentLocale_t = 0; } currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, ""), 0); #else setlocale(LC_ALL, ""); #endif #ifdef MAC_OS host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock); #endif } ~PIInit() { #ifdef WINDOWS WSACleanup(); #endif #ifdef MAC_OS mach_port_deallocate(mach_task_self(), __pi_mac_clock); #endif //if (currentLocale_t != 0) freelocale(currentLocale_t); } private: bool fileExists(const string & p) {FILE * f = fopen(p.c_str(), "r"); if (f == 0) return false; fclose(f); return true;} }; extern PIInit piInit; extern lconv * currentLocale; #ifdef WINDOWS inline int random() {return rand();} # ifdef CC_VC inline double round(const double & v) {return floor(v + 0.5);} # endif #endif inline ushort letobe_s(ushort v) {return v = (v << 8) | (v >> 8);} inline bool atob(const string & str) { return str == "1" ? true : false;}; inline string btos(const bool num) { return num ? "0" : "1";}; inline string itos(const int num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%d", num); #else sprintf_s(ch, 256, "%d", num); #endif return string(ch); }; inline string ltos(const long num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%ld", num); #else sprintf_s(ch, 256, "%ld", num); #endif return string(ch); }; inline string uitos(const uint num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%ud", num); #else sprintf_s(ch, 256, "%ud", num); #endif return string(ch); }; inline string ultos(const ulong num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%lud", num); #else sprintf_s(ch, 256, "%lud", num); #endif return string(ch); }; inline string ftos(const float num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%g", num); #else sprintf_s(ch, 256, "%g", num); #endif return string(ch); }; inline string dtos(const double num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%g", num); #else sprintf_s(ch, 256, "%g", num); #endif return string(ch); }; #ifdef CC_VC inline string errorString() { char * msg; int err = GetLastError(); FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL); return "code " + itos(err) + " - " + string(msg); } #else inline void errorClear() {errno = 0;} inline string errorString() {int e = errno; return "code " + itos(e) + " - " + string(strerror(e));} #endif inline string PIPVersion() {return itos(PIP_VERSION_MAJOR) + "." + itos(PIP_VERSION_MINOR) + "." + itos(PIP_VERSION_REVISION) + PIP_VERSION_SUFFIX;} #endif // PIINCLUDES_H