- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/src/frost_utils_str.cpp

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*                                        */
    00005
    00006 #include "frost_utils_types.h"
    00007 #include "frost_utils_stdhelper.h"
    00008
    00009 #include <sstream>
    00010
    00011 namespace Frost
    00012 {
    00013     typedef std::stringstream string_stream;
    00014
    00015     s_ustr UTF8ToUnicode( const s_str& s )
    00016     {
    00017         return UTF8ToUnicode(s.Get());
    00018     }
    00019
    00020     s_ustr::string UTF8ToUnicode( const s_str::string& s )
    00021     {
    00022         static uchar MAX_ANSI = 127;
    00023         static uchar ESC_C2   = 194;
    00024         static uchar ESC_C3   = 195;
    00025
    00026         s_ustr::string sResult;
    00027
    00028         s_str::string::const_iterator iter;
    00029         uchar cEscape = 0;
    00030
    00031         foreach (iter, s)
    00032         {
    00033             uchar c = *iter;
    00034             if (c <= MAX_ANSI)
    00035             {
    00036                 sResult.push_back(c);
    00037             }
    00038             else
    00039             {
    00040                 if (c == ESC_C2 || c == ESC_C3)
    00041                 {
    00042                     cEscape = c;
    00043                     continue;
    00044                 }
    00045
    00046                 if (cEscape != 0)
    00047                 {
    00048                     if (cEscape == ESC_C3)
    00049                     {
    00050                         // 192 : offset of for "c3" (195) escaped characters (accentuated)
    00051                         // 128 : start offset for these characters (the first one is "c3 80")
    00052                         sResult.push_back(192 + c - 128);
    00053                     }
    00054                     else if (cEscape == ESC_C2)
    00055                     {
    00056                         // 192 : offset of for "c2" (194) escaped characters (misc)
    00057                         // 128 : start offset for these characters (the first one is "c2 80")
    00058                         sResult.push_back(128 + c - 128);
    00059                     }
    00060                 }
    00061             }
    00062         }
    00063
    00064         return sResult;
    00065     }
    00066
    00067     s_uint UTF8ToUnicode( const s_char& c )
    00068     {
    00069         return UTF8ToUnicode(c.Get());
    00070     }
    00071
    00072     uint UTF8ToUnicode( const string_element& c )
    00073     {
    00074         static uchar MAX_ANSI = 127;
    00075
    00076         uchar uc = (uchar)c;
    00077
    00078         if (uc <= MAX_ANSI)
    00079             return uc;
    00080         else
    00081             return 0u;
    00082     }
    00083
    00084     s_str UnicodeToUTF8( const s_ustr& s )
    00085     {
    00086         return UnicodeToUTF8(s.Get());
    00087     }
    00088
    00089     s_str::string UnicodeToUTF8( const s_ustr::string& s )
    00090     {
    00091         static uint MAX_ANSI = 127;
    00092         static uint ESC_C2   = 194;
    00093         static uint ESC_C3   = 195;
    00094
    00095         s_str::string sResult;
    00096
    00097         s_ustr::string::const_iterator iter;
    00098
    00099         foreach (iter, s)
    00100         {
    00101             uint c = *iter;
    00102             if (c <= MAX_ANSI)
    00103             {
    00104                 sResult.push_back(c);
    00105             }
    00106             else
    00107             {
    00108                 if (c < 192)
    00109                 {
    00110                     sResult.push_back((uchar)ESC_C2);
    00111                     sResult.push_back((uchar)(128 + c - 128));
    00112                 }
    00113                 else
    00114                 {
    00115                     sResult.push_back((uchar)ESC_C3);
    00116                     sResult.push_back((uchar)(128 + c - 192));
    00117                 }
    00118             }
    00119         }
    00120
    00121         return sResult;
    00122     }
    00123
    00124     s_char UnicodeToUTF8( const s_uint& c )
    00125     {
    00126         return UnicodeToUTF8(c.Get());
    00127     }
    00128
    00129     string_element UnicodeToUTF8( const uint& c )
    00130     {
    00131         static uint MAX_ANSI = 127;
    00132         if (c <= MAX_ANSI)
    00133             return (uchar)c;
    00134         else
    00135             return '\0';
    00136     }
    00137
    00138     s_uint_t<default_uint> HexToUInt( const s_str_t<string_element>& s )
    00139     {
    00140         default_uint i;
    00141         string_stream ss;
    00142         ss << s.Get();
    00143         ss >> std::hex >> i;
    00144         return i;
    00145     }
    00146
    00147     long StringToInt(const s_str& s)
    00148     {
    00149         long i;
    00150         string_stream ss(s.Get());
    00151         ss >> i;
    00152         return i;
    00153     }
    00154
    00155     long StringToInt(const s_ustr& s)
    00156     {
    00157         return StringToInt(UnicodeToUTF8(s));
    00158     }
    00159
    00160     ulong StringToUInt(const s_str& s)
    00161     {
    00162         ulong ui;
    00163         string_stream ss(s.Get());
    00164         ss >> ui;
    00165         return ui;
    00166     }
    00167
    00168     ulong StringToUInt(const s_ustr& s)
    00169     {
    00170         return StringToUInt(UnicodeToUTF8(s));
    00171     }
    00172
    00173     double StringToFloat(const s_str& s)
    00174     {
    00175         double d;
    00176         string_stream ss(s.Get());
    00177         ss >> d;
    00178         return d;
    00179     }
    00180
    00181     double StringToFloat(const s_ustr& s)
    00182     {
    00183         return StringToFloat(UnicodeToUTF8(s));
    00184     }
    00185
    00186     s_str::string IntToString(const long& i)
    00187     {
    00188         string_stream sStream;
    00189         sStream << i;
    00190         return sStream.str();
    00191     }
    00192
    00193     s_str::string UIntToString(const ulong& ui)
    00194     {
    00195         string_stream sStream;
    00196         sStream << ui;
    00197         return sStream.str();
    00198     }
    00199
    00200     template <class T>
    00201     s_str::string FloatToString(const T& f)
    00202     {
    00203         string_stream sStream;
    00204         sStream.precision(s_float_t<T>::DIGIT);
    00205         sStream << f;
    00206         return sStream.str();
    00207     }
    00208
    00209     template s_str::string FloatToString(const float& f);
    00210     template s_str::string FloatToString(const double& f);
    00211 }