source: cpp/common/Convert.cpp @ 109

Last change on this file since 109 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

  • Property svn:eol-style set to native
File size: 3.7 KB
RevLine 
[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
18int Convert::toInt(string s) {return atoi(s.c_str());}
19float 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?!
21string Convert::toLowerCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::tolower);  return s;}
22string Convert::toUpperCase(string s) {std::transform(s.begin(), s.end(), s.begin(), ::toupper);  return s;}
23#endif
24char Convert::toLowerCase(char c) {return (char)tolower(c);}
25char Convert::toUpperCase(char c) {return (char)toupper(c);}
26
27template<class T> const char* printf_format_for(const T& value) {return "unknown type";}
28template<> const char* printf_format_for(const unsigned int& value) {return "%u";}
29template<> const char* printf_format_for(const int& value) {return "%d";}
30template<> const char* printf_format_for(const short& value) {return "%d";}
31template<> const char* printf_format_for(const float& value) {return "%g";}
32template<> const char* printf_format_for(const double& value) {return "%g";}
33
34template<class T> string Convert::_toString(const T& value)
35{
36char buf[30];
37sprintf(buf,printf_format_for(value),value);
38return 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
54string Convert::toString(unsigned int v) {return _toString(v);}
55string Convert::toString(int v) {return _toString(v);}
56string Convert::toString(short v) {return _toString(v);}
57string Convert::toString(float v) {return _toString(v);}
58string 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"
67static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER;
68#endif
69
70struct tm Convert::localtime(const time_t &timep)
71{
72#ifndef MULTITHREADED
73
74return *::localtime(&timep);
75
76#else
77
78struct tm ret;
79
80#if defined LINUX // || android?
81return *::localtime_r(&timep,&ret);
82#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
83::localtime_s(&ret,&timep);
84return ret;
85#else //borland?
86pthread_mutex_lock(&fix_unsafe_mutex);
87ret=*::localtime(&timep);
88pthread_mutex_unlock(&fix_unsafe_mutex);
89return ret;
90#endif
91
92#endif
93}
94
95string Convert::asctime(const struct tm &tm)
96{
97char *ret;
98#ifndef MULTITHREADED
99
100ret=::asctime(&tm);
101
102#else //MULTITHREADED
103
104char buf[26];
105#if defined LINUX // || android?
106ret=::asctime_r(&tm,buf);
107#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
108asctime_s(buf,sizeof(buf),&tm);
109ret=buf;
110#else //borland?
111pthread_mutex_lock(&fix_unsafe_mutex);
112strcpy(buf,::asctime(&tm));
113ret=buf;
114pthread_mutex_unlock(&fix_unsafe_mutex);
115#endif
116#endif
117
118return string(ret,24); //24 znaki z pominieciem ostatniego \n
119}
Note: See TracBrowser for help on using the repository browser.