[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[1130] | 2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 5 | #include "Convert.h" |
---|
| 6 | #include <sstream> |
---|
[1130] | 7 | #include <algorithm> |
---|
[109] | 8 | |
---|
| 9 | #if defined __ANDROID__ || defined __BORLANDC__ |
---|
[247] | 10 | #include <ctype.h> //toupper, tolower |
---|
[109] | 11 | #endif |
---|
| 12 | |
---|
| 13 | #ifdef SHP |
---|
[247] | 14 | #include <cstdlib> |
---|
[109] | 15 | #else |
---|
[247] | 16 | #include <stdlib.h> |
---|
[109] | 17 | #endif |
---|
| 18 | |
---|
| 19 | #include <stdio.h> |
---|
| 20 | |
---|
| 21 | |
---|
[247] | 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); } |
---|
[109] | 28 | |
---|
[247] | 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"; } |
---|
[109] | 35 | |
---|
| 36 | template<class T> string Convert::_toString(const T& value) |
---|
| 37 | { |
---|
[247] | 38 | char buf[30]; |
---|
| 39 | sprintf(buf, printf_format_for(value), value); |
---|
| 40 | return string(buf); |
---|
| 41 | /* |
---|
| 42 | #ifndef MULTITHREADED |
---|
[109] | 43 | static |
---|
[247] | 44 | #endif |
---|
[109] | 45 | std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy) |
---|
| 46 | //dlatego robimy go raz (static) i potem tylko czyscimy |
---|
| 47 | //ciekawostka: kiedy nie byl static, czasy wykonania bogatego w konwersje kawa³ka kodu oscylowa³y w trybie debug |
---|
| 48 | //(5.5s lub 55s) a w release zawsze 57s. Po uzyciu static czas tego samego kodu jest zawsze debug: 0.72s release: 0.33s |
---|
| 49 | oss.clear(); //clear error flag |
---|
| 50 | oss.str(""); //set empty string |
---|
[247] | 51 | oss << value; |
---|
| 52 | return oss.str(); |
---|
| 53 | */ |
---|
[109] | 54 | } |
---|
| 55 | |
---|
[247] | 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); } |
---|
[109] | 61 | |
---|
[247] | 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 | } |
---|
| 69 | |
---|
[109] | 70 | #ifdef MULTITHREADED |
---|
| 71 | //jezeli jest tu a nie jako static w funkcji, to inicjalizacja |
---|
| 72 | //nastapi na samym poczatku (w nieprzewidywalnym momencie, ale nie szkodzi(?)) |
---|
| 73 | //gdyby byla w funkcji to teoretycznie dwa watki moglyby wejsc |
---|
| 74 | //do niej rownoczesnie i zaczac inicjalizacje po czym jeden korzystalby |
---|
| 75 | //z mutexa gdy drugi dalej by go inicjalizowal |
---|
[400] | 76 | #if ! ((defined LINUX) || (defined _WIN32 && !defined __BORLANDC__ )) |
---|
| 77 | // only for the "borland?" cases in localtime() and asctime() below |
---|
[109] | 78 | #include "threads.h" |
---|
[247] | 79 | static pthread_mutex_t fix_unsafe_mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
[109] | 80 | #endif |
---|
[400] | 81 | #endif |
---|
[109] | 82 | |
---|
| 83 | struct tm Convert::localtime(const time_t &timep) |
---|
| 84 | { |
---|
| 85 | #ifndef MULTITHREADED |
---|
| 86 | |
---|
[247] | 87 | return *::localtime(&timep); |
---|
[109] | 88 | |
---|
| 89 | #else |
---|
| 90 | |
---|
[247] | 91 | struct tm ret; |
---|
[109] | 92 | #if defined LINUX // || android? |
---|
[1005] | 93 | return *::localtime_r(&timep, &ret); |
---|
[257] | 94 | #elif defined _WIN32 && !defined __BORLANDC__ |
---|
[247] | 95 | ::localtime_s(&ret, &timep); |
---|
| 96 | return ret; |
---|
[109] | 97 | #else //borland? |
---|
[247] | 98 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
[1005] | 99 | ret = *::localtime(&timep); |
---|
[247] | 100 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
| 101 | return ret; |
---|
[109] | 102 | #endif |
---|
| 103 | |
---|
| 104 | #endif |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | string Convert::asctime(const struct tm &tm) |
---|
| 108 | { |
---|
[247] | 109 | char *ret; |
---|
[109] | 110 | #ifndef MULTITHREADED |
---|
| 111 | |
---|
[1005] | 112 | ret = ::asctime(&tm); |
---|
[109] | 113 | |
---|
| 114 | #else //MULTITHREADED |
---|
| 115 | |
---|
[247] | 116 | char buf[26]; |
---|
[109] | 117 | #if defined LINUX // || android? |
---|
[1005] | 118 | ret = ::asctime_r(&tm, buf); |
---|
[257] | 119 | #elif defined _WIN32 && !defined __BORLANDC__ |
---|
[247] | 120 | asctime_s(buf, sizeof(buf), &tm); |
---|
| 121 | ret = buf; |
---|
[109] | 122 | #else //borland? |
---|
[247] | 123 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
[1005] | 124 | strcpy(buf, ::asctime(&tm)); |
---|
| 125 | ret = buf; |
---|
[247] | 126 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
[109] | 127 | #endif |
---|
| 128 | #endif |
---|
| 129 | |
---|
[247] | 130 | return string(ret, 24); //24 znaki z pominieciem ostatniego \n |
---|
[109] | 131 | } |
---|
[281] | 132 | |
---|
| 133 | string Convert::wstrToUtf8(const wchar_t *str) |
---|
| 134 | { |
---|
| 135 | if (str == NULL) return ""; |
---|
| 136 | string res; |
---|
| 137 | wchar_t *wcp = (wchar_t*)str; |
---|
| 138 | while (*wcp != 0) |
---|
| 139 | { |
---|
| 140 | int c = *wcp; |
---|
| 141 | if (c < 0x80) res += c; |
---|
| 142 | else if (c < 0x800) { res += 192 + c / 64; res += 128 + c % 64; } |
---|
| 143 | else if (c - 0xd800u < 0x800) res += "<ERROR-CHAR>"; |
---|
| 144 | else if (c < 0x10000) { res += 224 + c / 4096; res += 128 + c / 64 % 64; res += 128 + c % 64; } |
---|
| 145 | else if (c < 0x110000) { res += 240 + c / 262144; res += 128 + c / 4096 % 64; res += 128 + c / 64 % 64; res += 128 + c % 64; } |
---|
| 146 | else res += "<ERROR-CHAR>"; |
---|
| 147 | wcp++; |
---|
| 148 | } |
---|
| 149 | return res; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | #ifdef _WIN32 |
---|
| 153 | wstring Convert::utf8ToUtf16(const char *str) |
---|
| 154 | { |
---|
| 155 | wstring wstr; |
---|
| 156 | int nOffset = 0; |
---|
[294] | 157 | int nDataLen = strlen(str); //ending \0 is not converted, but resize() below sets the proper length of wstr |
---|
[281] | 158 | int nLenWide = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str + nOffset), |
---|
| 159 | (int)(nDataLen - nOffset), NULL, 0); |
---|
| 160 | wstr.resize(nLenWide); |
---|
| 161 | if (MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str + nOffset), |
---|
| 162 | (int)(nDataLen - nOffset), |
---|
| 163 | &wstr[0], nLenWide) != nLenWide) |
---|
| 164 | { |
---|
| 165 | //ASSERT(false); //some conversion error |
---|
[294] | 166 | return wstr + L"<UTF8_CONV_ERROR>"; |
---|
[281] | 167 | } |
---|
| 168 | return wstr; |
---|
| 169 | } |
---|
| 170 | #endif |
---|