cartobase  5.0.5
string_conversion.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef CARTOBASE_TYPE_STRING_CONVERSION_H
35 #define CARTOBASE_TYPE_STRING_CONVERSION_H
36 
39 #include <string>
40 #include <vector>
41 #include <set>
42 
43 namespace carto
44 {
45 
46 
48 // toString< T > //
50 
51 template < typename T >
52 inline
53 std::string toString( const T& object )
54 {
55 
56  std::ostringstream stream;
57  stream << object;
58  return stream.str();
59 
60 }
61 
62 
64 // toString< char > //
66 
67 template <>
68 inline std::string toString( const char& object )
69 {
70  return toString<int>( object );
71 }
72 
73 
78 std::vector< std::string > split( const std::string &text,
79  const std::string &sep );
80 
81 std::vector< std::string > split( const std::string &text,
82  const std::set<std::string> &sep );
83 
88 std::string join( const std::vector< std::string > &pieces,
89  const std::string &sep );
90 
91 std::string stringLower( const std::string & );
92 std::string stringUpper( const std::string & );
93 
97 std::string stringStrip(const std::string& str, char c = ' ');
98 
100 bool isInt( const std::string & s );
101 
103 bool isFloat( const std::string & s );
104 
106 // toString< unsigned char > //
108 
109 template <>
110 inline std::string toString( const unsigned char& object )
111 {
112  return toString<int>( object );
113 }
114 
115 
117 // toString< signed char > //
119 
120 template <>
121 inline std::string toString( const signed char& object )
122 {
123  return toString<int>( object );
124 }
125 
126 
127 
129 // stringTo< T > //
131 
132 template < typename T >
133 void stringTo( const std::string& value, T& result );
134 
135 // default implementation
136 template < typename T >
137 inline
138 void stringTo( const std::string& value, T& result )
139 {
140 
141 #if __GNUC__ == 2 && __GNUC_MINOR__ - 0 <= 95
142 
143  std::istringstream stream( value.c_str() );
144 
145 #else
146 
147  std::istringstream stream( value );
148 
149 #endif
150  stream >> result;
151 
152 }
153 
154 
156 // stringTo< string > //
158 
159 template <>
160 inline
161 void stringTo< std::string >( const std::string& value, std::string &result )
162 {
163 
164  result = value;
165 
166 }
167 
168 
170 // stringTo< long > //
172 
173 template <>
174 void stringTo< long >( const std::string& value,
175  long& result );
176 
177 
179 // stringTo< unsigned long > //
181 
182 template <>
183 void stringTo< unsigned long >( const std::string& value,
184  unsigned long& result );
185 
186 
188 // stringTo< int > //
190 
191 template <>
192 void stringTo< int >( const std::string& value, int& result );
193 
194 
196 // stringTo< unsigned int > //
198 
199 template <>
200 void stringTo< unsigned int >( const std::string& value,
201  unsigned int& result );
202 
203 
205 // stringTo< unsigned char > //
207 
208 template <>
209 void stringTo< unsigned char >( const std::string& value,
210  unsigned char& result );
211 
212 
214 // stringTo< signed char > //
216 
217 template <>
218 void stringTo< signed char >( const std::string& value,
219  signed char& result );
220 
221 
223 // stringTo< char > //
225 
226 template <>
227 inline
228 void stringTo< char >( const std::string& value, char& result )
229 {
230 
231 #ifdef __CHAR_UNSIGNED__
232 
234  reinterpret_cast< unsigned char& >( result ) );
235 
236 #else
237 
238  stringTo< signed char >( value, reinterpret_cast< signed char& >( result ) );
239 
240 #endif
241 
242 }
243 
244 
246 // stringTo< unsigned short > //
248 
249 template <>
250 void stringTo< unsigned short >( const std::string& value,
251  unsigned short& result );
252 
253 
255 // stringTo< short > //
257 
258 template <>
259 void stringTo< short >( const std::string& value,
260  short& result );
261 
262 
264 // stringTo< double > //
266 
267 template <>
268 void stringTo< double >( const std::string& value,
269  double& result );
270 
271 
273 // stringTo< float > //
275 
276 template <>
277 void stringTo< float >( const std::string& value, float& result );
278 
279 
281 // stringTo< bool > //
283 
284 template <>
285 void stringTo< bool >( const std::string& value, bool& result );
286 
297 std::string quotedString( const std::string & s, char *quote = 0,
298  bool with_quotes = true );
299 
300 
301 } // namespace carto
302 
303 
304 #endif
void stringTo< unsigned int >(const std::string &value, unsigned int &result)
bool isInt(const std::string &s)
Does the string represent a valid integer ?
void stringTo< bool >(const std::string &value, bool &result)
void stringTo< double >(const std::string &value, double &result)
void stringTo< unsigned long >(const std::string &value, unsigned long &result)
void stringTo< signed char >(const std::string &value, signed char &result)
void stringTo< std::string >(const std::string &value, std::string &result)
std::string stringStrip(const std::string &str, char c=' ')
void stringTo< unsigned short >(const std::string &value, unsigned short &result)
void stringTo< char >(const std::string &value, char &result)
std::string stringUpper(const std::string &)
void stringTo(const std::string &value, T &result)
void stringTo< float >(const std::string &value, float &result)
std::string quotedString(const std::string &s, char *quote=0, bool with_quotes=true)
Quote a string in python style: escape quotes (single and double), depending on a quote character...
bool isFloat(const std::string &s)
Does the string represent a valid float ?
void stringTo< short >(const std::string &value, short &result)
void stringTo< unsigned char >(const std::string &value, unsigned char &result)
std::vector< std::string > split(const std::string &text, const std::string &sep)
void stringTo< long >(const std::string &value, long &result)
std::string toString(const T &object)
void stringTo< int >(const std::string &value, int &result)
std::string join(const std::vector< std::string > &pieces, const std::string &sep)
std::string stringLower(const std::string &)