Changeset 247 for cpp/common/Convert.cpp
- Timestamp:
- 11/07/14 17:51:01 (9 years ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.