Changeset 247 for cpp/common/Convert.cpp


Ignore:
Timestamp:
11/07/14 17:51:01 (9 years ago)
Author:
Maciej Komosinski
Message:

Sources support both 32-bit and 64-bit, and more compilers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r197 r247  
    88
    99#if defined __ANDROID__ || defined __BORLANDC__
    10  #include <ctype.h> //toupper, tolower
     10#include <ctype.h> //toupper, tolower
    1111#endif
    1212
    1313#ifdef SHP
    14  #include <cstdlib>
     14#include <cstdlib>
    1515#else
    16  #include <stdlib.h>
     16#include <stdlib.h>
    1717#endif
    1818
     
    2020
    2121
    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);}
     22int Convert::toInt(string s) { return atoi(s.c_str()); }
     23float Convert::toFloat(string s) { return (float)atof(s.c_str()); }
     24string Convert::toLowerCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower);  return s; }
     25string Convert::toUpperCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper);  return s; }
     26char Convert::toLowerCase(char c) { return (char)tolower(c); }
     27char Convert::toUpperCase(char c) { return (char)toupper(c); }
    3028
    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";}
     29template<class T> const char* printf_format_for(const T& value) { return "unknown type"; }
     30template<> const char* printf_format_for(const unsigned int& value) { return "%u"; }
     31template<> const char* printf_format_for(const int& value) { return "%d"; }
     32template<> const char* printf_format_for(const short& value) { return "%d"; }
     33template<> const char* printf_format_for(const float& value) { return "%g"; }
     34template<> const char* printf_format_for(const double& value) { return "%g"; }
    3735
    3836template<class T> string Convert::_toString(const T& value)
    3937{
    40 char buf[30];
    41 sprintf(buf,printf_format_for(value),value);
    42 return string(buf);
    43 /*
    44 #ifndef MULTITHREADED
     38        char buf[30];
     39        sprintf(buf, printf_format_for(value), value);
     40        return string(buf);
     41        /*
     42        #ifndef MULTITHREADED
    4543        static
    46 #endif
     44        #endif
    4745        std::ostringstream oss; //pod VS tworzenie go trwa dlugo nawet w wersji release (szczegolnie jak np konwertuje sie cos setki tysiecy razy)
    4846        //dlatego robimy go raz (static) i potem tylko czyscimy
     
    5149        oss.clear(); //clear error flag
    5250        oss.str(""); //set empty string
    53   oss << value;
    54   return oss.str();
    55 */
     51        oss << value;
     52        return oss.str();
     53        */
    5654}
    5755
    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);}
     56string Convert::toString(unsigned int v) { return _toString(v); }
     57string Convert::toString(int v) { return _toString(v); }
     58string Convert::toString(short v) { return _toString(v); }
     59string Convert::toString(float v) { return _toString(v); }
     60string Convert::toString(double v) { return _toString(v); }
     61
     62uint32_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}
    6369
    6470#ifdef MULTITHREADED
     
    6975//z mutexa gdy drugi dalej by go inicjalizowal
    7076#include "threads.h"
    71 static pthread_mutex_t fix_unsafe_mutex=PTHREAD_MUTEX_INITIALIZER;
     77static pthread_mutex_t fix_unsafe_mutex = PTHREAD_MUTEX_INITIALIZER;
    7278#endif
    7379
     
    7682#ifndef MULTITHREADED
    7783
    78 return *::localtime(&timep);
     84        return *::localtime(&timep);
    7985
    8086#else
    8187
    82 struct tm ret;
     88        struct tm ret;
    8389
    8490#if defined LINUX // || android?
    85 return *::localtime_r(&timep,&ret);
     91        return *::localtime_r(&timep,&ret);
    8692#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
    87 ::localtime_s(&ret,&timep);
    88 return ret;
     93        ::localtime_s(&ret, &timep);
     94        return ret;
    8995#else //borland?
    90 pthread_mutex_lock(&fix_unsafe_mutex);
    91 ret=*::localtime(&timep);
    92 pthread_mutex_unlock(&fix_unsafe_mutex);
    93 return ret;
     96        pthread_mutex_lock(&fix_unsafe_mutex);
     97        ret=*::localtime(&timep);
     98        pthread_mutex_unlock(&fix_unsafe_mutex);
     99        return ret;
    94100#endif
    95101
     
    99105string Convert::asctime(const struct tm &tm)
    100106{
    101 char *ret;
     107        char *ret;
    102108#ifndef MULTITHREADED
    103109
    104 ret=::asctime(&tm);
     110        ret=::asctime(&tm);
    105111
    106112#else //MULTITHREADED
    107113
    108 char buf[26];
     114        char buf[26];
    109115#if defined LINUX // || android?
    110 ret=::asctime_r(&tm,buf);
     116        ret=::asctime_r(&tm,buf);
    111117#elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
    112 asctime_s(buf,sizeof(buf),&tm);
    113 ret=buf;
     118        asctime_s(buf, sizeof(buf), &tm);
     119        ret = buf;
    114120#else //borland?
    115 pthread_mutex_lock(&fix_unsafe_mutex);
    116 strcpy(buf,::asctime(&tm));
    117 ret=buf;
    118 pthread_mutex_unlock(&fix_unsafe_mutex);
     121        pthread_mutex_lock(&fix_unsafe_mutex);
     122        strcpy(buf,::asctime(&tm));
     123        ret=buf;
     124        pthread_mutex_unlock(&fix_unsafe_mutex);
    119125#endif
    120126#endif
    121127
    122 return string(ret,24); //24 znaki z pominieciem ostatniego \n
     128        return string(ret, 24); //24 znaki z pominieciem ostatniego \n
    123129}
    124 
Note: See TracChangeset for help on using the changeset viewer.