Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields  

base.StringTools.h

00001 /*----------------------------------------------------------------------------
00002   SWORD 2000 - Software With Objects for Rapid Development
00003   Copyright (C) 2003 Eric NICOLAS
00004   ----------------------------------------------------------------------------
00005   SWORD is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU Lesser General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   SWORD is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013   GNU Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public License
00016   along with SWORD; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018   --------------------------------------------------------------------------*/
00019 #ifndef _SWORD_BASE_SRINGTOOLS_
00020 #define _SWORD_BASE_SRINGTOOLS_
00021 
00022 #include <vector>
00023 
00024 namespace sword {
00025 
00026         // Implemented templates are (with standard 'string' basic_string instance)
00027         //    string &trimLeft (string &str);
00028         //    string &trimRight(string &str);
00029         //    string &trim     (string &str);
00030         //    void strlwr(string &s);
00031         //    void strupr(string &s);
00032         //    string strlwr(const string &s);
00033         //    string strupr(const string &s);
00034         //    void replace (string &s, char          src, char          dst);
00035         //    void replace (string &s, const char *  src, const char *  dst);
00036         //    void replace (string &s, const string &src, const string &dst);
00037         //    void replacem(string &s, const char *  src, const char *  dst);
00038         //    void replacem(string &s, const string &src, const string &dst);
00039         //    void split(const string &str, vector<string> &segments, char separator);
00040   //    std::string join(vector<string> &segments, char separator);
00041         //    vector<string> split(const string &str, char separator);
00042         //    void extractNameValue(const string &line, string &name, string &value);
00043         //    std::string readLine(const std::string& blob, size_t& offset);
00044 
00045         // Other functions
00046         //    int stricmp (const char *s1, const char *s2);
00047         //    int strnicmp(const char *s1, const char *s2, size_t length);
00048         //    int stricmp (const std::string &s1, const std::string &s2);
00049         //    int strnicmp(const std::string &s1, const std::string &s2, size_t length);
00050 
00052         SWORDDECL std::string format(float value);
00053 
00055         SWORDDECL std::string format(double value);
00056 
00058 
00070         SWORDDECL std::string format(double value, word precision, char decimalChar = '.', char thousandChar = '\0', bool negativeInParenthesis = false);
00071 
00072         // internal functions
00073         // - work on the provided 'buffer', which has 'bufferLen' characters
00074         // - returns the address of the built string (which is usually *not* the begining of 'buffer')
00075 
00076         SWORDDECL char *format_(char *buffer, word bufferLen, word64 value, char thousandChar = '\0');
00077         SWORDDECL char *format_(char *buffer, word bufferLen, int64  value, char thousandChar = '\0', bool negativeInParenthesis = false);
00078         SWORDDECL char *format_(char *buffer, word bufferLen, double value, word precision, char decimalChar = '.', char thousandChar = '\0', bool negativeInParenthesis = false);
00079 
00081         SWORDDECL std::string format(void *value);
00082 
00084         SWORDDECL std::string format(char value);
00085 
00087         SWORDDECL std::string format(const char *format, ...);
00088 
00090         SWORDDECL int8 parseInt8(const std::string &str);
00091 
00093         SWORDDECL int16 parseInt16(const std::string &str);
00094 
00096         SWORDDECL int32 parseInt32(const std::string &str);
00097 
00099         SWORDDECL int parseInt(const std::string &str);
00100 
00102         SWORDDECL word8 parseWord8(const std::string &str);
00103 
00105         SWORDDECL word16 parseWord16(const std::string &str);
00106 
00108         SWORDDECL word32 parseWord32(const std::string &str);
00109 
00111         SWORDDECL word parseWord(const std::string &str);
00112 
00114         SWORDDECL float parseFloat(const std::string &str);
00115 
00117         SWORDDECL double parseDouble(const std::string &str);
00118 
00119         // Remove spaces (trim)
00120 
00121         template<class charT, class traits, class Allocator>
00122         inline std::basic_string<charT, traits, Allocator> trimLeft(const std::basic_string<charT, traits, Allocator> &str)
00123         {
00124                 std::string result = str;
00125                 result.erase(0, result.find_first_not_of(" \t\n\r"));
00126                 return result;
00127         }
00128 
00129         template<class charT, class traits, class Allocator>
00130         inline std::basic_string<charT, traits, Allocator> trimRight(const std::basic_string<charT, traits, Allocator> &str)
00131         {
00132                 std::string result = str;
00133                 result.erase(result.find_last_not_of(" \t\n\r") + 1);
00134                 return result;
00135         }
00136 
00137         template<class charT, class traits, class Allocator>
00138         inline std::basic_string<charT, traits, Allocator> trim(const std::basic_string<charT, traits, Allocator> &str)
00139         {
00140                 return trimLeft( trimRight( str ) );
00141         }
00142 
00143         // lower/upper case transform (in place)
00144 
00145         template<class charT, class traits, class Allocator>
00146         void strlwr(std::basic_string<charT, traits, Allocator> &str)
00147         {
00148                 // scan the string
00149                 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00150                         i!=str.end(); ++i)
00151                 {
00152                         // convert current char to lower case
00153                         *i=(char)tolower(*i);
00154                 }
00155         }
00156 
00157         template<class charT, class traits, class Allocator>
00158         void strupr(std::basic_string<charT, traits, Allocator> &str)
00159         {
00160                 // scan the string
00161                 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00162                         i!=str.end(); ++i)
00163                 {
00164                         // convert current char to upper case
00165                         *i=toupper(*i);
00166                 }
00167         }
00168 
00169         // lower/upper case transform (copies)
00170 
00171         template<class charT, class traits, class Allocator>
00172         std::basic_string<charT, traits, Allocator> strlwr(const std::basic_string<charT, traits, Allocator> &str)
00173         {
00174                 std::basic_string<charT, traits, Allocator> target(str);
00175                 strlwr(target);
00176                 return target;
00177         }
00178 
00179         template<class charT, class traits, class Allocator>
00180         std::basic_string<charT, traits, Allocator> strupr(const std::basic_string<charT, traits, Allocator> &str)
00181         {
00182                 std::basic_string<charT, traits, Allocator> target(str);
00183                 strupr(target);
00184                 return target;
00185         }
00186 
00187         // String part replacement
00188 
00189         template<class charT, class traits, class Allocator>
00190         void replace(std::basic_string<charT, traits, Allocator> &str,
00191                 const char *before, const char *after)
00192         {
00193                 // scan the string
00194     size_t pos=str.find(before);
00195                 if (pos!=std::string::npos)
00196                         str=str.substr(0,pos)+after+str.substr(pos+traits::length(before));
00197         }
00198 
00199         template<class charT, class traits, class Allocator>
00200         void replace(std::basic_string<charT, traits, Allocator> &str,
00201                 const std::basic_string<charT, traits, Allocator> &before,
00202                 const std::basic_string<charT, traits, Allocator> &after)
00203         {
00204                 // scan the string
00205     size_t pos=str.find(before);
00206                 if (pos!=std::string::npos)
00207                         str=str.substr(0,pos)+after+str.substr(pos+before.size());
00208         }
00209 
00210         template<class charT, class traits, class Allocator>
00211         void replacem(std::basic_string<charT, traits, Allocator> &str, char before, char after)
00212         {
00213                 // scan the string
00214                 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00215                         i!=str.end(); ++i)
00216                         // if current character equals the 'before' parameter
00217                         if (traits::eq(*i,before))
00218                                 // then change it to 'after'
00219                                 traits::assign(*i,after);
00220         }
00221 
00222         template<class charT, class traits, class Allocator>
00223         void replacem(std::basic_string<charT, traits, Allocator> &str,
00224                 const char *before, const char *after)
00225         {
00226                 // scan the string
00227     size_t pos=str.find(before);
00228                 while(pos!=std::string::npos)
00229                 {
00230                         str = str.substr(0,pos)+after+str.substr(pos+traits::length(before));
00231                         pos = str.find(before, pos+traits::length(after));
00232                 }
00233         }
00234 
00235         template<class charT, class traits, class Allocator>
00236         void replacem(std::basic_string<charT, traits, Allocator> &str,
00237                                         const std::basic_string<charT, traits, Allocator> &before,
00238                                         const std::basic_string<charT, traits, Allocator> &after)
00239         {
00240                 // scan the string
00241     size_t pos=str.find(before);
00242                 while(pos!=std::string::npos)
00243                 {
00244                         str = str.substr(0,pos)+after+str.substr(pos+before.size());
00245                         pos = str.find(before, pos+after.size());
00246                 }
00247         }
00248 
00249         // Other manipulations
00250 
00261         template<class charT, class traits, class Allocator>
00262         void split(const std::basic_string<charT, traits, Allocator> &str,
00263                                 std::vector<std::basic_string<charT, traits, Allocator> > &segments,
00264                                 charT separator)
00265         {
00266                 std::basic_string<charT, traits, Allocator> remain=str;
00267                 std::basic_string<charT, traits, Allocator> segment;
00268                 for(;;)
00269                 {
00270                         size_t pos=remain.find(separator);
00271                         if (pos==std::string::npos) break;
00272                         //
00273                         segment=trim(remain.substr(0, pos));
00274                         remain =remain.substr(pos+1);
00275                         segments.push_back(segment);
00276                 }
00277                 if (remain.size()) segments.push_back(remain);
00278         }
00279 
00289         template<class charT, class traits, class Allocator>
00290         std::vector<std::basic_string<charT, traits, Allocator> >
00291         split(const std::basic_string<charT, traits, Allocator> &str,
00292                                 charT separator)
00293         {
00294                 std::basic_string<charT, traits, Allocator> remain=str;
00295                 std::basic_string<charT, traits, Allocator> segment;
00296                 std::vector<std::basic_string<charT, traits, Allocator> > segments;
00297                 for(;;)
00298                 {
00299                         size_t pos=remain.find(separator);
00300                         if (pos==std::string::npos) break;
00301                         //
00302                         segment=trim(remain.substr(0, pos));
00303                         remain =remain.substr(pos+1);
00304                         segments.push_back(segment);
00305                 }
00306                 if (remain.size()) segments.push_back(remain);
00307 
00308                 return segments;
00309         }
00310 
00317   template<class charT, class traits, class Allocator>
00318   std::basic_string<charT, traits, Allocator> join(const std::vector<std::basic_string<charT, traits, Allocator> > &segments, charT separator)
00319   {
00320     std::basic_ostringstream<charT, traits, Allocator> oss;
00321     bool first = true;
00322     for(
00323       std::vector<std::basic_string<charT, traits, Allocator> >::const_iterator i=segments.begin();
00324       i!=segments.end();
00325       ++i)
00326     {
00327       if (first)
00328         first = false;
00329       else
00330         oss << separator;
00331       oss << *i;
00332     }
00333     return oss.str();
00334   }
00335 
00336         template<class charT, class traits, class Allocator>
00337         void extractNameValue(const std::basic_string<charT, traits, Allocator> &line,
00338                                                         std::basic_string<charT, traits, Allocator> &name,
00339                                                         std::basic_string<charT, traits, Allocator> &value,
00340               char separator = '=')
00341         {
00342                 int i=line.find(separator);
00343                 name =trim(line.substr(0,i));
00344                 value=trim(line.substr(i+1));
00345         }
00346 
00347         SWORDDECL int stricmp (const std::string &s1, const std::string &s2);
00348         SWORDDECL int strnicmp(const std::string &s1, const std::string &s2, size_t length);
00349 
00350         // internal function used in streams.File and network.Url
00351         SWORDDECL void canonize__(const char *src, char *dst);
00352 
00353         SWORDDECL std::string readLine(const std::string& blob, size_t& offset);
00354 
00355 } // namespace sword
00356 
00357 #ifndef HAVE_STRICMP
00358 SWORDDECL int stricmp (const char *s1, const char *s2);
00359 #endif
00360 
00361 #ifndef HAVE_STRNICMP
00362 SWORDDECL int strnicmp(const char *s1, const char *s2, size_t length);
00363 #endif
00364 
00365 #include "sword.private/base.StringTools.inl"
00366 
00367 #endif // _SWORD_BASE_SRINGTOOLS_

Generated on Tue Dec 23 20:08:56 2003 for SWORD by doxygen1.3-rc2