String.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <cstdio>
00013 #include <cstdarg>
00014 
00015 #include <iostream>
00016 
00017 #include "zypp/base/String.h"
00018 
00019 using std::string;
00020 
00022 namespace zypp
00023 { 
00024 
00025   namespace str
00026   { 
00027 
00028     /******************************************************************
00029      **
00030      **      FUNCTION NAME : form
00031      **      FUNCTION TYPE : std::string
00032     */
00033     std::string form( const char * format, ... )
00034     {
00035       SafeBuf safe;
00036 
00037       va_list ap;
00038       va_start( ap, format );
00039       vasprintf( &safe._buf, format, ap );
00040       va_end( ap );
00041 
00042       return safe.asString();
00043     }
00044 
00045     /******************************************************************
00046      **
00047      **      FUNCTION NAME : strerror
00048      **      FUNCTION TYPE : std::string
00049     */
00050     std::string strerror( int errno_r )
00051     {
00052       return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
00053     }
00054 
00055     /******************************************************************
00056      **
00057      **      FUNCTION NAME : strToTrue
00058      **      FUNCTION TYPE : bool
00059     */
00060     bool strToTrue( const std::string & str )
00061     {
00062       std::string t( toLower( str ) );
00063       return(    t == "1"
00064               || t == "yes"
00065               || t == "true"
00066               || t == "on"
00067             );
00068     }
00069 
00070     /******************************************************************
00071      **
00072      **      FUNCTION NAME : strToFalse
00073      **      FUNCTION TYPE : bool
00074     */
00075     bool strToFalse( const std::string & str )
00076     {
00077       std::string t( toLower( str ) );
00078       return ! (    t == "0"
00079                  || t == "no"
00080                  || t == "false"
00081                  || t == "off"
00082                );
00083     }
00084 
00085     /******************************************************************
00086      **
00087      **      FUNCTION NAME : toLower
00088      **      FUNCTION TYPE : std::string
00089     */
00090     std::string toLower( const std::string & s )
00091     {
00092       if ( s.empty() )
00093         return s;
00094 
00095       std::string ret( s );
00096       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00097         {
00098           if ( isupper( ret[i] ) )
00099             ret[i] = static_cast<char>(tolower( ret[i] ));
00100         }
00101       return ret;
00102     }
00103 
00104     /******************************************************************
00105      **
00106      **      FUNCTION NAME : toUpper
00107      **      FUNCTION TYPE : std::string
00108     */
00109     std::string toUpper( const std::string & s )
00110     {
00111       if ( s.empty() )
00112         return s;
00113 
00114       std::string ret( s );
00115       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00116         {
00117           if ( islower( ret[i] ) )
00118             ret[i] = static_cast<char>(toupper( ret[i] ));
00119         }
00120       return ret;
00121     }
00122 
00123     /******************************************************************
00124      **
00125      **      FUNCTION NAME : trim
00126      **      FUNCTION TYPE : std::string
00127     */
00128     std::string trim( const std::string & s, const Trim trim_r )
00129     {
00130       if ( s.empty() || trim_r == NO_TRIM )
00131         return s;
00132 
00133       std::string ret( s );
00134 
00135       if ( trim_r && L_TRIM )
00136         {
00137           std::string::size_type p = ret.find_first_not_of( " \t\n" );
00138           if ( p == std::string::npos )
00139             return std::string();
00140 
00141           ret = ret.substr( p );
00142         }
00143 
00144       if ( trim_r && R_TRIM )
00145         {
00146           std::string::size_type p = ret.find_last_not_of( " \t\n" );
00147           if ( p == std::string::npos )
00148             return std::string();
00149 
00150           ret = ret.substr( 0, p+1 );
00151         }
00152 
00153       return ret;
00154     }
00155     /******************************************************************
00156     **
00157     **  FUNCTION NAME : stripFirstWord
00158     **  FUNCTION TYPE : std::string
00159     **
00160     **  DESCRIPTION :
00161     */
00162     std::string stripFirstWord( std::string & line, const bool ltrim_first )
00163     {
00164       if ( ltrim_first )
00165         line = ltrim( line );
00166 
00167       if ( line.empty() )
00168         return line;
00169 
00170       std::string ret;
00171       std::string::size_type p = line.find_first_of( " \t" );
00172 
00173       if ( p == std::string::npos ) {
00174         // no ws on line
00175         ret = line;
00176         line.erase();
00177       } else if ( p == 0 ) {
00178         // starts with ws
00179         // ret remains empty
00180         line = ltrim( line );
00181       }
00182       else {
00183         // strip word and ltim line
00184         ret = line.substr( 0, p );
00185         line = ltrim( line.erase( 0, p ) );
00186       }
00187       return ret;
00188     }
00189 
00190     string gsub(const string& sData, const string& sFrom, const string& sTo)
00191     {
00192       string sNew = sData;
00193 
00194       if (! sNew.empty())
00195       {
00196         string::size_type toLen = sTo.length();
00197         string::size_type frLen = sFrom.length();
00198         string::size_type loc = 0;
00199 
00200         while (string::npos != (loc = sNew.find(sFrom, loc)))
00201         {
00202           sNew.replace(loc, frLen, sTo);
00203           loc += toLen;
00204 
00205           if (loc >= sNew.length())
00206           break;
00207         }
00208       }
00209 
00210       return sNew;
00211     }
00212 
00213     /******************************************************************
00214     **
00215     **
00216     **      FUNCTION NAME : getline
00217     **      FUNCTION TYPE : std::string
00218     **
00219     **      DESCRIPTION :
00220     */
00221     static inline std::string _getline( std::istream & str, const Trim trim_r )
00222     {
00223       const unsigned tmpBuffLen = 1024;
00224       char           tmpBuff[tmpBuffLen];
00225 
00226       std::string ret;
00227       do {
00228         str.clear();
00229         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
00230         ret += tmpBuff;
00231       } while( str.rdstate() == std::ios::failbit );
00232 
00233       return trim( ret, trim_r );
00234     }
00235 
00236     std::string getline( std::istream & str, const Trim trim_r )
00237     {
00238       return _getline(str, trim_r);
00239     }
00240 
00241     std::string getline( std::istream & str, bool trim )
00242     {
00243       return _getline(str, trim?TRIM:NO_TRIM);
00244     }
00245 
00247   } // namespace str
00250 } // namespace zypp

Generated on Tue Sep 25 19:22:59 2007 for libzypp by  doxygen 1.5.3