// This file is a part of the Framsticks GDK. // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. // Refer to http://www.framsticks.com/ for further information. #include "Convert.h" #include #if defined __ANDROID__ || defined __BORLANDC__ #include //toupper, tolower #endif #ifdef SHP #include #else #include #endif #include int Convert::toInt(string s) {return atoi(s.c_str());} float Convert::toFloat(string s) {return (float)atof(s.c_str());} #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 , but why only in the theater app?? string Convert::toLowerCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s;} string Convert::toUpperCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s;} #endif char Convert::toLowerCase(char c) {return (char)tolower(c);} char Convert::toUpperCase(char c) {return (char)toupper(c);} template const char* printf_format_for(const T& value) {return "unknown type";} template<> const char* printf_format_for(const unsigned int& value) {return "%u";} template<> const char* printf_format_for(const int& value) {return "%d";} template<> const char* printf_format_for(const short& value) {return "%d";} template<> const char* printf_format_for(const float& value) {return "%g";} template<> const char* printf_format_for(const double& value) {return "%g";} template string Convert::_toString(const T& value) { char buf[30]; sprintf(buf,printf_format_for(value),value); return string(buf); /* #ifndef MULTITHREADED static #endif std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy) //dlatego robimy go raz (static) i potem tylko czyscimy //ciekawostka: kiedy nie byl static, czasy wykonania bogatego w konwersje kawałka kodu oscylowały w trybie debug //(5.5s lub 55s) a w release zawsze 57s. Po uzyciu static czas tego samego kodu jest zawsze debug: 0.72s release: 0.33s oss.clear(); //clear error flag oss.str(""); //set empty string oss << value; return oss.str(); */ } string Convert::toString(unsigned int v) {return _toString(v);} string Convert::toString(int v) {return _toString(v);} string Convert::toString(short v) {return _toString(v);} string Convert::toString(float v) {return _toString(v);} string Convert::toString(double v) {return _toString(v);} #ifdef MULTITHREADED //jezeli jest tu a nie jako static w funkcji, to inicjalizacja //nastapi na samym poczatku (w nieprzewidywalnym momencie, ale nie szkodzi(?)) //gdyby byla w funkcji to teoretycznie dwa watki moglyby wejsc //do niej rownoczesnie i zaczac inicjalizacje po czym jeden korzystalby //z mutexa gdy drugi dalej by go inicjalizowal #include "threads.h" static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER; #endif struct tm Convert::localtime(const time_t &timep) { #ifndef MULTITHREADED return *::localtime(&timep); #else struct tm ret; #if defined LINUX // || android? return *::localtime_r(&timep,&ret); #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? ::localtime_s(&ret,&timep); return ret; #else //borland? pthread_mutex_lock(&fix_unsafe_mutex); ret=*::localtime(&timep); pthread_mutex_unlock(&fix_unsafe_mutex); return ret; #endif #endif } string Convert::asctime(const struct tm &tm) { char *ret; #ifndef MULTITHREADED ret=::asctime(&tm); #else //MULTITHREADED char buf[26]; #if defined LINUX // || android? ret=::asctime_r(&tm,buf); #elif defined _WIN32 && !defined __BORLANDC__ // and not bada? asctime_s(buf,sizeof(buf),&tm); ret=buf; #else //borland? pthread_mutex_lock(&fix_unsafe_mutex); strcpy(buf,::asctime(&tm)); ret=buf; pthread_mutex_unlock(&fix_unsafe_mutex); #endif #endif return string(ret,24); //24 znaki z pominieciem ostatniego \n }