Changeset 247 for cpp/common
- Timestamp:
- 11/07/14 17:51:01 (10 years ago)
- Location:
- cpp/common
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/Convert.cpp
r197 r247 8 8 9 9 #if defined __ANDROID__ || defined __BORLANDC__ 10 10 #include <ctype.h> //toupper, tolower 11 11 #endif 12 12 13 13 #ifdef SHP 14 14 #include <cstdlib> 15 15 #else 16 16 #include <stdlib.h> 17 17 #endif 18 18 … … 20 20 21 21 22 int Convert::toInt(string s) {return atoi(s.c_str());} 23 float Convert::toFloat(string s) {return (float)atof(s.c_str());} 24 #ifndef __BORLANDC__ //the borland compiler compiles this file in GUI and CLI, but not in the theater app. Somehow it cannot find ::tolower and ::toupper; maybe this is because of various #defines in <cctype>, but why only in the theater app?? 25 string Convert::toLowerCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s;} 26 string Convert::toUpperCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s;} 27 #endif 28 char Convert::toLowerCase(char c) {return (char)tolower(c);} 29 char Convert::toUpperCase(char c) {return (char)toupper(c);} 22 int Convert::toInt(string s) { return atoi(s.c_str()); } 23 float Convert::toFloat(string s) { return (float)atof(s.c_str()); } 24 string Convert::toLowerCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } 25 string Convert::toUpperCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s; } 26 char Convert::toLowerCase(char c) { return (char)tolower(c); } 27 char Convert::toUpperCase(char c) { return (char)toupper(c); } 30 28 31 template<class T> const char* printf_format_for(const T& value) { return "unknown type";}32 template<> const char* printf_format_for(const unsigned int& value) { return "%u";}33 template<> const char* printf_format_for(const int& value) { return "%d";}34 template<> const char* printf_format_for(const short& value) { return "%d";}35 template<> const char* printf_format_for(const float& value) { return "%g";}36 template<> const char* printf_format_for(const double& value) { return "%g";}29 template<class T> const char* printf_format_for(const T& value) { return "unknown type"; } 30 template<> const char* printf_format_for(const unsigned int& value) { return "%u"; } 31 template<> const char* printf_format_for(const int& value) { return "%d"; } 32 template<> const char* printf_format_for(const short& value) { return "%d"; } 33 template<> const char* printf_format_for(const float& value) { return "%g"; } 34 template<> const char* printf_format_for(const double& value) { return "%g"; } 37 35 38 36 template<class T> string Convert::_toString(const T& value) 39 37 { 40 char buf[30];41 sprintf(buf,printf_format_for(value),value);42 return string(buf);43 /*44 #ifndef MULTITHREADED38 char buf[30]; 39 sprintf(buf, printf_format_for(value), value); 40 return string(buf); 41 /* 42 #ifndef MULTITHREADED 45 43 static 46 #endif44 #endif 47 45 std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy) 48 46 //dlatego robimy go raz (static) i potem tylko czyscimy … … 51 49 oss.clear(); //clear error flag 52 50 oss.str(""); //set empty string 53 54 55 */51 oss << value; 52 return oss.str(); 53 */ 56 54 } 57 55 58 string Convert::toString(unsigned int v) {return _toString(v);} 59 string Convert::toString(int v) {return _toString(v);} 60 string Convert::toString(short v) {return _toString(v);} 61 string Convert::toString(float v) {return _toString(v);} 62 string Convert::toString(double v) {return _toString(v);} 56 string Convert::toString(unsigned int v) { return _toString(v); } 57 string Convert::toString(int v) { return _toString(v); } 58 string Convert::toString(short v) { return _toString(v); } 59 string Convert::toString(float v) { return _toString(v); } 60 string Convert::toString(double v) { return _toString(v); } 61 62 uint32_t Convert::hexToInt(const string& col) 63 { 64 uint32_t value; 65 std::istringstream iss(col); 66 iss >> std::hex >> value; 67 return value; 68 } 63 69 64 70 #ifdef MULTITHREADED … … 69 75 //z mutexa gdy drugi dalej by go inicjalizowal 70 76 #include "threads.h" 71 static pthread_mutex_t fix_unsafe_mutex =PTHREAD_MUTEX_INITIALIZER;77 static pthread_mutex_t fix_unsafe_mutex = PTHREAD_MUTEX_INITIALIZER; 72 78 #endif 73 79 … … 76 82 #ifndef MULTITHREADED 77 83 78 return *::localtime(&timep);84 return *::localtime(&timep); 79 85 80 86 #else 81 87 82 struct tm ret;88 struct tm ret; 83 89 84 90 #if defined LINUX // || android? 85 return *::localtime_r(&timep,&ret);91 return *::localtime_r(&timep,&ret); 86 92 #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? 87 ::localtime_s(&ret,&timep);88 return ret;93 ::localtime_s(&ret, &timep); 94 return ret; 89 95 #else //borland? 90 pthread_mutex_lock(&fix_unsafe_mutex);91 ret=*::localtime(&timep);92 pthread_mutex_unlock(&fix_unsafe_mutex);93 return ret;96 pthread_mutex_lock(&fix_unsafe_mutex); 97 ret=*::localtime(&timep); 98 pthread_mutex_unlock(&fix_unsafe_mutex); 99 return ret; 94 100 #endif 95 101 … … 99 105 string Convert::asctime(const struct tm &tm) 100 106 { 101 char *ret;107 char *ret; 102 108 #ifndef MULTITHREADED 103 109 104 ret=::asctime(&tm);110 ret=::asctime(&tm); 105 111 106 112 #else //MULTITHREADED 107 113 108 char buf[26];114 char buf[26]; 109 115 #if defined LINUX // || android? 110 ret=::asctime_r(&tm,buf);116 ret=::asctime_r(&tm,buf); 111 117 #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? 112 asctime_s(buf,sizeof(buf),&tm);113 ret=buf;118 asctime_s(buf, sizeof(buf), &tm); 119 ret = buf; 114 120 #else //borland? 115 pthread_mutex_lock(&fix_unsafe_mutex);116 strcpy(buf,::asctime(&tm));117 ret=buf;118 pthread_mutex_unlock(&fix_unsafe_mutex);121 pthread_mutex_lock(&fix_unsafe_mutex); 122 strcpy(buf,::asctime(&tm)); 123 ret=buf; 124 pthread_mutex_unlock(&fix_unsafe_mutex); 119 125 #endif 120 126 #endif 121 127 122 return string(ret,24); //24 znaki z pominieciem ostatniego \n128 return string(ret, 24); //24 znaki z pominieciem ostatniego \n 123 129 } 124 -
cpp/common/Convert.h
r197 r247 10 10 #include "nonstd_stl.h" 11 11 #include "2d.h" 12 #include <stdint.h> 13 12 14 13 15 typedef XY<double> Pt2D; … … 17 19 { 18 20 public: 19 static int toInt(string s); 20 static float toFloat(string s); 21 static string toLowerCase(string s); 22 static string toUpperCase(string s); 23 static char toLowerCase(char c); 24 static char toUpperCase(char c); 25 template<class T> static string _toString(const T& value); 26 static string toString(unsigned int v); 27 static string toString(int v); 28 static string toString(short v); 29 static string toString(float v); 30 static string toString(double v);// ze niby w badzie ma nie byc? ale w frams na pewno ma byc bo tu same double 31 static string zeroPad(string s,int l) {while((int)s.length()<l) s=string("0")+s; return s;} 21 static int toInt(string s); 22 static float toFloat(string s); 23 static string toLowerCase(string s); 24 static string toUpperCase(string s); 25 static char toLowerCase(char c); 26 static char toUpperCase(char c); 27 template<class T> static string _toString(const T& value); 28 static string toString(unsigned int v); 29 static string toString(int v); 30 static string toString(short v); 31 static string toString(float v); 32 static string toString(double v); 33 static string zeroPad(string s, int l) { while ((int)s.length() < l) s = string("0") + s; return s; } 34 static uint32_t hexToInt(const string& col); 32 35 33 static double toRadians(double kat) {return kat*M_PI/180;}34 static double toDegrees(double kat) {return kat/M_PI*180;}35 static double atan_2(double y,double x) {if (x==0 && y==0) return 0; else return atan2(y,x);} //needed by borland 5/6 only?36 static double toRadians(double angle) { return angle*M_PI / 180; } 37 static double toDegrees(double angle) { return angle / M_PI * 180; } 38 static double atan_2(double y, double x) { if (x == 0 && y == 0) return 0; else return atan2(y, x); } //needed by borland 5/6 only? 36 39 37 static double odleglosc_sq(double x1,double y1,double x2,double y2) //odleglosc do kwadratu, wystarczy do porownywania 38 {double dx=x2-x1, dy=y2-y1; return dx*dx+dy*dy;} 39 static double odleglosc_sq(const Pt2D& p1,const Pt2D& p2) //odleglosc do kwadratu 40 {return odleglosc_sq(p1.x,p1.y,p2.x,p2.y);} 40 static double odleglosc_sq(double x1, double y1, double x2, double y2) //odleglosc do kwadratu, wystarczy do porownywania 41 { 42 double dx = x2 - x1, dy = y2 - y1; return dx*dx + dy*dy; 43 } 44 static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu 45 { 46 return odleglosc_sq(p1.x, p1.y, p2.x, p2.y); 47 } 41 48 42 static double odleglosc(double x1,double y1,double x2,double y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} 43 static double odleglosc(const Pt2D& p1,const Pt2D& p2) 44 {return sqrt(odleglosc_sq(p1,p2));} 45 46 //static float odleglosc(int x1,int y1,int x2,int y2) {float dx=x1-x2; float dy=y1-y2; return sqrt(dx*dx+dy*dy);} 47 //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} 48 //static float odleglosc_sq(float x1,float y1,float x2,float y2) {float dx=x1-x2; float dy=y1-y2; return dx*dx+dy*dy;} 49 static double odleglosc(double x1, double y1, double x2, double y2) { return sqrt(odleglosc_sq(x1, y1, x2, y2)); } 50 static double odleglosc(const Pt2D& p1, const Pt2D& p2) 51 { 52 return sqrt(odleglosc_sq(p1, p2)); 53 } 49 54 50 static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania 51 static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania 55 //static float odleglosc(int x1,int y1,int x2,int y2) {float dx=x1-x2; float dy=y1-y2; return sqrt(dx*dx+dy*dy);} 56 //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));} 57 //static float odleglosc_sq(float x1,float y1,float x2,float y2) {float dx=x1-x2; float dy=y1-y2; return dx*dx+dy*dy;} 58 59 static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania 60 static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania 61 62 static std::wstring strTOwstr(const char *s) 63 { 64 string str(s); 65 return std::wstring(str.begin(), str.end()); 66 } 67 68 static string wstrTOstr(const wchar_t *s) 69 { 70 wstring str(s); 71 return string(str.begin(), str.end()); 72 } 73 74 static string wstrTOutf8(const wchar_t *s) 75 { 76 if (s == NULL) return ""; 77 string res; 78 wchar_t *wcp = (wchar_t*)s; 79 while (*wcp != 0) 80 { 81 int c = *wcp; 82 if (c < 0x80) res += c; 83 else if (c < 0x800) { res += 192 + c / 64; res += 128 + c % 64; } 84 else if (c - 0xd800u < 0x800) res += "<ERR-CHAR>"; 85 else if (c < 0x10000) { res += 224 + c / 4096; res += 128 + c / 64 % 64; res += 128 + c % 64; } 86 else if (c < 0x110000) { res += 240 + c / 262144; res += 128 + c / 4096 % 64; res += 128 + c / 64 % 64; res += 128 + c % 64; } 87 else res += "<ERR-CHAR>"; 88 wcp++; 89 } 90 return res; 91 } 92 //#endif 52 93 }; 53 94 54 95 55 96 56 struct Kat //znormalizowany k¹t w radianach [0,2pi) i stopniach [0,360) z obliczonymi sinus i cosinus oraz intem[0,359]97 struct Angle //normalized angle in radians [0,2pi) and degrees [0,360) with pre-computed sine and cosine and degree as integer [0,359] 57 98 { 58 99 private: 59 double kat; //w radianach, read-only100 double angle; //in radians, read-only 60 101 public: 61 double kat_stopnie; //read-only62 int kat_stopnie_int; //read-only102 double angle_deg; //read-only 103 int angle_deg_int; //read-only 63 104 64 Kat() {set(0);}65 Kat(double k) {set(k);}66 Kat(Kat &kt) {set(kt.get());}67 Kat(double dy,double dx) {set(dy,dx);}68 void set(double k) { k=fmod(k,M_PI*2); if (k<0) k+=M_PI*2; kat=k; sinus=sin(k); cosinus=cos(k); kat_stopnie=Convert::toDegrees(kat); kat_stopnie_int=roundToInt(kat_stopnie); kat_stopnie_int%=360; }69 void set(double dy, double dx) {set(Convert::atan_2(dy,dx));}70 void add(double dk) { set(kat+dk);}71 void add( Kat &kt) {set(kat+kt.get());}72 double get() { return kat;}73 double sin us,cosinus;105 Angle() { set(0); } 106 Angle(double k) { set(k); } 107 Angle(Angle &kt) { set(kt.get()); } 108 Angle(double dy, double dx) { set(dy, dx); } 109 void set(double k) { k = fmod(k, M_PI * 2); if (k < 0) k += M_PI * 2; angle = k; sine = sin(k); cosine = cos(k); angle_deg = Convert::toDegrees(angle); angle_deg_int = roundToInt(angle_deg); angle_deg_int %= 360; } 110 void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); } 111 void add(double dk) { set(angle + dk); } 112 void add(Angle &kt) { set(angle + kt.get()); } 113 double get() { return angle; } 114 double sine, cosine; 74 115 }; 75 116 -
cpp/common/nonstd.h
r244 r247 57 57 #endif 58 58 59 #if defined MACOS || defined __ANDROID__ 59 #if defined MACOS || defined __ANDROID__ || defined IPHONE 60 60 #define stricmp(a,b) strcasecmp(a,b) 61 61 #endif … … 86 86 #ifdef LINUX 87 87 #define GET_APP_HOME "./" 88 #define GET_APP_RESOURCES "./" 89 #endif 90 91 #ifdef MACOS 92 #ifdef MACOS_APP_DIR //separate home/resources 93 #include <string> 94 std::string getAppHome(); 95 #define GET_APP_HOME getAppHome() 96 #else //old style 97 #define GET_APP_HOME "./" 98 #endif 88 99 #define GET_APP_RESOURCES "./" 89 100 #endif -
cpp/common/nonstd_math.cpp
r227 r247 22 22 23 23 24 #ifdef IPHONE 25 //TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app 26 void fpExceptInit() 27 {} 28 29 void fpExceptEnable() 30 {} 31 32 void fpExceptDisable() 33 {} 34 #endif 24 35 25 36 26 #if defined LINUX || defined TIZEN || defined __ANDROID__ || defined IPHONE37 #if defined LINUX || defined TIZEN || defined __ANDROID__ 27 38 28 39 #include <fenv.h> -
cpp/common/nonstd_stdio.cpp
r227 r247 5 5 #include "nonstd_stdio.h" 6 6 #include "nonstd.h" 7 #include "Convert.h" //strTOwstr() 8 #include <common/stl-util.h> 7 9 8 10 #if defined _WIN32 && !defined SHP 9 11 //<unistd.h> not needed for unlink() 10 12 #include "Shlwapi.h" //PathIsRelative() 11 #include "Util.h" //strTOwstr()13 #include <sys/stat.h> //_stat 12 14 #else 13 15 #include <unistd.h> … … 23 25 } 24 26 27 bool directoryExists(const char* path) 28 { 29 struct _stat s; 30 if (_stat(path,&s)!=0) return false; 31 return S_ISDIR(s.st_mode); 32 } 33 34 bool makeDirectory(const char* path) 35 { 36 #ifdef _WIN32 37 return mkdir(path)==0; 38 #else 39 return mkdir(path,0777)==0; 40 #endif 41 } 42 43 bool makeDirectories(const char* path) 44 { 45 if (directoryExists(path)) return true; 46 string parentdir=getFileDir(path); 47 if (!makeDirectories(parentdir.c_str())) return false; 48 return makeDirectory(path); 49 } 50 51 int getFileSize(const char* path) 52 { 53 int size; 54 MFILE *f=mfopen(path,FOPEN_READ_BINARY); 55 if (f==NULL) return -1; 56 size=getFileSize(f); 57 mfclose(f); 58 return size; 59 } 60 61 int getFileSize(MFILE *f) 62 { 63 int saved_pos = mftell(f); 64 mfseek(f, 0, SEEK_END); 65 int size = mftell(f); 66 mfseek(f, saved_pos, SEEK_SET); 67 return size; 68 } 69 25 70 bool removeFile(const char* path) 26 71 { … … 35 80 return PathIsRelative(fname) == FALSE; //no wide char for old borland compiler 36 81 #else 37 return PathIsRelative( Util::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx82 return PathIsRelative(Convert::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx 38 83 #endif 39 84 #else -
cpp/common/nonstd_stdio.h
r227 r247 7 7 8 8 bool fileExists(const char* path); 9 bool directoryExists(const char* path); 10 bool makeDirectory(const char* path); 11 bool makeDirectories(const char* path); 9 12 bool removeFile(const char* path); 10 13 bool isAbsolutePath(const char* fname); 14 int getFileSize(const char* path); 11 15 12 16 #ifdef _WIN32 … … 24 28 #ifndef MOBILE2D 25 29 #include <io.h> //borland compiler: include <io.h> before <dir.h> causes the SimWorld class in "simul.h" be unrecognized, for unknown reason :O moreover, this problem is only pertinent to the CLI project, not GUI. Maybe this is caused by global defines like NOVCL, NO_STRICT etc.? 26 #define makeDirectory(name) mkdir(name) 30 // #define makeDirectory(name) mkdir(name) 27 31 #endif 28 32 33 #define S_ISDIR(x) ((x & _S_IFDIR)==_S_IFDIR) 29 34 30 35 #else … … 32 37 #include <unistd.h> 33 38 #include <sys/stat.h> 34 #define makeDirectory(name) mkdir(name,0777)39 // #define makeDirectory(name) mkdir(name,0777) 35 40 #define _unlink unlink //_unlink jest ISO-conformant, unlink jest POSIX-deprecated 36 41 #define _stat stat 37 42 #endif 38 43 … … 95 100 #endif 96 101 102 int getFileSize(MFILE *f); 103 97 104 #endif -
cpp/common/random.h
r197 r247 12 12 #include <sys/stat.h> 13 13 #include <fcntl.h> 14 #endif 15 #ifdef __BORLANDC__ 16 #include <stdint.h> //uintptr_t in borland 14 17 #endif 15 18 #ifdef _WIN32 … … 76 79 { 77 80 counter++; 78 seed = time(NULL); //time (seconds); could use hi-res timer but then we would depend on common/timer.h79 seed ^= counter; //incremented value, possibly randomly initialized80 seed ^= (unsigned int) &counter; //memory address81 seed = time(NULL); //time (seconds); could use hi-res timer but then we would depend on common/timer.h 82 seed ^= counter; //incremented value, possibly randomly initialized 83 seed ^= (unsigned int)(uintptr_t)&counter; //memory address 81 84 } 82 85 #ifdef _WIN32 //add more randomness from uuid -
cpp/common/stl-util.cpp
r246 r247 10 10 #include "framsg.h" 11 11 #include <assert.h> 12 #ifdef USE_VIRTFILE 13 #include <frams/virtfile/virtfile.h> 14 #endif 15 #ifdef __BORLANDC__ 16 #define va_copy(to,from) to=from //borland does not have va_copy() at all; va_list is just a pointer in borland 17 #endif 12 18 13 19 string ssprintf_va(const char* format, va_list ap) … … 16 22 long size = 256; 17 23 char* buf; 24 va_list ap_copy; // "va_list ap" can only by used once by printf-type functions as they advance the current argument pointer (crashed on linux x86_64) 25 // (does not apply to SString::sprintf, it does not have the va_list variant) 18 26 19 27 //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying 20 28 #ifdef USE_VSCPRINTF 21 size = _vscprintf(format, ap) + 1; //+1 for terminating null character 29 va_copy(ap_copy,ap); 30 size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character 31 va_end(ap_copy); 22 32 #endif 23 33 … … 26 36 buf = (char*)malloc(size); 27 37 assert(buf != NULL); 28 int n = vsnprintf(buf, size, format, ap); 38 va_copy(ap_copy,ap); 39 int n = vsnprintf(buf, size, format, ap_copy); 40 va_end(ap_copy); 29 41 if (n > -1 && n < size) 30 42 { … … 54 66 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file) 55 67 { 68 bool ok=false; 69 #ifdef USE_VIRTFILE 70 if (!isAbsolutePath(filename)) 71 { 72 VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY); 73 if (f) 74 { 75 int size=f->getSize(); 76 data.resize(size); 77 int przeczytane = f->Vread(&data[0], size, 1); 78 ok = przeczytane == 1; 79 delete f; 80 } 81 } 82 else 83 #endif 84 { 56 85 MFILE *f = mfopen(filename, FOPEN_READ_BINARY); 57 bool ok = f != NULL;58 86 if (f) 59 87 { 60 mfseek(f, 0, SEEK_END); 61 long size = mftell(f); 62 mfseek(f, 0, SEEK_SET); 88 int size=getFileSize(f); 63 89 data.resize(size); 64 90 int przeczytane = mfread(&data[0], size, 1, f); 65 91 mfclose(f); 66 ok &= przeczytane == 1; 92 ok = przeczytane == 1; 93 } 67 94 } 68 95 if (warn_on_missing_file && !ok)
Note: See TracChangeset
for help on using the changeset viewer.