00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef stringutil_h
00024 #define stringutil_h
00025
00026 #include <cstdio>
00027 #include <cstdarg>
00028 #include <cstdlib>
00029
00030 #include <iosfwd>
00031 #include <vector>
00032 #include <string>
00033 #include <list>
00034
00038
00039 namespace stringutil {
00040 ;
00041
00042 enum Trim {
00043 NO_TRIM = 0x00,
00044 L_TRIM = 0x01,
00045 R_TRIM = 0x02,
00046 TRIM = (L_TRIM|R_TRIM)
00047 };
00048
00049 inline std::string form( const char * format, ... )
00050 __attribute__ ((format (printf, 1, 2)));
00051
00061 inline std::string form( const char * format, ... ) {
00062 char * buf = 0;
00063 std::string val;
00064
00065 va_list ap;
00066 va_start( ap, format );
00067
00068 #if 1
00069 int numprinted = vasprintf( &buf, format, ap );
00070 if ( numprinted >= 0 ) {
00071 val = buf;
00072 free( buf );
00073 }
00074 #else
00075
00076
00077
00078 va_list ap1;
00079 va_start( ap1, format );
00080 buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
00081 vsprintf( buf, format, ap1 );
00082 val = buf;
00083 delete [] buf;
00084 va_end( ap1 );
00085 #endif
00086
00087 va_end( ap );
00088 return val;
00089 }
00090
00101 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
00102 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
00103 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
00104 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
00105 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
00106 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
00107 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
00108 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
00109 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
00110 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
00111
00122 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00123 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00124 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
00125 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
00126 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
00127 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
00128 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00129 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00130 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00131 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00132
00143 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00144 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00145 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
00146 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
00147 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
00148 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
00149 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00150 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00151 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00152 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00153
00157 template<typename _It>
00158 inline _It strtonum( const std::string & str );
00159
00160 template<>
00161 inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
00162 template<>
00163 inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
00164 template<>
00165 inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
00166 template<>
00167 inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
00168
00169 template<>
00170 inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00171 template<>
00172 inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00173 template<>
00174 inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00175 template<>
00176 inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
00177
00181 template<typename _It>
00182 inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
00183
00208 extern std::string getline( std::istream & str, bool trim = false );
00209
00214 extern std::string getline( std::istream & str, const Trim trim_r );
00215
00246 extern unsigned split( const std::string line_r,
00247 std::vector<std::string> & words_r,
00248 const std::string & sep_t = " \t",
00249 const bool singlesep_r = false );
00250
00254 extern std::string join( const std::vector<std::string> & words_r,
00255 const std::string & sep_r = " " );
00256
00257
00266 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
00267 {
00268 std::vector<std::string> lines;
00269 stringutil::split( text_r, lines, sep_r, true );
00270 std::list<std::string> ret;
00271 for ( unsigned i = 0; i < lines.size(); ++i ) {
00272 ret.push_back( lines[i] );
00273 }
00274 return ret;
00275 }
00276
00294 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
00295
00299 extern std::string ltrim( const std::string & s );
00300 extern std::string rtrim( const std::string & s );
00301 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
00302 switch ( trim_r ) {
00303 case L_TRIM:
00304 return ltrim( s );
00305 case R_TRIM:
00306 return rtrim( s );
00307 case TRIM:
00308 return ltrim( rtrim( s ) );
00309 case NO_TRIM:
00310 break;
00311 }
00312 return s;
00313 }
00314
00318 extern std::string toLower( const std::string & s );
00319 extern std::string toUpper( const std::string & s );
00320
00324 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
00325 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
00326
00328 }
00330
00331 #endif // stringutil_h