Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * string_conversions.cpp - string conversions 00004 * 00005 * Created: Thu Oct 12 12:05:42 2006 00006 * Copyright 2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <utils/misc/string_conversions.h> 00025 #include <core/exceptions/system.h> 00026 00027 #ifndef _GNU_SOURCE 00028 #define _GNU_SOURCE 00029 #endif 00030 00031 #include <cstdio> 00032 #include <cstdlib> 00033 00034 namespace fawkes { 00035 00036 /** @class StringConversions <utils/misc/string_conversions.h> 00037 * Utility class that holds string methods. 00038 * @author Tim Niemueller 00039 */ 00040 00041 /** Convert string to all-uppercase string. 00042 * @param str string to convert 00043 * @return converted string 00044 */ 00045 std::string 00046 StringConversions::to_upper(std::string str) 00047 { 00048 for(unsigned int i = 0; i < str.length(); ++i) { 00049 str[i] = (char)toupper(str[i]); 00050 } 00051 return str; 00052 } 00053 00054 00055 /** Convert string to all-lowercase string. 00056 * @param str string to convert 00057 * @return converted string 00058 */ 00059 std::string 00060 StringConversions::to_lower(std::string str) 00061 { 00062 for(unsigned int i = 0; i < str.length(); ++i) { 00063 str[i] = (char)tolower(str[i]); 00064 } 00065 return str; 00066 } 00067 00068 00069 /** Convert unsigned int value to a string. 00070 * @param i value to convert 00071 * @return string representation of value. 00072 */ 00073 std::string 00074 StringConversions::to_string(const unsigned int i) 00075 { 00076 char *tmp; 00077 std::string rv; 00078 if (asprintf(&tmp, "%u", i) == -1) { 00079 throw OutOfMemoryException("StringConversions::tostring(const unsigned int): asprintf() failed"); 00080 } 00081 rv = tmp; 00082 free(tmp); 00083 return rv; 00084 } 00085 00086 00087 /** Convert int value to a string. 00088 * @param i value to convert 00089 * @return string representation of value. 00090 */ 00091 std::string 00092 StringConversions::to_string(const int i) 00093 { 00094 char *tmp; 00095 std::string rv; 00096 if (asprintf(&tmp, "%i", i) == -1) { 00097 throw OutOfMemoryException("StringConversions::tostring(const int): asprintf() failed"); 00098 } 00099 rv = tmp; 00100 free(tmp); 00101 return rv; 00102 } 00103 00104 00105 /** Convert long int value to a string. 00106 * @param i value to convert 00107 * @return string representation of value. 00108 */ 00109 std::string 00110 StringConversions::to_string(const long int i) 00111 { 00112 char *tmp; 00113 std::string rv; 00114 if (asprintf(&tmp, "%li", i) == -1) { 00115 throw OutOfMemoryException("StringConversions::tostring(const long int): asprintf() failed"); 00116 } 00117 rv = tmp; 00118 free(tmp); 00119 return rv; 00120 } 00121 00122 00123 /** Convert float value to a string. 00124 * @param f value to convert 00125 * @return string representation of value. 00126 */ 00127 std::string 00128 StringConversions::to_string(const float f) 00129 { 00130 char *tmp; 00131 std::string rv; 00132 if (asprintf(&tmp, "%f", f) == -1) { 00133 throw OutOfMemoryException("StringConversions::tostring(const float): asprintf() failed"); 00134 } 00135 rv = tmp; 00136 free(tmp); 00137 return rv; 00138 } 00139 00140 00141 /** Convert double value to a string. 00142 * @param d value to convert 00143 * @return string representation of value. 00144 */ 00145 std::string 00146 StringConversions::to_string(const double d) 00147 { 00148 char *tmp; 00149 std::string rv; 00150 if (asprintf(&tmp, "%f", d) == -1) { 00151 throw OutOfMemoryException("StringConversions::tostring(const double d): asprintf() failed"); 00152 } 00153 rv = tmp; 00154 free(tmp); 00155 return rv; 00156 } 00157 00158 00159 /** Convert bool value to a string. 00160 * @param b value to convert 00161 * @return string representation of value. 00162 */ 00163 std::string 00164 StringConversions::to_string(const bool b) 00165 { 00166 if ( b ) { 00167 return std::string("true"); 00168 } else { 00169 return std::string("false"); 00170 } 00171 } 00172 00173 00174 /** Convert string to an unsigned int value 00175 * @param s string to convert 00176 * @return value as represented by string 00177 */ 00178 unsigned int 00179 StringConversions::to_uint(std::string s) 00180 { 00181 unsigned int l = atoll(s.c_str()); 00182 return l; 00183 } 00184 00185 00186 /** Convert string to an int value 00187 * @param s string to convert 00188 * @return value as represented by string 00189 */ 00190 int 00191 StringConversions::to_int(std::string s) 00192 { 00193 return atoi(s.c_str()); 00194 } 00195 00196 00197 /** Convert string to a float value 00198 * @param s string to convert 00199 * @return value as represented by string 00200 */ 00201 float 00202 StringConversions::to_float(std::string s) 00203 { 00204 return (float)atof(s.c_str()); 00205 } 00206 00207 00208 /** Convert string to a double value 00209 * @param s string to convert 00210 * @return value as represented by string 00211 */ 00212 double 00213 StringConversions::to_double(std::string s) 00214 { 00215 return atof(s.c_str()); 00216 } 00217 00218 00219 /** Convert string to a bool value 00220 * @param s string to convert 00221 * @return value as represented by string 00222 */ 00223 bool 00224 StringConversions::to_bool(std::string s) 00225 { 00226 if ( (s == "true") || 00227 (s == "yes") || 00228 (s == "1") ) { 00229 return true; 00230 } else { 00231 return false; 00232 } 00233 } 00234 00235 /** Trim string. 00236 * Removes spaces at beginning and end of string. 00237 * @param s string to trim, upon return contains trimmed string 00238 */ 00239 void 00240 StringConversions::trim_inplace(std::string &s) 00241 { 00242 std::string::size_type p1 = s.find_first_not_of(' '); 00243 std::string::size_type p2 = s.find_last_not_of(' '); 00244 s = s.substr(p1 == std::string::npos ? 0 : p1, 00245 p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1); 00246 } 00247 00248 00249 /** Trim spring. 00250 * Removes spaces at beginning and end of string. 00251 * @param s string to trim 00252 * @return trimmed string 00253 */ 00254 std::string 00255 StringConversions::trim(std::string &s) 00256 { 00257 std::string::size_type p1 = s.find_first_not_of(' '); 00258 std::string::size_type p2 = s.find_last_not_of(' '); 00259 return s.substr(p1 == std::string::npos ? 0 : p1, 00260 p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1); 00261 } 00262 00263 00264 } // end namespace fawkes