Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

String.h

Go to the documentation of this file.
00001 #ifndef Impala_Basis_String_h
00002 #define Impala_Basis_String_h
00003 
00004 #include <string>
00005 #include <algorithm>
00006 #include <iostream>
00007 #include <sstream>
00008 #include <iomanip>
00009 #include <cctype>
00010 #include "Basis/Std.h"
00011 
00012 namespace Impala
00013 {
00014 
00015 
00016 typedef std::string String;
00017 typedef const std::string& CString;
00018 
00019 inline String
00020 MakeString(const int i)
00021 {
00022     std::ostringstream oss;
00023     oss << i;
00024     return oss.str();
00025 }
00026 
00027 inline String
00028 MakeString(const int i, const int width, const char fill = ' ')
00029 {
00030     std::ostringstream oss;
00031     oss << std::setw(width) << std::setfill(fill) << i;
00032     return oss.str();
00033 }
00034 
00035 inline String
00036 MakeString(const long i)
00037 {
00038     std::ostringstream oss;
00039     oss << i;
00040     return oss.str();
00041 }
00042 
00043 inline String
00044 MakeString(const long i, const int width, const char fill = ' ')
00045 {
00046     std::ostringstream oss;
00047     oss << std::setw(width) << std::setfill(fill) << i;
00048     return oss.str();
00049 }
00050 
00051 inline String
00052 MakeString(const unsigned long ul)
00053 {
00054     std::ostringstream oss;
00055     oss << ul;
00056     return oss.str();
00057 }
00058 
00059 inline String
00060 MakeString(const unsigned long ul, const int width, const char fill = ' ')
00061 {
00062     std::ostringstream oss;
00063     oss << std::setw(width) << std::setfill(fill) << ul;
00064     return oss.str();
00065 }
00066 
00067 inline String
00068 MakeString(const long long i)
00069 {
00070     std::ostringstream oss;
00071     oss << i;
00072     return oss.str();
00073 }
00074 
00075 inline String
00076 MakeString(const long long i, int width, char fill = ' ')
00077 {
00078     std::ostringstream oss;
00079     oss << std::setw(width) << std::setfill(fill) << i;
00080     return oss.str();
00081 }
00082 
00083 inline String
00084 MakeString(const unsigned long long ull)
00085 {
00086     std::ostringstream oss;
00087     oss << ull;
00088     return oss.str();
00089 }
00090 
00091 inline String
00092 MakeString(const unsigned long long ull, int width, char fill = ' ')
00093 {
00094     std::ostringstream oss;
00095     oss << std::setw(width) << std::setfill(fill) << ull;
00096     return oss.str();
00097 }
00098 
00099 inline String
00100 MakeString(const double d)
00101 {
00102     std::ostringstream oss;
00103     oss << d;
00104     return oss.str();
00105 }
00106 
00107 inline String
00108 MakeString(const double d, const int width, const char fill = ' ')
00109 {
00110     std::ostringstream oss;
00111     oss << std::setw(width) << std::setfill(fill) << d;
00112     return oss.str();
00113 }
00114 
00115 inline String
00116 MakeString(CString s)
00117 {
00118     return s;
00119 }
00120 
00121 inline String
00122 MakeString(CString src, const int width, const char fill = ' ')
00123 {
00124     if (src.length() >= width)
00125         return src.substr(0, width);
00126     String res = src;
00127     for (int i=src.length() ; i<width ; i++)
00128         res.push_back(fill);
00129     return res;
00130 }
00131 
00133 inline int
00134 atoi(CString s)
00135 {
00136     std::istringstream iss(s);
00137     int i = 0;
00138     iss >> i;
00139     return i;
00140 
00141 }
00142 
00144 inline long
00145 atol(CString s)
00146 {
00147     std::istringstream iss(s);
00148     long i = 0;
00149     iss >> i;
00150     return i;
00151 }
00152 
00154 inline long
00155 atoul(CString s)
00156 {
00157     std::istringstream iss(s);
00158     long i = 0;
00159     iss >> i;
00160     return i;
00161 }
00162 
00164 inline long long
00165 atoll(CString s)
00166 {
00167     std::istringstream iss(s);
00168     long long i = 0;
00169     iss >> i;
00170     return i;
00171 }
00172 
00174 inline unsigned long long
00175 atoull(CString s)
00176 {
00177     std::istringstream iss(s);
00178     unsigned long long i = 0;
00179     iss >> i;
00180     return i;
00181 }
00182 
00184 inline double
00185 atof(CString s)
00186 {
00187     std::istringstream iss(s);
00188     double d = 0.0;
00189     iss >> d;
00190     return d;
00191 }
00192 
00194 inline String
00195 StringReplace(String src, CString subFrom,
00196               CString subTo)
00197 {
00198     String::size_type start = src.find(subFrom);
00199     if (start == String::npos)
00200         return src;
00201     return src.replace(start, subFrom.size(), subTo);
00202 }
00203 
00205 inline String
00206 StringReplaceAll(String src, CString subFrom,
00207                  CString subTo, const bool recursive = true)
00208 {
00209     String::size_type subFromSize = subFrom.size();
00210     String::size_type subToSize = subTo.size();
00211     String::size_type start = src.find(subFrom);
00212     while (start != String::npos)
00213     {
00214         src = src.replace(start, subFromSize, subTo);
00215         start = src.find(subFrom, recursive ? 0 : start + subToSize);
00216     }
00217     return src;
00218 }
00219 
00220 inline String
00221 StringHead(CString src, const char toChar, const bool includeChar)
00222 {
00223     String::size_type p = src.find(toChar, 0);
00224     if (p == String::npos)
00225         return "";
00226     if (includeChar)
00227         p++;
00228     return src.substr(0, p);
00229 }
00230 
00231 inline String
00232 StringTail(String src, char toChar, bool includeChar)
00233 {
00234     String::size_type p = src.find(toChar, 0);
00235     if (p == String::npos)
00236         return "";
00237     if (!includeChar)
00238         p++;
00239     return src.substr(p, src.size() - p);
00240 }
00241 
00242 inline bool
00243 StringStartsWith(CString src, CString sub)
00244 {
00245     return src.compare(0, sub.size(), sub) == 0;
00246 }
00247 
00248 inline bool
00249 StringEndsWith(String src, const String& sub)
00250 {
00251     return src.compare(src.size() - sub.size(), sub.size(), sub) == 0;
00252 }
00253 
00254 inline void
00255 StringToLower(String& s)
00256 {
00257     //String s = src;
00258     // 2006-07-03 Ork - fixed header and function for GCC compilation compliance.
00259     std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
00260     //return r;
00261 }
00262 
00263 inline bool
00264 StringToBool(CString s)
00265 {
00266     return ((s == "0") || (s == "false") || (s == "no")) ? false : true;
00267 }
00268 
00269 } // namespace Impala
00270 
00271 #endif

Generated on Fri Mar 19 09:30:43 2010 for ImpalaSrc by  doxygen 1.5.1