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 <core/exceptions/system.h>
25 #include <utils/misc/string_conversions.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 
42 /** Convert string to all-uppercase string.
43  * @param str string to convert
44  * @return converted string
45  */
46 std::string
47 StringConversions::to_upper(std::string str)
48 {
49  for (unsigned int i = 0; i < str.length(); ++i) {
50  str[i] = (char)toupper(str[i]);
51  }
52  return str;
53 }
54 
55 /** Convert string to all-lowercase string.
56  * @param str string to convert
57  * @return converted string
58  */
59 std::string
60 StringConversions::to_lower(std::string str)
61 {
62  for (unsigned int i = 0; i < str.length(); ++i) {
63  str[i] = (char)tolower(str[i]);
64  }
65  return str;
66 }
67 
68 /** Convert unsigned int value to a string.
69  * @param i value to convert
70  * @return string representation of value.
71  */
72 std::string
73 StringConversions::to_string(const unsigned int i)
74 {
75  char * tmp;
76  std::string rv;
77  if (asprintf(&tmp, "%u", i) == -1) {
79  "StringConversions::tostring(const unsigned int): asprintf() failed");
80  }
81  rv = tmp;
82  free(tmp);
83  return rv;
84 }
85 
86 /** Convert int value to a string.
87  * @param i value to convert
88  * @return string representation of value.
89  */
90 std::string
92 {
93  char * tmp;
94  std::string rv;
95  if (asprintf(&tmp, "%i", i) == -1) {
96  throw OutOfMemoryException("StringConversions::tostring(const int): asprintf() failed");
97  }
98  rv = tmp;
99  free(tmp);
100  return rv;
101 }
102 
103 /** Convert long int value to a string.
104  * @param i value to convert
105  * @return string representation of value.
106  */
107 std::string
108 StringConversions::to_string(const long int i)
109 {
110  char * tmp;
111  std::string rv;
112  if (asprintf(&tmp, "%li", i) == -1) {
113  throw OutOfMemoryException("StringConversions::tostring(const long int): asprintf() failed");
114  }
115  rv = tmp;
116  free(tmp);
117  return rv;
118 }
119 
120 /** Convert float value to a string.
121  * @param f value to convert
122  * @return string representation of value.
123  */
124 std::string
125 StringConversions::to_string(const float f)
126 {
127  char * tmp;
128  std::string rv;
129  if (asprintf(&tmp, "%f", f) == -1) {
130  throw OutOfMemoryException("StringConversions::tostring(const float): asprintf() failed");
131  }
132  rv = tmp;
133  free(tmp);
134  return rv;
135 }
136 
137 /** Convert double value to a string.
138  * @param d value to convert
139  * @return string representation of value.
140  */
141 std::string
142 StringConversions::to_string(const double d)
143 {
144  char * tmp;
145  std::string rv;
146  if (asprintf(&tmp, "%f", d) == -1) {
147  throw OutOfMemoryException("StringConversions::tostring(const double d): asprintf() failed");
148  }
149  rv = tmp;
150  free(tmp);
151  return rv;
152 }
153 
154 /** Convert bool value to a string.
155  * @param b value to convert
156  * @return string representation of value.
157  */
158 std::string
159 StringConversions::to_string(const bool b)
160 {
161  if (b) {
162  return std::string("true");
163  } else {
164  return std::string("false");
165  }
166 }
167 
168 /** Convert string to an unsigned int value
169  * @param s string to convert
170  * @return value as represented by string
171  */
172 unsigned int
173 StringConversions::to_uint(std::string s)
174 {
175  unsigned int l = atoll(s.c_str());
176  return l;
177 }
178 
179 /** Convert string to an int value
180  * @param s string to convert
181  * @return value as represented by string
182  */
183 int
184 StringConversions::to_int(std::string s)
185 {
186  return atoi(s.c_str());
187 }
188 
189 /** Convert string to a long int value
190  * @param s string to convert
191  * @return value as represented by string
192  */
193 long
194 StringConversions::to_long(std::string s)
195 {
196  return atol(s.c_str());
197 }
198 
199 /** Convert string to a float value
200  * @param s string to convert
201  * @return value as represented by string
202  */
203 float
204 StringConversions::to_float(std::string s)
205 {
206  return (float)atof(s.c_str());
207 }
208 
209 /** Convert string to a double value
210  * @param s string to convert
211  * @return value as represented by string
212  */
213 double
214 StringConversions::to_double(std::string s)
215 {
216  return atof(s.c_str());
217 }
218 
219 /** Convert string to a bool value
220  * @param s string to convert
221  * @return value as represented by string
222  */
223 bool
224 StringConversions::to_bool(std::string s)
225 {
226  if ((s == "true") || (s == "yes") || (s == "1")) {
227  return true;
228  } else {
229  return false;
230  }
231 }
232 
233 /** Trim string.
234  * Removes spaces at beginning and end of string.
235  * @param s string to trim, upon return contains trimmed string
236  */
237 void
238 StringConversions::trim_inplace(std::string &s)
239 {
240  std::string::size_type p1 = s.find_first_not_of(' ');
241  std::string::size_type p2 = s.find_last_not_of(' ');
242  s = s.substr(p1 == std::string::npos ? 0 : p1,
243  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
244 }
245 
246 /** Trim spring.
247  * Removes spaces at beginning and end of string.
248  * @param s string to trim
249  * @return trimmed string
250  */
251 std::string
252 StringConversions::trim(const std::string &s)
253 {
254  std::string::size_type p1 = s.find_first_not_of(' ');
255  std::string::size_type p2 = s.find_last_not_of(' ');
256  return s.substr(p1 == std::string::npos ? 0 : p1,
257  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
258 }
259 
260 /** Resolves path-string with \@...\@ tags
261  * @param s string to resolve
262  * @return path
263  */
264 std::string
265 StringConversions::resolve_path(std::string s)
266 {
267  std::map<std::string, std::string> resolve_map;
268  resolve_map["@BASEDIR@"] = BASEDIR;
269  resolve_map["@RESDIR@"] = RESDIR;
270  resolve_map["@CONFDIR@"] = CONFDIR;
271  resolve_map["@SRCDIR@"] = SRCDIR;
272  resolve_map["@FAWKES_BASEDIR@"] = FAWKES_BASEDIR;
273  resolve_map["~"] = getenv("HOME");
274  std::string res = s;
275  for (std::map<std::string, std::string>::iterator it = resolve_map.begin();
276  it != resolve_map.end();
277  it++) {
278  std::size_t start_pos = res.find(it->first);
279  if (start_pos != std::string::npos) {
280  res.replace(start_pos, it->first.size(), it->second);
281  }
282  }
283  return res;
284 }
285 
286 /** Resolves vector of path-string with \@...\@ tags
287  * @param s strings to resolve
288  * @return vector of resolved paths
289  */
290 std::vector<std::string>
291 StringConversions::resolve_paths(std::vector<std::string> s)
292 {
293  std::vector<std::string> res = std::vector<std::string>(s.size());
294  for (unsigned int i = 0; i < s.size(); i++) {
295  res[i] = resolve_path(s[i]);
296  }
297  return res;
298 }
299 
300 } // end namespace fawkes
fawkes::StringConversions::trim
static std::string trim(const std::string &s)
Trim spring.
Definition: string_conversions.cpp:251
fawkes::StringConversions::resolve_paths
static std::vector< std::string > resolve_paths(std::vector< std::string > s)
Resolves vector of path-string with @...@ tags.
Definition: string_conversions.cpp:290
fawkes::StringConversions::to_uint
static unsigned int to_uint(std::string s)
Convert string to an unsigned int value.
Definition: string_conversions.cpp:172
fawkes::StringConversions::to_string
static std::string to_string(unsigned int i)
Convert unsigned int value to a string.
Definition: string_conversions.cpp:72
fawkes::StringConversions::resolve_path
static std::string resolve_path(std::string s)
Resolves path-string with @...@ tags.
Definition: string_conversions.cpp:264
fawkes::StringConversions::to_int
static int to_int(std::string s)
Convert string to an int value.
Definition: string_conversions.cpp:183
fawkes::StringConversions::to_lower
static std::string to_lower(std::string str)
Convert string to all-lowercase string.
Definition: string_conversions.cpp:59
fawkes
fawkes::StringConversions::to_bool
static bool to_bool(std::string s)
Convert string to a bool value.
Definition: string_conversions.cpp:223
fawkes::StringConversions::to_long
static long to_long(std::string s)
Convert string to a long int value.
Definition: string_conversions.cpp:193
fawkes::StringConversions::to_upper
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
Definition: string_conversions.cpp:46
fawkes::StringConversions::to_double
static double to_double(std::string s)
Convert string to a double value.
Definition: string_conversions.cpp:213
fawkes::OutOfMemoryException
Definition: system.h:35
fawkes::StringConversions::trim_inplace
static void trim_inplace(std::string &s)
Trim string.
Definition: string_conversions.cpp:237
fawkes::StringConversions::to_float
static float to_float(std::string s)
Convert string to a float value.
Definition: string_conversions.cpp:203