Fawkes API  Fawkes Development Version
string_conversions.cpp
1 
2 /***************************************************************************
3  * string_conversions.cpp - string conversions
4  *
5  * Created: Thu Oct 12 12:05:42 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/misc/string_conversions.h>
25 #include <core/exceptions/system.h>
26 
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <map>
34 
35 namespace fawkes {
36 
37 /** @class StringConversions <utils/misc/string_conversions.h>
38  * Utility class that holds string methods.
39  * @author Tim Niemueller
40  *
41  * @fn static std::string StringConversions::to_string(std::string &s)
42  * No-op conversion of string.
43  * @param s value to convert
44  * @return string the very same string
45  */
46 
47 /** Convert string to all-uppercase string.
48  * @param str string to convert
49  * @return converted string
50  */
51 std::string
53 {
54  for(unsigned int i = 0; i < str.length(); ++i) {
55  str[i] = (char)toupper(str[i]);
56  }
57  return str;
58 }
59 
60 
61 /** Convert string to all-lowercase string.
62  * @param str string to convert
63  * @return converted string
64  */
65 std::string
67 {
68  for(unsigned int i = 0; i < str.length(); ++i) {
69  str[i] = (char)tolower(str[i]);
70  }
71  return str;
72 }
73 
74 
75 /** Convert unsigned int value to a string.
76  * @param i value to convert
77  * @return string representation of value.
78  */
79 std::string
80 StringConversions::to_string(const unsigned int i)
81 {
82  char *tmp;
83  std::string rv;
84  if (asprintf(&tmp, "%u", i) == -1) {
85  throw OutOfMemoryException("StringConversions::tostring(const unsigned int): asprintf() failed");
86  }
87  rv = tmp;
88  free(tmp);
89  return rv;
90 }
91 
92 
93 /** Convert int value to a string.
94  * @param i value to convert
95  * @return string representation of value.
96  */
97 std::string
99 {
100  char *tmp;
101  std::string rv;
102  if (asprintf(&tmp, "%i", i) == -1) {
103  throw OutOfMemoryException("StringConversions::tostring(const int): asprintf() failed");
104  }
105  rv = tmp;
106  free(tmp);
107  return rv;
108 }
109 
110 
111 /** Convert long int value to a string.
112  * @param i value to convert
113  * @return string representation of value.
114  */
115 std::string
117 {
118  char *tmp;
119  std::string rv;
120  if (asprintf(&tmp, "%li", i) == -1) {
121  throw OutOfMemoryException("StringConversions::tostring(const long int): asprintf() failed");
122  }
123  rv = tmp;
124  free(tmp);
125  return rv;
126 }
127 
128 
129 /** Convert float value to a string.
130  * @param f value to convert
131  * @return string representation of value.
132  */
133 std::string
135 {
136  char *tmp;
137  std::string rv;
138  if (asprintf(&tmp, "%f", f) == -1) {
139  throw OutOfMemoryException("StringConversions::tostring(const float): asprintf() failed");
140  }
141  rv = tmp;
142  free(tmp);
143  return rv;
144 }
145 
146 
147 /** Convert double value to a string.
148  * @param d value to convert
149  * @return string representation of value.
150  */
151 std::string
153 {
154  char *tmp;
155  std::string rv;
156  if (asprintf(&tmp, "%f", d) == -1) {
157  throw OutOfMemoryException("StringConversions::tostring(const double d): asprintf() failed");
158  }
159  rv = tmp;
160  free(tmp);
161  return rv;
162 }
163 
164 
165 /** Convert bool value to a string.
166  * @param b value to convert
167  * @return string representation of value.
168  */
169 std::string
171 {
172  if ( b ) {
173  return std::string("true");
174  } else {
175  return std::string("false");
176  }
177 }
178 
179 
180 /** Convert string to an unsigned int value
181  * @param s string to convert
182  * @return value as represented by string
183  */
184 unsigned int
186 {
187  unsigned int l = atoll(s.c_str());
188  return l;
189 }
190 
191 
192 /** Convert string to an int value
193  * @param s string to convert
194  * @return value as represented by string
195  */
196 int
198 {
199  return atoi(s.c_str());
200 }
201 
202 /** Convert string to a long int value
203  * @param s string to convert
204  * @return value as represented by string
205  */
206 long
208 {
209  return atol(s.c_str());
210 }
211 
212 
213 /** Convert string to a float value
214  * @param s string to convert
215  * @return value as represented by string
216  */
217 float
219 {
220  return (float)atof(s.c_str());
221 }
222 
223 
224 /** Convert string to a double value
225  * @param s string to convert
226  * @return value as represented by string
227  */
228 double
230 {
231  return atof(s.c_str());
232 }
233 
234 
235 /** Convert string to a bool value
236  * @param s string to convert
237  * @return value as represented by string
238  */
239 bool
241 {
242  if ( (s == "true") ||
243  (s == "yes") ||
244  (s == "1") ) {
245  return true;
246  } else {
247  return false;
248  }
249 }
250 
251 /** Trim string.
252  * Removes spaces at beginning and end of string.
253  * @param s string to trim, upon return contains trimmed string
254  */
255 void
257 {
258  std::string::size_type p1 = s.find_first_not_of(' ');
259  std::string::size_type p2 = s.find_last_not_of(' ');
260  s = s.substr(p1 == std::string::npos ? 0 : p1,
261  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
262 }
263 
264 
265 /** Trim spring.
266  * Removes spaces at beginning and end of string.
267  * @param s string to trim
268  * @return trimmed string
269  */
270 std::string
271 StringConversions::trim(std::string &s)
272 {
273  std::string::size_type p1 = s.find_first_not_of(' ');
274  std::string::size_type p2 = s.find_last_not_of(' ');
275  return s.substr(p1 == std::string::npos ? 0 : p1,
276  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
277 }
278 
279 /** Resolves path-string with \@...\@ tags
280  * @param s string to resolve
281  * @return path
282  */
283 std::string
285 {
286  std::map<std::string, std::string> resolve_map;
287  resolve_map["@BASEDIR@"] = BASEDIR;
288  resolve_map["@RESDIR@"] = RESDIR;
289  resolve_map["@CONFDIR@"] = CONFDIR;
290  resolve_map["@SRCDIR@"] = SRCDIR;
291  resolve_map["@FAWKES_BASEDIR@"] = FAWKES_BASEDIR;
292  std::string res = s;
293  for(std::map<std::string, std::string>::iterator it = resolve_map.begin(); it != resolve_map.end(); it++)
294  {
295  std::size_t start_pos = res.find(it->first);
296  if(start_pos != std::string::npos)
297  {
298  res.replace(start_pos, it->first.size(), it->second);
299  }
300  }
301  return res;
302 }
303 
304 /** Resolves vector of path-string with \@...\@ tags
305  * @param s strings to resolve
306  * @return vector of resolved paths
307  */
308 std::vector<std::string>
309 StringConversions::resolve_paths(std::vector<std::string> s)
310 {
311  std::vector<std::string> res = std::vector<std::string>(s.size());
312  for(unsigned int i = 0; i < s.size(); i++)
313  {
314  res[i] = resolve_path(s[i]);
315  }
316  return res;
317 }
318 
319 
320 } // end namespace fawkes
static std::string resolve_path(std::string s)
Resolves path-string with @...@ tags.
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
Fawkes library namespace.
static std::string to_lower(std::string str)
Convert string to all-lowercase string.
static std::vector< std::string > resolve_paths(std::vector< std::string > s)
Resolves vector of path-string with @...@ tags.
static void trim_inplace(std::string &s)
Trim string.
static int to_int(std::string s)
Convert string to an int value.
static std::string trim(std::string &s)
Trim spring.
static long to_long(std::string s)
Convert string to a long int value.
static float to_float(std::string s)
Convert string to a float value.
static bool to_bool(std::string s)
Convert string to a bool value.
static double to_double(std::string s)
Convert string to a double value.
static std::string to_string(unsigned int i)
Convert unsigned int value to a string.
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
static unsigned int to_uint(std::string s)
Convert string to an unsigned int value.