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 
00020 namespace zypp
00021 { 
00022 
00023   namespace str
00024   { 
00025 
00026     /******************************************************************
00027      **
00028      **      FUNCTION NAME : form
00029      **      FUNCTION TYPE : std::string
00030     */
00031     std::string form( const char * format, ... )
00032     {
00033       SafeBuf safe;
00034 
00035       va_list ap;
00036       va_start( ap, format );
00037       vasprintf( &safe._buf, format, ap );
00038       va_end( ap );
00039 
00040       return safe.asString();
00041     }
00042 
00043     /******************************************************************
00044      **
00045      **      FUNCTION NAME : strerror
00046      **      FUNCTION TYPE : std::string
00047     */
00048     std::string strerror( int errno_r )
00049     {
00050       return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
00051     }
00052 
00053     /******************************************************************
00054      **
00055      **      FUNCTION NAME : strToTrue
00056      **      FUNCTION TYPE : bool
00057     */
00058     bool strToTrue( const std::string & str )
00059     {
00060       std::string t( toLower( str ) );
00061       return(    t == "1"
00062               || t == "yes"
00063               || t == "true"
00064               || t == "on"
00065             );
00066     }
00067 
00068     /******************************************************************
00069      **
00070      **      FUNCTION NAME : strToFalse
00071      **      FUNCTION TYPE : bool
00072     */
00073     bool strToFalse( const std::string & str )
00074     {
00075       std::string t( toLower( str ) );
00076       return ! (    t == "0"
00077                  || t == "no"
00078                  || t == "false"
00079                  || t == "off"
00080                );
00081     }
00082 
00084     // Hexencode
00086     namespace {
00088       inline bool heIsAlNum( char ch )
00089       {
00090         return ( ( 'a' <= ch && ch <= 'z' )
00091                ||( 'A' <= ch && ch <= 'Z' )
00092                ||( '0' <= ch && ch <= '9' ) );
00093       }
00095       inline int heDecodeCh( char ch )
00096       {
00097         if ( '0' <= ch && ch <= '9' )
00098           return( ch - '0' );
00099         if ( 'A' <= ch && ch <= 'F' )
00100           return( ch - 'A' + 10 );
00101         if ( 'a' <= ch && ch <= 'f' )
00102           return( ch - 'A' + 10 );
00103         return -1;
00104       }
00105     }
00106 
00107     std::string hexencode( const std::string & str_r )
00108     {
00109       static const char *const hdig = "0123456789ABCDEF";
00110       std::string res;
00111       res.reserve( str_r.size() );
00112       for ( const char * it = str_r.c_str(); *it; ++it )
00113       {
00114         if ( heIsAlNum( *it ) )
00115         {
00116           res += *it;
00117         }
00118         else
00119         {
00120           res += '%';
00121           res += hdig[(unsigned char)(*it)/16];
00122           res += hdig[(unsigned char)(*it)%16];
00123         }
00124       }
00125       return res;
00126     }
00127 
00128     std::string hexdecode( const std::string & str_r )
00129     {
00130       std::string res;
00131       res.reserve( str_r.size() );
00132       for ( const char * it = str_r.c_str(); *it; ++it )
00133       {
00134         if ( *it == '%' )
00135         {
00136           int d1 = heDecodeCh( *(it+1) );
00137           if ( d1 != -1 )
00138           {
00139             int d2 = heDecodeCh( *(it+2) );
00140             if ( d2 != -1 )
00141             {
00142               res += (d1<<4)|d2;
00143               it += 2;
00144               continue;
00145             }
00146           }
00147         }
00148         // verbatim if no %XX:
00149         res += *it;
00150       }
00151       return res;
00152     }
00154 
00155    /******************************************************************
00156      **
00157      **      FUNCTION NAME : toLower
00158      **      FUNCTION TYPE : std::string
00159     */
00160     std::string toLower( const std::string & s )
00161     {
00162       if ( s.empty() )
00163         return s;
00164 
00165       std::string ret( s );
00166       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00167         {
00168           if ( isupper( ret[i] ) )
00169             ret[i] = static_cast<char>(tolower( ret[i] ));
00170         }
00171       return ret;
00172     }
00173 
00174     /******************************************************************
00175      **
00176      **      FUNCTION NAME : toUpper
00177      **      FUNCTION TYPE : std::string
00178     */
00179     std::string toUpper( const std::string & s )
00180     {
00181       if ( s.empty() )
00182         return s;
00183 
00184       std::string ret( s );
00185       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00186         {
00187           if ( islower( ret[i] ) )
00188             ret[i] = static_cast<char>(toupper( ret[i] ));
00189         }
00190       return ret;
00191     }
00192 
00193     /******************************************************************
00194      **
00195      **      FUNCTION NAME : trim
00196      **      FUNCTION TYPE : std::string
00197     */
00198     std::string trim( const std::string & s, const Trim trim_r )
00199     {
00200       if ( s.empty() || trim_r == NO_TRIM )
00201         return s;
00202 
00203       std::string ret( s );
00204 
00205       if ( trim_r && L_TRIM )
00206         {
00207           std::string::size_type p = ret.find_first_not_of( " \t\n" );
00208           if ( p == std::string::npos )
00209             return std::string();
00210 
00211           ret = ret.substr( p );
00212         }
00213 
00214       if ( trim_r && R_TRIM )
00215         {
00216           std::string::size_type p = ret.find_last_not_of( " \t\n" );
00217           if ( p == std::string::npos )
00218             return std::string();
00219 
00220           ret = ret.substr( 0, p+1 );
00221         }
00222 
00223       return ret;
00224     }
00225     /******************************************************************
00226     **
00227     **  FUNCTION NAME : stripFirstWord
00228     **  FUNCTION TYPE : std::string
00229     **
00230     **  DESCRIPTION :
00231     */
00232     std::string stripFirstWord( std::string & line, const bool ltrim_first )
00233     {
00234       if ( ltrim_first )
00235         line = ltrim( line );
00236 
00237       if ( line.empty() )
00238         return line;
00239 
00240       std::string ret;
00241       std::string::size_type p = line.find_first_of( " \t" );
00242 
00243       if ( p == std::string::npos ) {
00244         // no ws on line
00245         ret = line;
00246         line.erase();
00247       } else if ( p == 0 ) {
00248         // starts with ws
00249         // ret remains empty
00250         line = ltrim( line );
00251       }
00252       else {
00253         // strip word and ltim line
00254         ret = line.substr( 0, p );
00255         line = ltrim( line.erase( 0, p ) );
00256       }
00257       return ret;
00258     }
00259 
00260     /******************************************************************
00261     **
00262     **
00263     **      FUNCTION NAME : getline
00264     **      FUNCTION TYPE : std::string
00265     **
00266     **      DESCRIPTION :
00267     */
00268     static inline std::string _getline( std::istream & str, const Trim trim_r )
00269     {
00270       const unsigned tmpBuffLen = 1024;
00271       char           tmpBuff[tmpBuffLen];
00272 
00273       std::string ret;
00274       do {
00275         str.clear();
00276         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
00277         ret += tmpBuff;
00278       } while( str.rdstate() == std::ios::failbit );
00279 
00280       return trim( ret, trim_r );
00281     }
00282 
00283     std::string getline( std::istream & str, const Trim trim_r )
00284     {
00285       return _getline(str, trim_r);
00286     }
00287 
00288     std::string getline( std::istream & str, bool trim )
00289     {
00290       return _getline(str, trim?TRIM:NO_TRIM);
00291     }
00292 
00293 
00294 
00296   } // namespace str
00299 } // namespace zypp

Generated on Sat Sep 5 12:40:16 2009 for zypp by  doxygen 1.4.6