stringutil.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013   File:       stringutil.h
00014 
00015   Author:     Michael Andres <ma@suse.de>
00016   Maintainer: Michael Andres <ma@suse.de>
00017 
00018   Purpose: Contains 'std::string form(const char * format, ...)' for
00019   printf style creation of strings and some more string utility
00020   functions.
00021 
00022 /-*/
00023 #ifndef stringutil_h
00024 #define stringutil_h
00025 
00026 #include <cstdio>
00027 #include <cstdarg>
00028 
00029 #include <iosfwd>
00030 #include <vector>
00031 #include <string>
00032 #include <list>
00033 
00037 
00038 namespace stringutil {
00039 ;
00040 
00041 enum Trim {
00042   NO_TRIM = 0x00,
00043   L_TRIM  = 0x01,
00044   R_TRIM  = 0x02,
00045   TRIM    = (L_TRIM|R_TRIM)
00046 };
00047 
00048 inline std::string form( const char * format, ... )
00049     __attribute__ ((format (printf, 1, 2)));
00050 
00060 inline std::string form( const char * format, ... ) {
00061   char * buf = 0;
00062   std::string val;
00063 
00064   va_list ap;
00065   va_start( ap, format );
00066 
00067 #if 1
00068   vasprintf( &buf, format, ap );
00069   if ( buf ) {
00070     val = buf;
00071     free( buf );
00072   }
00073 #else
00074   // Don't know wheter we actually nedd two va_lists, one to
00075   // evaluate the buffer size needed, and one to actually fill
00076   // the buffer. Maybe there's a save way to reuse a va_lists.
00077   va_list ap1;
00078   va_start( ap1, format );
00079   buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
00080   vsprintf( buf, format, ap1 );
00081   val = buf;
00082   delete [] buf;
00083   va_end( ap1 );
00084 #endif
00085 
00086   va_end( ap );
00087   return val;
00088 }
00089 
00100 inline std::string numstring( char n,               int w = 0 ) { return form( "%*hhd",  w, n ); }
00101 inline std::string numstring( unsigned char n,      int w = 0 ) { return form( "%*hhu",  w, n ); }
00102 inline std::string numstring( short n,              int w = 0 ) { return form( "%*hd",   w, n ); }
00103 inline std::string numstring( unsigned short n,     int w = 0 ) { return form( "%*hu",   w, n ); }
00104 inline std::string numstring( int n,                int w = 0 ) { return form( "%*d",    w, n ); }
00105 inline std::string numstring( unsigned n,           int w = 0 ) { return form( "%*u",    w, n ); }
00106 inline std::string numstring( long n,               int w = 0 ) { return form( "%*ld",   w, n ); }
00107 inline std::string numstring( unsigned long n,      int w = 0 ) { return form( "%*lu",   w, n ); }
00108 inline std::string numstring( long long n,          int w = 0 ) { return form( "%*lld",  w, n ); }
00109 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu",  w, n ); }
00110 
00121 inline std::string hexstring( char n,               int w = 4 ) { return form( "%#0*hhx", w, n ); }
00122 inline std::string hexstring( unsigned char n,      int w = 4 ) { return form( "%#0*hhx", w, n ); }
00123 inline std::string hexstring( short n,              int w = 10 ){ return form( "%#0*hx",  w, n ); }
00124 inline std::string hexstring( unsigned short n,     int w = 10 ){ return form( "%#0*hx",  w, n ); }
00125 inline std::string hexstring( int n,                int w = 10 ){ return form( "%#0*x",   w, n ); }
00126 inline std::string hexstring( unsigned n,           int w = 10 ){ return form( "%#0*x",   w, n ); }
00127 inline std::string hexstring( long n,               int w = 10 ){ return form( "%#0*lx",  w, n ); }
00128 inline std::string hexstring( unsigned long n,      int w = 10 ){ return form( "%#0*lx",  w, n ); }
00129 inline std::string hexstring( long long n,          int w = 0 ) { return form( "%#0*llx", w, n ); }
00130 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00131 
00142 inline std::string octstring( char n,               int w = 4 ) { return form( "%#0*hho",  w, n ); }
00143 inline std::string octstring( unsigned char n,      int w = 4 ) { return form( "%#0*hho",  w, n ); }
00144 inline std::string octstring( short n,              int w = 5 ) { return form( "%#0*ho",   w, n ); }
00145 inline std::string octstring( unsigned short n,     int w = 5 ) { return form( "%#0*ho",   w, n ); }
00146 inline std::string octstring( int n,                int w = 5 ) { return form( "%#0*o",    w, n ); }
00147 inline std::string octstring( unsigned n,           int w = 5 ) { return form( "%#0*o",    w, n ); }
00148 inline std::string octstring( long n,               int w = 5 ) { return form( "%#0*lo",   w, n ); }
00149 inline std::string octstring( unsigned long n,      int w = 5 ) { return form( "%#0*lo",   w, n ); }
00150 inline std::string octstring( long long n,          int w = 0 ) { return form( "%#0*llo",  w, n ); }
00151 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo",  w, n ); }
00152 
00156 template<typename _It>
00157   inline _It strtonum( const std::string & str );
00158 
00159 template<>
00160   inline short              strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00161 template<>
00162   inline int                strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00163 template<>
00164   inline long               strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00165 template<>
00166   inline long long          strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
00167 
00168 template<>
00169   inline unsigned short     strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00170 template<>
00171   inline unsigned           strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00172 template<>
00173   inline unsigned long      strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00174 template<>
00175   inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
00176 
00180 template<typename _It>
00181   inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
00182 
00207 extern std::string getline( std::istream & str, bool trim = false );
00208 
00213 extern std::string getline( std::istream & str, const Trim trim_r );
00214 
00245 extern unsigned split( const std::string          line_r,
00246                        std::vector<std::string> & words_r,
00247                        const std::string &        sep_t       = " \t",
00248                        const bool                 singlesep_r = false );
00249 
00253 extern std::string join( const std::vector<std::string> & words_r,
00254                          const std::string & sep_r = " " );
00255 
00256 
00265 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
00266 {
00267   std::vector<std::string> lines;
00268   stringutil::split( text_r, lines, "\n", true );
00269   std::list<std::string> ret;
00270   for ( unsigned i = 0; i < lines.size(); ++i ) {
00271     ret.push_back( lines[i] );
00272   }
00273   return ret;
00274 }
00275 
00293 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
00294 
00298 extern std::string ltrim( const std::string & s );
00299 extern std::string rtrim( const std::string & s );
00300 inline std::string  trim( const std::string & s, const Trim trim_r = TRIM ) {
00301   switch ( trim_r ) {
00302   case L_TRIM:
00303     return ltrim( s );
00304   case R_TRIM:
00305     return rtrim( s );
00306   case TRIM:
00307     return ltrim( rtrim( s ) );
00308   case NO_TRIM:
00309     break;
00310   }
00311   return s;
00312 }
00313 
00317 extern std::string toLower( const std::string & s );
00318 extern std::string toUpper( const std::string & s );
00319 
00323 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
00324 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
00325 
00327 }  // namespace stringutil
00329 
00330 #endif // stringutil_h

Generated on Fri Jun 16 15:51:35 2006 for liby2util by  doxygen 1.4.6