/* PIP - Platform Independent Primitives Global includes Copyright (C) 2011 Ivan Pelipenko peri4@rambler.ru 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 0x000101 #define PIP_VERSION_MAJOR (PIP_VERSION & 0xFF0000) >> 16 #define PIP_VERSION_MINOR (PIP_VERSION & 0xFF00) >> 8 #define PIP_VERSION_REVISION PIP_VERSION & 0xFF #if WIN32 || WIN64 || _WIN32 || _WIN64 || __WIN32__ || __WIN64__ # define WINDOWS #endif #if __QNX__ || __QNXNTO__ # define QNX #endif #ifndef WINDOWS # ifndef QNX # define LINUX # endif #endif #if __GNUC__ # define CC_GCC #elif _MSC_VER # define CC_VC #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 #ifdef WINDOWS # include # include # include #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::string; #ifndef QNX using std::wstring; # ifndef WINDOWS static locale_t currentLocale_t = 0; # endif #else typedef std::basic_string wstring; #endif static bool isPIInit = false; class PIInit { public: PIInit() { if (isPIInit) return; isPIInit = true; #ifdef LINUX if (currentLocale_t != 0) { freelocale(currentLocale_t); currentLocale_t = 0; } currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, ""), 0); #else setlocale(LC_ALL, ""); #endif } ~PIInit() { //if (currentLocale_t != 0) freelocale(currentLocale_t); } }; static PIInit piInit; static lconv * currentLocale = std::localeconv(); #ifdef CC_VC inline string errorString() {char buff[1024]; strerror_s(buff, 1024, GetLastError()); return string(buff);} #else inline string errorString() {return string(strerror(errno));} #endif #ifdef WINDOWS inline int random() {return rand();} # ifdef CC_VC inline double round(const double & v) {return floor(v + 0.5);} # endif #endif template inline void piSwap(Type & f, Type & s) {Type t = f; f = s; s = t;} template inline Type piMin(const Type & f, const Type & s) {return (f > s) ? s : f;} template inline Type piMin(const Type & f, const Type & s, const Type & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);} template inline Type piMax(const Type & f, const Type & s) {return (f < s) ? s : f;} template inline Type piMax(const Type & f, const Type & s, const Type & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);} template inline Type piClamp(const Type & v, const Type & min, const Type & max) {return (v > max ? max : (v < min ? min : v));} 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); }; inline string PIPVersion() {return itos(PIP_VERSION_MAJOR) + "." + itos(PIP_VERSION_MINOR) + "." + itos(PIP_VERSION_REVISION);} #endif // PIINCLUDES_H