cartobase 6.0.6
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
43namespace carto
44{
45
46
48// toString< T > //
50
51template < typename T >
52inline
53std::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
67template <>
68inline std::string toString( const char& object )
69{
70 return toString<int>( object );
71}
72
73
78std::vector< std::string > split( const std::string &text,
79 const std::string &sep );
80
81std::vector< std::string > split( const std::string &text,
82 const std::set<std::string> &sep );
83
88std::string join( const std::vector< std::string > &pieces,
89 const std::string &sep );
90
91std::string stringLower( const std::string & );
92std::string stringUpper( const std::string & );
93
97std::string stringStrip(const std::string& str, char c = ' ');
98
100bool isInt( const std::string & s );
101
103bool isFloat( const std::string & s );
104
106// toString< unsigned char > //
108
109template <>
110inline std::string toString( const unsigned char& object )
111{
112 return toString<int>( object );
113}
114
115
117// toString< signed char > //
119
120template <>
121inline std::string toString( const signed char& object )
122{
123 return toString<int>( object );
124}
125
126
127
129// stringTo< T > //
131
132template < typename T >
133void stringTo( const std::string& value, T& result );
134
135// default implementation
136template < typename T >
137inline
138void 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
159template <>
160inline
161void stringTo< std::string >( const std::string& value, std::string &result )
162{
163
164 result = value;
165
166}
167
168
170// stringTo< long > //
172
173template <>
174void stringTo< long >( const std::string& value,
175 long& result );
176
177
179// stringTo< unsigned long > //
181
182template <>
183void stringTo< unsigned long >( const std::string& value,
184 unsigned long& result );
185
186
188// stringTo< int > //
190
191template <>
192void stringTo< int >( const std::string& value, int& result );
193
194
196// stringTo< unsigned int > //
198
199template <>
200void stringTo< unsigned int >( const std::string& value,
201 unsigned int& result );
202
203
205// stringTo< unsigned char > //
207
208template <>
209void stringTo< unsigned char >( const std::string& value,
210 unsigned char& result );
211
212
214// stringTo< signed char > //
216
217template <>
218void stringTo< signed char >( const std::string& value,
219 signed char& result );
220
221
223// stringTo< char > //
225
226template <>
227inline
228void 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
249template <>
250void stringTo< unsigned short >( const std::string& value,
251 unsigned short& result );
252
253
255// stringTo< short > //
257
258template <>
259void stringTo< short >( const std::string& value,
260 short& result );
261
262
264// stringTo< double > //
266
267template <>
268void stringTo< double >( const std::string& value,
269 double& result );
270
271
273// stringTo< float > //
275
276template <>
277void stringTo< float >( const std::string& value, float& result );
278
279
281// stringTo< bool > //
283
284template <>
285void stringTo< bool >( const std::string& value, bool& result );
286
297std::string quotedString( const std::string & s, char *quote = 0,
298 bool with_quotes = true );
299
300
301} // namespace carto
302
303
304#endif
bool isInt(const std::string &s)
Does the string represent a valid integer ?
void stringTo< signed char >(const std::string &value, signed char &result)
void stringTo< float >(const std::string &value, float &result)
std::string stringStrip(const std::string &str, char c=' ')
std::vector< std::string > split(const std::string &text, const std::string &sep)
void stringTo< unsigned long >(const std::string &value, unsigned long &result)
void stringTo< char >(const std::string &value, char &result)
std::string toString(const T &object)
void stringTo< bool >(const std::string &value, bool &result)
void stringTo< short >(const std::string &value, short &result)
void stringTo< int >(const std::string &value, int &result)
void stringTo< long >(const std::string &value, long &result)
std::string join(const std::vector< std::string > &pieces, const std::string &sep)
void stringTo(const std::string &value, T &result)
void stringTo< double >(const std::string &value, double &result)
bool isFloat(const std::string &s)
Does the string represent a valid float ?
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.
std::string stringUpper(const std::string &)
std::string stringLower(const std::string &)
void stringTo< unsigned int >(const std::string &value, unsigned int &result)
void stringTo< unsigned short >(const std::string &value, unsigned short &result)
void stringTo< unsigned char >(const std::string &value, unsigned char &result)
void stringTo< std::string >(const std::string &value, std::string &result)