Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
34 namespace fawkes {
35 
36 /** @class StringConversions <utils/misc/string_conversions.h>
37  * Utility class that holds string methods.
38  * @author Tim Niemueller
39  */
40 
41 /** Convert string to all-uppercase string.
42  * @param str string to convert
43  * @return converted string
44  */
45 std::string
47 {
48  for(unsigned int i = 0; i < str.length(); ++i) {
49  str[i] = (char)toupper(str[i]);
50  }
51  return str;
52 }
53 
54 
55 /** Convert string to all-lowercase string.
56  * @param str string to convert
57  * @return converted string
58  */
59 std::string
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 
69 /** Convert unsigned int value to a string.
70  * @param i value to convert
71  * @return string representation of value.
72  */
73 std::string
74 StringConversions::to_string(const unsigned int i)
75 {
76  char *tmp;
77  std::string rv;
78  if (asprintf(&tmp, "%u", i) == -1) {
79  throw OutOfMemoryException("StringConversions::tostring(const unsigned int): asprintf() failed");
80  }
81  rv = tmp;
82  free(tmp);
83  return rv;
84 }
85 
86 
87 /** Convert int value to a string.
88  * @param i value to convert
89  * @return string representation of value.
90  */
91 std::string
93 {
94  char *tmp;
95  std::string rv;
96  if (asprintf(&tmp, "%i", i) == -1) {
97  throw OutOfMemoryException("StringConversions::tostring(const int): asprintf() failed");
98  }
99  rv = tmp;
100  free(tmp);
101  return rv;
102 }
103 
104 
105 /** Convert long int value to a string.
106  * @param i value to convert
107  * @return string representation of value.
108  */
109 std::string
111 {
112  char *tmp;
113  std::string rv;
114  if (asprintf(&tmp, "%li", i) == -1) {
115  throw OutOfMemoryException("StringConversions::tostring(const long int): asprintf() failed");
116  }
117  rv = tmp;
118  free(tmp);
119  return rv;
120 }
121 
122 
123 /** Convert float value to a string.
124  * @param f value to convert
125  * @return string representation of value.
126  */
127 std::string
129 {
130  char *tmp;
131  std::string rv;
132  if (asprintf(&tmp, "%f", f) == -1) {
133  throw OutOfMemoryException("StringConversions::tostring(const float): asprintf() failed");
134  }
135  rv = tmp;
136  free(tmp);
137  return rv;
138 }
139 
140 
141 /** Convert double value to a string.
142  * @param d value to convert
143  * @return string representation of value.
144  */
145 std::string
147 {
148  char *tmp;
149  std::string rv;
150  if (asprintf(&tmp, "%f", d) == -1) {
151  throw OutOfMemoryException("StringConversions::tostring(const double d): asprintf() failed");
152  }
153  rv = tmp;
154  free(tmp);
155  return rv;
156 }
157 
158 
159 /** Convert bool value to a string.
160  * @param b value to convert
161  * @return string representation of value.
162  */
163 std::string
165 {
166  if ( b ) {
167  return std::string("true");
168  } else {
169  return std::string("false");
170  }
171 }
172 
173 
174 /** Convert string to an unsigned int value
175  * @param s string to convert
176  * @return value as represented by string
177  */
178 unsigned int
180 {
181  unsigned int l = atoll(s.c_str());
182  return l;
183 }
184 
185 
186 /** Convert string to an int value
187  * @param s string to convert
188  * @return value as represented by string
189  */
190 int
192 {
193  return atoi(s.c_str());
194 }
195 
196 
197 /** Convert string to a float value
198  * @param s string to convert
199  * @return value as represented by string
200  */
201 float
203 {
204  return (float)atof(s.c_str());
205 }
206 
207 
208 /** Convert string to a double value
209  * @param s string to convert
210  * @return value as represented by string
211  */
212 double
214 {
215  return atof(s.c_str());
216 }
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
225 {
226  if ( (s == "true") ||
227  (s == "yes") ||
228  (s == "1") ) {
229  return true;
230  } else {
231  return false;
232  }
233 }
234 
235 /** Trim string.
236  * Removes spaces at beginning and end of string.
237  * @param s string to trim, upon return contains trimmed string
238  */
239 void
241 {
242  std::string::size_type p1 = s.find_first_not_of(' ');
243  std::string::size_type p2 = s.find_last_not_of(' ');
244  s = s.substr(p1 == std::string::npos ? 0 : p1,
245  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
246 }
247 
248 
249 /** Trim spring.
250  * Removes spaces at beginning and end of string.
251  * @param s string to trim
252  * @return trimmed string
253  */
254 std::string
255 StringConversions::trim(std::string &s)
256 {
257  std::string::size_type p1 = s.find_first_not_of(' ');
258  std::string::size_type p2 = s.find_last_not_of(' ');
259  return s.substr(p1 == std::string::npos ? 0 : p1,
260  p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1);
261 }
262 
263 
264 } // end namespace fawkes
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
static std::string to_lower(std::string str)
Convert string to all-lowercase string.
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 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.