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
00019 using std::string;
00020
00022 namespace zypp
00023 {
00024
00025 namespace str
00026 {
00027
00028
00029
00030
00031
00032
00033 std::string form( const char * format, ... )
00034 {
00035 SafeBuf safe;
00036
00037 va_list ap;
00038 va_start( ap, format );
00039 vasprintf( &safe._buf, format, ap );
00040 va_end( ap );
00041
00042 return safe.asString();
00043 }
00044
00045
00046
00047
00048
00049
00050 std::string strerror( int errno_r )
00051 {
00052 return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
00053 }
00054
00055
00056
00057
00058
00059
00060 bool strToTrue( const std::string & str )
00061 {
00062 std::string t( toLower( str ) );
00063 return( t == "1"
00064 || t == "yes"
00065 || t == "true"
00066 || t == "on"
00067 );
00068 }
00069
00070
00071
00072
00073
00074
00075 bool strToFalse( const std::string & str )
00076 {
00077 std::string t( toLower( str ) );
00078 return ! ( t == "0"
00079 || t == "no"
00080 || t == "false"
00081 || t == "off"
00082 );
00083 }
00084
00085
00086
00087
00088
00089
00090 std::string toLower( const std::string & s )
00091 {
00092 if ( s.empty() )
00093 return s;
00094
00095 std::string ret( s );
00096 for ( std::string::size_type i = 0; i < ret.length(); ++i )
00097 {
00098 if ( isupper( ret[i] ) )
00099 ret[i] = static_cast<char>(tolower( ret[i] ));
00100 }
00101 return ret;
00102 }
00103
00104
00105
00106
00107
00108
00109 std::string toUpper( const std::string & s )
00110 {
00111 if ( s.empty() )
00112 return s;
00113
00114 std::string ret( s );
00115 for ( std::string::size_type i = 0; i < ret.length(); ++i )
00116 {
00117 if ( islower( ret[i] ) )
00118 ret[i] = static_cast<char>(toupper( ret[i] ));
00119 }
00120 return ret;
00121 }
00122
00123
00124
00125
00126
00127
00128 std::string trim( const std::string & s, const Trim trim_r )
00129 {
00130 if ( s.empty() || trim_r == NO_TRIM )
00131 return s;
00132
00133 std::string ret( s );
00134
00135 if ( trim_r && L_TRIM )
00136 {
00137 std::string::size_type p = ret.find_first_not_of( " \t\n" );
00138 if ( p == std::string::npos )
00139 return std::string();
00140
00141 ret = ret.substr( p );
00142 }
00143
00144 if ( trim_r && R_TRIM )
00145 {
00146 std::string::size_type p = ret.find_last_not_of( " \t\n" );
00147 if ( p == std::string::npos )
00148 return std::string();
00149
00150 ret = ret.substr( 0, p+1 );
00151 }
00152
00153 return ret;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 std::string stripFirstWord( std::string & line, const bool ltrim_first )
00163 {
00164 if ( ltrim_first )
00165 line = ltrim( line );
00166
00167 if ( line.empty() )
00168 return line;
00169
00170 std::string ret;
00171 std::string::size_type p = line.find_first_of( " \t" );
00172
00173 if ( p == std::string::npos ) {
00174
00175 ret = line;
00176 line.erase();
00177 } else if ( p == 0 ) {
00178
00179
00180 line = ltrim( line );
00181 }
00182 else {
00183
00184 ret = line.substr( 0, p );
00185 line = ltrim( line.erase( 0, p ) );
00186 }
00187 return ret;
00188 }
00189
00190 string gsub(const string& sData, const string& sFrom, const string& sTo)
00191 {
00192 string sNew = sData;
00193
00194 if (! sNew.empty())
00195 {
00196 string::size_type toLen = sTo.length();
00197 string::size_type frLen = sFrom.length();
00198 string::size_type loc = 0;
00199
00200 while (string::npos != (loc = sNew.find(sFrom, loc)))
00201 {
00202 sNew.replace(loc, frLen, sTo);
00203 loc += toLen;
00204
00205 if (loc >= sNew.length())
00206 break;
00207 }
00208 }
00209
00210 return sNew;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 static inline std::string _getline( std::istream & str, const Trim trim_r )
00222 {
00223 const unsigned tmpBuffLen = 1024;
00224 char tmpBuff[tmpBuffLen];
00225
00226 std::string ret;
00227 do {
00228 str.clear();
00229 str.getline( tmpBuff, tmpBuffLen );
00230 ret += tmpBuff;
00231 } while( str.rdstate() == std::ios::failbit );
00232
00233 return trim( ret, trim_r );
00234 }
00235
00236 std::string getline( std::istream & str, const Trim trim_r )
00237 {
00238 return _getline(str, trim_r);
00239 }
00240
00241 std::string getline( std::istream & str, bool trim )
00242 {
00243 return _getline(str, trim?TRIM:NO_TRIM);
00244 }
00245
00247 }
00250 }