00001
00002
00003
00004
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
00029
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
00046
00047
00048 std::string strerror( int errno_r )
00049 {
00050 return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
00051 }
00052
00053
00054
00055
00056
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
00075
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
00094
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
00126
00127
00128
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
00143 ret = line;
00144 line.erase();
00145 } else if ( p == 0 ) {
00146
00147
00148 line = ltrim( line );
00149 }
00150 else {
00151
00152 ret = line.substr( 0, p );
00153 line = ltrim( line.erase( 0, p ) );
00154 }
00155 return ret;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
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 );
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 }
00197 }