[109] | 1 | #include "Convert.h" |
---|
| 2 | |
---|
| 3 | #include <sstream> |
---|
| 4 | |
---|
| 5 | #if defined __ANDROID__ || defined __BORLANDC__ |
---|
| 6 | #include <ctype.h> //toupper, tolower |
---|
| 7 | #endif |
---|
| 8 | |
---|
| 9 | #ifdef SHP |
---|
| 10 | #include <cstdlib> |
---|
| 11 | #else |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | #endif |
---|
| 14 | |
---|
| 15 | #include <stdio.h> |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | int Convert::toInt(string s) {return atoi(s.c_str());} |
---|
| 19 | float Convert::toFloat(string s) {return (float)atof(s.c_str());} |
---|
| 20 | #ifndef __BORLANDC__ //szok: borland kompiluje ten plik w GUI i CLI ale nie w teatrze. Z jakichs powodow nie potrafi znalezc ::tolower i ::toupper, moze chodzi o pewne definy w <cctype> ale czemu tylko w teatrze?! |
---|
| 21 | string Convert::toLowerCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s;} |
---|
| 22 | string Convert::toUpperCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s;} |
---|
| 23 | #endif |
---|
| 24 | char Convert::toLowerCase(char c) {return (char)tolower(c);} |
---|
| 25 | char Convert::toUpperCase(char c) {return (char)toupper(c);} |
---|
| 26 | |
---|
| 27 | template<class T> const char* printf_format_for(const T& value) {return "unknown type";} |
---|
| 28 | template<> const char* printf_format_for(const unsigned int& value) {return "%u";} |
---|
| 29 | template<> const char* printf_format_for(const int& value) {return "%d";} |
---|
| 30 | template<> const char* printf_format_for(const short& value) {return "%d";} |
---|
| 31 | template<> const char* printf_format_for(const float& value) {return "%g";} |
---|
| 32 | template<> const char* printf_format_for(const double& value) {return "%g";} |
---|
| 33 | |
---|
| 34 | template<class T> string Convert::_toString(const T& value) |
---|
| 35 | { |
---|
| 36 | char buf[30]; |
---|
| 37 | sprintf(buf,printf_format_for(value),value); |
---|
| 38 | return string(buf); |
---|
| 39 | /* |
---|
| 40 | #ifndef MULTITHREADED |
---|
| 41 | static |
---|
| 42 | #endif |
---|
| 43 | std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy) |
---|
| 44 | //dlatego robimy go raz (static) i potem tylko czyscimy |
---|
| 45 | //ciekawostka: kiedy nie byl static, czasy wykonania bogatego w konwersje kawa³ka kodu oscylowa³y w trybie debug |
---|
| 46 | //(5.5s lub 55s) a w release zawsze 57s. Po uzyciu static czas tego samego kodu jest zawsze debug: 0.72s release: 0.33s |
---|
| 47 | oss.clear(); //clear error flag |
---|
| 48 | oss.str(""); //set empty string |
---|
| 49 | oss << value; |
---|
| 50 | return oss.str(); |
---|
| 51 | */ |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | string Convert::toString(unsigned int v) {return _toString(v);} |
---|
| 55 | string Convert::toString(int v) {return _toString(v);} |
---|
| 56 | string Convert::toString(short v) {return _toString(v);} |
---|
| 57 | string Convert::toString(float v) {return _toString(v);} |
---|
| 58 | string Convert::toString(double v) {return _toString(v);} |
---|
| 59 | |
---|
| 60 | #ifdef MULTITHREADED |
---|
| 61 | //jezeli jest tu a nie jako static w funkcji, to inicjalizacja |
---|
| 62 | //nastapi na samym poczatku (w nieprzewidywalnym momencie, ale nie szkodzi(?)) |
---|
| 63 | //gdyby byla w funkcji to teoretycznie dwa watki moglyby wejsc |
---|
| 64 | //do niej rownoczesnie i zaczac inicjalizacje po czym jeden korzystalby |
---|
| 65 | //z mutexa gdy drugi dalej by go inicjalizowal |
---|
| 66 | #include "threads.h" |
---|
| 67 | static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER; |
---|
| 68 | #endif |
---|
| 69 | |
---|
| 70 | struct tm Convert::localtime(const time_t &timep) |
---|
| 71 | { |
---|
| 72 | #ifndef MULTITHREADED |
---|
| 73 | |
---|
| 74 | return *::localtime(&timep); |
---|
| 75 | |
---|
| 76 | #else |
---|
| 77 | |
---|
| 78 | struct tm ret; |
---|
| 79 | |
---|
| 80 | #if defined LINUX // || android? |
---|
| 81 | return *::localtime_r(&timep,&ret); |
---|
| 82 | #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? |
---|
| 83 | ::localtime_s(&ret,&timep); |
---|
| 84 | return ret; |
---|
| 85 | #else //borland? |
---|
| 86 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
| 87 | ret=*::localtime(&timep); |
---|
| 88 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
| 89 | return ret; |
---|
| 90 | #endif |
---|
| 91 | |
---|
| 92 | #endif |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | string Convert::asctime(const struct tm &tm) |
---|
| 96 | { |
---|
| 97 | char *ret; |
---|
| 98 | #ifndef MULTITHREADED |
---|
| 99 | |
---|
| 100 | ret=::asctime(&tm); |
---|
| 101 | |
---|
| 102 | #else //MULTITHREADED |
---|
| 103 | |
---|
| 104 | char buf[26]; |
---|
| 105 | #if defined LINUX // || android? |
---|
| 106 | ret=::asctime_r(&tm,buf); |
---|
| 107 | #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? |
---|
| 108 | asctime_s(buf,sizeof(buf),&tm); |
---|
| 109 | ret=buf; |
---|
| 110 | #else //borland? |
---|
| 111 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
| 112 | strcpy(buf,::asctime(&tm)); |
---|
| 113 | ret=buf; |
---|
| 114 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
| 115 | #endif |
---|
| 116 | #endif |
---|
| 117 | |
---|
| 118 | return string(ret,24); //24 znaki z pominieciem ostatniego \n |
---|
| 119 | } |
---|