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 float value to a string. 00106 * @param f value to convert 00107 * @return string representation of value. 00108 */ 00109 std::string 00110 StringConversions::to_string(const float f) 00111 { 00112 char *tmp; 00113 std::string rv; 00114 if (asprintf(&tmp, "%f", f) == -1) { 00115 throw OutOfMemoryException("StringConversions::tostring(const float): asprintf() failed"); 00116 } 00117 rv = tmp; 00118 free(tmp); 00119 return rv; 00120 } 00121 00122 00123 /** Convert double value to a string. 00124 * @param d value to convert 00125 * @return string representation of value. 00126 */ 00127 std::string 00128 StringConversions::to_string(const double d) 00129 { 00130 char *tmp; 00131 std::string rv; 00132 if (asprintf(&tmp, "%f", d) == -1) { 00133 throw OutOfMemoryException("StringConversions::tostring(const double d): asprintf() failed"); 00134 } 00135 rv = tmp; 00136 free(tmp); 00137 return rv; 00138 } 00139 00140 00141 /** Convert bool value to a string. 00142 * @param b value to convert 00143 * @return string representation of value. 00144 */ 00145 std::string 00146 StringConversions::to_string(const bool b) 00147 { 00148 if ( b ) { 00149 return std::string("true"); 00150 } else { 00151 return std::string("false"); 00152 } 00153 } 00154 00155 00156 /** Convert string to an unsigned int value 00157 * @param s string to convert 00158 * @return value as represented by string 00159 */ 00160 unsigned int 00161 StringConversions::to_uint(std::string s) 00162 { 00163 unsigned int l = atoll(s.c_str()); 00164 return l; 00165 } 00166 00167 00168 /** Convert string to an int value 00169 * @param s string to convert 00170 * @return value as represented by string 00171 */ 00172 int 00173 StringConversions::to_int(std::string s) 00174 { 00175 return atoi(s.c_str()); 00176 } 00177 00178 00179 /** Convert string to a float value 00180 * @param s string to convert 00181 * @return value as represented by string 00182 */ 00183 float 00184 StringConversions::to_float(std::string s) 00185 { 00186 return (float)atof(s.c_str()); 00187 } 00188 00189 00190 /** Convert string to a double value 00191 * @param s string to convert 00192 * @return value as represented by string 00193 */ 00194 double 00195 StringConversions::to_double(std::string s) 00196 { 00197 return atof(s.c_str()); 00198 } 00199 00200 00201 /** Convert string to a bool value 00202 * @param s string to convert 00203 * @return value as represented by string 00204 */ 00205 bool 00206 StringConversions::to_bool(std::string s) 00207 { 00208 if ( (s == "true") || 00209 (s == "yes") || 00210 (s == "1") ) { 00211 return true; 00212 } else { 00213 return false; 00214 } 00215 } 00216 00217 /** Trim string. 00218 * Removes spaces at beginning and end of string. 00219 * @param s string to trim, upon return contains trimmed string 00220 */ 00221 void 00222 StringConversions::trim_inplace(std::string &s) 00223 { 00224 std::string::size_type p1 = s.find_first_not_of(' '); 00225 std::string::size_type p2 = s.find_last_not_of(' '); 00226 s = s.substr(p1 == std::string::npos ? 0 : p1, 00227 p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1); 00228 } 00229 00230 00231 /** Trim spring. 00232 * Removes spaces at beginning and end of string. 00233 * @param s string to trim 00234 * @return trimmed string 00235 */ 00236 std::string 00237 StringConversions::trim(std::string &s) 00238 { 00239 std::string::size_type p1 = s.find_first_not_of(' '); 00240 std::string::size_type p2 = s.find_last_not_of(' '); 00241 return s.substr(p1 == std::string::npos ? 0 : p1, 00242 p2 == std::string::npos ? s.length() - 1 : p2 - p1 + 1); 00243 } 00244 00245 00246 } // end namespace fawkes