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 : toLower
00056      **      FUNCTION TYPE : std::string
00057     */
00058     std::string toLower( const std::string & s )
00059     {
00060       if ( s.empty() )
00061         return s;
00062 
00063       std::string ret( s );
00064       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00065         {
00066           if ( isupper( ret[i] ) )
00067             ret[i] = static_cast<char>(tolower( ret[i] ));
00068         }
00069       return ret;
00070     }
00071 
00072     /******************************************************************
00073      **
00074      **      FUNCTION NAME : toUpper
00075      **      FUNCTION TYPE : std::string
00076     */
00077     std::string toUpper( const std::string & s )
00078     {
00079       if ( s.empty() )
00080         return s;
00081 
00082       std::string ret( s );
00083       for ( std::string::size_type i = 0; i < ret.length(); ++i )
00084         {
00085           if ( islower( ret[i] ) )
00086             ret[i] = static_cast<char>(toupper( ret[i] ));
00087         }
00088       return ret;
00089     }
00090 
00091     /******************************************************************
00092      **
00093      **      FUNCTION NAME : trim
00094      **      FUNCTION TYPE : std::string
00095     */
00096     std::string trim( const std::string & s, const Trim trim_r )
00097     {
00098       if ( s.empty() || trim_r == NO_TRIM )
00099         return s;
00100 
00101       std::string ret( s );
00102 
00103       if ( trim_r && L_TRIM )
00104         {
00105           std::string::size_type p = ret.find_first_not_of( " \t\n" );
00106           if ( p == std::string::npos )
00107             return std::string();
00108 
00109           ret = ret.substr( p );
00110         }
00111 
00112       if ( trim_r && R_TRIM )
00113         {
00114           std::string::size_type p = ret.find_last_not_of( " \t\n" );
00115           if ( p == std::string::npos )
00116             return std::string();
00117 
00118           ret = ret.substr( 0, p+1 );
00119         }
00120 
00121       return ret;
00122     }
00123     /******************************************************************
00124     **
00125     **  FUNCTION NAME : stripFirstWord
00126     **  FUNCTION TYPE : std::string
00127     **
00128     **  DESCRIPTION :
00129     */
00130     std::string stripFirstWord( std::string & line, const bool ltrim_first )
00131     {
00132       if ( ltrim_first )
00133         line = ltrim( line );
00134     
00135       if ( line.empty() )
00136         return line;
00137     
00138       std::string ret;
00139       std::string::size_type p = line.find_first_of( " \t" );
00140     
00141       if ( p == std::string::npos ) {
00142         // no ws on line
00143         ret = line;
00144         line.erase();
00145       } else if ( p == 0 ) {
00146         // starts with ws
00147         // ret remains empty
00148         line = ltrim( line );
00149       }
00150       else {
00151         // strip word and ltim line
00152         ret = line.substr( 0, p );
00153         line = ltrim( line.erase( 0, p ) );
00154       }
00155       return ret;
00156     }
00157 
00158     /******************************************************************
00159     **
00160     **
00161     **      FUNCTION NAME : getline
00162     **      FUNCTION TYPE : std::string
00163     **
00164     **      DESCRIPTION :
00165     */
00166     static inline std::string _getline( std::istream & str, const Trim trim_r )
00167     {
00168       const unsigned tmpBuffLen = 1024;
00169       char           tmpBuff[tmpBuffLen];
00170 
00171       std::string ret;
00172       do {
00173         str.clear();
00174         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
00175         ret += tmpBuff;
00176       } while( str.rdstate() == std::ios::failbit );
00177     
00178       return trim( ret, trim_r );
00179     }
00180     
00181     std::string getline( std::istream & str, const Trim trim_r )
00182     {
00183       return _getline(str, trim_r);
00184     }
00185     
00186     std::string getline( std::istream & str, bool trim )
00187     {
00188       return _getline(str, trim?TRIM:NO_TRIM);
00189     }
00190 
00191 
00192 
00194   } // namespace str
00197 } // namespace zypp

Generated on Thu Jul 6 00:07:20 2006 for zypp by  doxygen 1.4.6