Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef STRING_TOOLS_H
00018 #define STRING_TOOLS_H
00019
00020 #include <string>
00021 #include <sstream>
00022 #include <cstdlib>
00023 #include <vector>
00024 #include "runtime_error.h"
00025
00027
00028
00036 std::string trim(const std::string& s);
00037
00041 std::string trimLeft(const std::string& s);
00042
00046 std::string trimRight(const std::string& s);
00047
00051 std::string strToLower(const std::string& s);
00052
00056 std::string strToUpper(const std::string& s);
00057
00062 template <typename OutputIterator>
00063 OutputIterator readInts(const char* str, OutputIterator out)
00064 {
00065 char* cl = (char*)str;
00066 char* cle = cl;
00067 while (1) {
00068 int id = strtol(cl, &cle, 10);
00069 if (cl == cle)
00070 break;
00071 *out++ = id;
00072 cl = cle;
00073 }
00074 return out;
00075 }
00076
00081 template <typename OutputIterator>
00082 OutputIterator readFloats(const char* str, OutputIterator out)
00083 {
00084 char* cl = (char*)str;
00085 char* cle = cl;
00086 while (1) {
00087 double val = strtod(cl, &cle);
00088 if (cl == cle)
00089 break;
00090 *out++ = val;
00091 cl = cle;
00092 }
00093 return out;
00094 }
00095
00100 std::string formatString(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
00101
00105 int strPrintf(std::string& str, const char* fmt, ...) __attribute__ ((format (printf, 2, 3)));
00106
00110 template<typename T>
00111 void convertString(const std::string& s, T& x, bool failIfLeftoverChars = true)
00112 {
00113 std::istringstream i(s);
00114 char c;
00115 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
00116 throw RuntimeError("%s: Unable to convert %s", __PRETTY_FUNCTION__, s.c_str());
00117 }
00118
00123 template<typename T>
00124 T stringToType(const std::string& s, bool failIfLeftoverChars = true)
00125 {
00126 T x;
00127 convertString(s, x, failIfLeftoverChars);
00128 return x;
00129 }
00130
00134 bool strStartsWith(const std::string & str, const std::string& substr);
00135
00139 bool strEndsWith(const std::string & str, const std::string& substr);
00140
00145 std::string strExpandFilename(const std::string& filename);
00146
00150 std::vector<std::string> strSplit(const std::string& s, const std::string& delim);
00151
00152
00153
00154 #endif
00155