Debian Codegraph @

libs/CppAddons/StringAddons.h

raw
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 inline std::string toString(int i)
34 {
35 std::stringstream str;
36 str << i;
37 return str.str();
38 }
39 template<typename Type> std::string toString(Type i)
40 {
41 std::stringstream str;
42 str << i;
43 return str.str();
44 }
45 template<typename Type> void toString(std::string& s, Type i)
46 {
47 std::stringstream str;
48 str << i;
49 s = str.str();
50 }
51
52 inline std::string ereaseEmptyChars(const std::string& str)
53 {
54 std::string result;
55
56 for(unsigned int i=0; i<str.length(); i++)
57 if(!isspace(str[i]))
58 result += str[i];
59
60 return result;
61 }
62
63 template<typename T> inline std::string binValue(T& d)
64 {
65 char* data = (char*)&d;
66
67 std::string str;
68
69 for(int i=sizeof(T)-1; i>=0; i--)
70 {
71 for(int j=7; j>=0; j--)
72 {
73 if(data[i] & (0x01<<j)) str += "1";
74 else str += "0";
75 }
76
77 if(i>0) str += "'";
78 }
79
80 return str;
81 }
82
83 #ifdef WIN32
84 std::wstring toWide(const std::string& str);
85 std::string toAnsi(const std::wstring& str);
86 #endif
87
88 // Undoable Out
89 template<typename OstreamType> void undoable_out_clear(OstreamType& out, int n)
90 {
91 while(n-->0) out << "\b";
92 }
93 template<typename OstreamType> int undoable_out(OstreamType& out, const std::string& s, int n)
94 {
95 undoable_out_clear(out, n);
96 std::stringstream str;
97 str.precision(1);
98 str << s;
99 n = str.tellp();
100 out << str.str();
101 return n;
102 }
103 template<class OstreamType> int undoable_out_percent(OstreamType& out, float f, int n=0)
104 {
105 undoable_out_clear(out, n);
106 std::stringstream str;
107 str.precision(1);
108 str << std::fixed << f << "% " << std::flush;
109 n = str.tellp();
110 out << str.str();
111 return n;
112 }
113
114 /* //-----------------------------------------------------------------------------
115 // UNICODE support for converting between CHAR, TCHAR, and WCHAR strings
116 //-----------------------------------------------------------------------------
117 void ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
118 void ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
119 void ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, int cchDestChar = -1 );
120 void ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
121 */
122 }
123
124 #endif