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
35namespace 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 */
46std::string
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 */
59std::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/** Convert unsigned int value to a string.
69 * @param i value to convert
70 * @return string representation of value.
71 */
72std::string
73StringConversions::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 */
90std::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 */
107std::string
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 */
124std::string
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 */
141std::string
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 */
158std::string
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 */
172unsigned int
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 */
183int
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 */
193long
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 */
203float
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 */
213double
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 */
223bool
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 */
237void
239{
240 std::string::size_type p1 = s.find_first_not_of(" \n\r\t\f\v");
241 std::string::size_type p2 = s.find_last_not_of(" \n\r\t\f\v");
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 */
251std::string
252StringConversions::trim(const std::string &s)
253{
254 std::string::size_type p1 = s.find_first_not_of(" \n\r\t\f\v");
255 std::string::size_type p2 = s.find_last_not_of(" \n\r\t\f\v");
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 */
264std::string
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 StringConversions::trim(res);
284}
285
286/** Resolves vector of path-string with \@...\@ tags
287 * @param s strings to resolve
288 * @return vector of resolved paths
289 */
290std::vector<std::string>
291StringConversions::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
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
static void trim_inplace(std::string &s)
Trim string.
static std::vector< std::string > resolve_paths(std::vector< std::string > s)
Resolves vector of path-string with @...@ tags.
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
static float to_float(std::string s)
Convert string to a float value.
static double to_double(std::string s)
Convert string to a double value.
static unsigned int to_uint(std::string s)
Convert string to an unsigned int value.
static bool to_bool(std::string s)
Convert string to a bool value.
static long to_long(std::string s)
Convert string to a long int value.
static std::string trim(const std::string &s)
Trim spring.
static std::string to_string(unsigned int i)
Convert unsigned int value to a string.
static int to_int(std::string s)
Convert string to an int value.
static std::string resolve_path(std::string s)
Resolves path-string with @...@ tags.
static std::string to_lower(std::string str)
Convert string to all-lowercase string.
Fawkes library namespace.