source: cpp/common/Convert.cpp @ 155

Last change on this file since 155 was 122, checked in by sz, 11 years ago

(c)opyright information added

  • Property svn:eol-style set to native
File size: 4.0 KB
RevLine 
[122]1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
[109]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
22int Convert::toInt(string s) {return atoi(s.c_str());}
23float Convert::toFloat(string s) {return (float)atof(s.c_str());}
[122]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??
[109]25string Convert::toLowerCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::tolower);  return s;}
26string Convert::toUpperCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::toupper);  return s;}
27#endif
28char Convert::toLowerCase(char c) {return (char)tolower(c);}
29char Convert::toUpperCase(char c) {return (char)toupper(c);}
30
31template<class T> const char* printf_format_for(const T& value) {return "unknown type";}
32template<> const char* printf_format_for(const unsigned int& value) {return "%u";}
33template<> const char* printf_format_for(const int& value) {return "%d";}
34template<> const char* printf_format_for(const short& value) {return "%d";}
35template<> const char* printf_format_for(const float& value) {return "%g";}
36template<> const char* printf_format_for(const double& value) {return "%g";}
37
38template<class T> string Convert::_toString(const T& value)
39{
40char buf[30];
41sprintf(buf,printf_format_for(value),value);
42return 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
58string Convert::toString(unsigned int v) {return _toString(v);}
59string Convert::toString(int v) {return _toString(v);}
60string Convert::toString(short v) {return _toString(v);}
61string Convert::toString(float v) {return _toString(v);}
62string 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"
71static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER;
72#endif
73
74struct tm Convert::localtime(const time_t &timep)
75{
76#ifndef MULTITHREADED
77
78return *::localtime(&timep);
79
80#else
81
82struct tm ret;
83
84#if defined LINUX // || android?
85return *::localtime_r(&timep,&ret);
86#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
87::localtime_s(&ret,&timep);
88return ret;
89#else //borland?
90pthread_mutex_lock(&fix_unsafe_mutex);
91ret=*::localtime(&timep);
92pthread_mutex_unlock(&fix_unsafe_mutex);
93return ret;
94#endif
95
96#endif
97}
98
99string Convert::asctime(const struct tm &tm)
100{
101char *ret;
102#ifndef MULTITHREADED
103
104ret=::asctime(&tm);
105
106#else //MULTITHREADED
107
108char buf[26];
109#if defined LINUX // || android?
110ret=::asctime_r(&tm,buf);
111#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
112asctime_s(buf,sizeof(buf),&tm);
113ret=buf;
114#else //borland?
115pthread_mutex_lock(&fix_unsafe_mutex);
116strcpy(buf,::asctime(&tm));
117ret=buf;
118pthread_mutex_unlock(&fix_unsafe_mutex);
119#endif
120#endif
121
122return string(ret,24); //24 znaki z pominieciem ostatniego \n
123}
[122]124
Note: See TracBrowser for help on using the repository browser.