Import fmit upstream version 0.97.6
[fmit.git] / libs / CppAddons / StringAddons.h
1 // Copyright 2003 "Gilles Degottex"
2
3 // This file is part of "CppAddons"
4
5 // "CppAddons" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 // 
10 // "CppAddons" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 // 
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #ifndef _StringAddons_h_
21 #define _StringAddons_h_
22
23 #include <stdio.h>
24 #include <string>
25 #include <sstream>
26
27 #include <ctype.h>
28
29 #include <time.h>
30
31 namespace StringAddons
32 {
33         template<typename Type> std::string toString(Type i)
34         {
35                 std::stringstream str;
36                 str << i;
37                 return str.str();
38         }
39         template<typename Type> void toString(std::string& s, Type i)
40         {
41                 std::stringstream str;
42                 str << i;
43                 s = str.str();
44         }
45
46         inline std::string ereaseEmptyChars(const std::string& str)
47         {
48                 std::string result;
49
50                 for(unsigned int i=0; i<str.length(); i++)
51                         if(!isspace(str[i]))
52                                 result += str[i];
53
54                 return result;
55         }
56
57         template<typename T> inline std::string binValue(T& d)
58         {
59                 char* data = (char*)&d;
60
61                 std::string str;
62
63                 for(int i=sizeof(T)-1; i>=0; i--)
64                 {
65                         for(int j=7; j>=0; j--)
66                         {
67                                 if(data[i] & (0x01<<j)) str += "1";
68                                 else                                    str += "0";
69                         }
70
71                         if(i>0) str += "'";
72                 }
73
74                 return str;
75         }
76
77 #ifdef WIN32
78         std::wstring toWide(const std::string& str);
79         std::string toAnsi(const std::wstring& str);
80 #endif
81
82         // Undoable Out
83         template<typename OstreamType> void undoable_out_clear(OstreamType& out, int n)
84         {
85                 while(n-->0) out << "\b";
86         }
87         template<typename OstreamType> int undoable_out(OstreamType& out, const std::string& s, int n)
88         {
89                 undoable_out_clear(out, n);
90                 std::stringstream str;
91                 str.precision(1);
92                 str << s;
93                 n = str.tellp();
94                 out << str.str();
95                 return n;
96         }
97         template<class OstreamType> int undoable_out_percent(OstreamType& out, float f, int n=0)
98         {
99                 undoable_out_clear(out, n);
100                 std::stringstream str;
101                 str.precision(1);
102                 str << std::fixed << f << "%   " << std::flush;
103                 n = str.tellp();
104                 out << str.str();
105                 return n;
106         }
107
108 /*      //-----------------------------------------------------------------------------
109         // UNICODE support for converting between CHAR, TCHAR, and WCHAR strings
110         //-----------------------------------------------------------------------------
111         void ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
112         void ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
113         void ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, int cchDestChar = -1 );
114         void ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
115 */
116 }
117
118 #endif