1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include "Convert.h" |
---|
6 | |
---|
7 | #include <sstream> |
---|
8 | |
---|
9 | #if defined __ANDROID__ || defined __BORLANDC__ |
---|
10 | #include <ctype.h> //toupper, tolower |
---|
11 | #endif |
---|
12 | |
---|
13 | #ifdef SHP |
---|
14 | #include <cstdlib> |
---|
15 | #else |
---|
16 | #include <stdlib.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
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);} |
---|
30 | |
---|
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";} |
---|
37 | |
---|
38 | template<class T> string Convert::_toString(const T& value) |
---|
39 | { |
---|
40 | char buf[30]; |
---|
41 | sprintf(buf,printf_format_for(value),value); |
---|
42 | return string(buf); |
---|
43 | /* |
---|
44 | #ifndef MULTITHREADED |
---|
45 | static |
---|
46 | #endif |
---|
47 | std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy) |
---|
48 | //dlatego robimy go raz (static) i potem tylko czyscimy |
---|
49 | //ciekawostka: kiedy nie byl static, czasy wykonania bogatego w konwersje kawa³ka kodu oscylowa³y w trybie debug |
---|
50 | //(5.5s lub 55s) a w release zawsze 57s. Po uzyciu static czas tego samego kodu jest zawsze debug: 0.72s release: 0.33s |
---|
51 | oss.clear(); //clear error flag |
---|
52 | oss.str(""); //set empty string |
---|
53 | oss << value; |
---|
54 | return oss.str(); |
---|
55 | */ |
---|
56 | } |
---|
57 | |
---|
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);} |
---|
63 | |
---|
64 | #ifdef MULTITHREADED |
---|
65 | //jezeli jest tu a nie jako static w funkcji, to inicjalizacja |
---|
66 | //nastapi na samym poczatku (w nieprzewidywalnym momencie, ale nie szkodzi(?)) |
---|
67 | //gdyby byla w funkcji to teoretycznie dwa watki moglyby wejsc |
---|
68 | //do niej rownoczesnie i zaczac inicjalizacje po czym jeden korzystalby |
---|
69 | //z mutexa gdy drugi dalej by go inicjalizowal |
---|
70 | #include "threads.h" |
---|
71 | static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER; |
---|
72 | #endif |
---|
73 | |
---|
74 | struct tm Convert::localtime(const time_t &timep) |
---|
75 | { |
---|
76 | #ifndef MULTITHREADED |
---|
77 | |
---|
78 | return *::localtime(&timep); |
---|
79 | |
---|
80 | #else |
---|
81 | |
---|
82 | struct tm ret; |
---|
83 | |
---|
84 | #if defined LINUX // || android? |
---|
85 | return *::localtime_r(&timep,&ret); |
---|
86 | #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? |
---|
87 | ::localtime_s(&ret,&timep); |
---|
88 | return ret; |
---|
89 | #else //borland? |
---|
90 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
91 | ret=*::localtime(&timep); |
---|
92 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
93 | return ret; |
---|
94 | #endif |
---|
95 | |
---|
96 | #endif |
---|
97 | } |
---|
98 | |
---|
99 | string Convert::asctime(const struct tm &tm) |
---|
100 | { |
---|
101 | char *ret; |
---|
102 | #ifndef MULTITHREADED |
---|
103 | |
---|
104 | ret=::asctime(&tm); |
---|
105 | |
---|
106 | #else //MULTITHREADED |
---|
107 | |
---|
108 | char buf[26]; |
---|
109 | #if defined LINUX // || android? |
---|
110 | ret=::asctime_r(&tm,buf); |
---|
111 | #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? |
---|
112 | asctime_s(buf,sizeof(buf),&tm); |
---|
113 | ret=buf; |
---|
114 | #else //borland? |
---|
115 | pthread_mutex_lock(&fix_unsafe_mutex); |
---|
116 | strcpy(buf,::asctime(&tm)); |
---|
117 | ret=buf; |
---|
118 | pthread_mutex_unlock(&fix_unsafe_mutex); |
---|
119 | #endif |
---|
120 | #endif |
---|
121 | |
---|
122 | return string(ret,24); //24 znaki z pominieciem ostatniego \n |
---|
123 | } |
---|
124 | |
---|