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 bool strToTrue( const std::string & str )
00059 {
00060 std::string t( toLower( str ) );
00061 return( t == "1"
00062 || t == "yes"
00063 || t == "true"
00064 || t == "on"
00065 );
00066 }
00067
00068
00069
00070
00071
00072
00073 bool strToFalse( const std::string & str )
00074 {
00075 std::string t( toLower( str ) );
00076 return ! ( t == "0"
00077 || t == "no"
00078 || t == "false"
00079 || t == "off"
00080 );
00081 }
00082
00083
00084
00085
00086
00087
00088 std::string toLower( const std::string & s )
00089 {
00090 if ( s.empty() )
00091 return s;
00092
00093 std::string ret( s );
00094 for ( std::string::size_type i = 0; i < ret.length(); ++i )
00095 {
00096 if ( isupper( ret[i] ) )
00097 ret[i] = static_cast<char>(tolower( ret[i] ));
00098 }
00099 return ret;
00100 }
00101
00102
00103
00104
00105
00106
00107 std::string toUpper( const std::string & s )
00108 {
00109 if ( s.empty() )
00110 return s;
00111
00112 std::string ret( s );
00113 for ( std::string::size_type i = 0; i < ret.length(); ++i )
00114 {
00115 if ( islower( ret[i] ) )
00116 ret[i] = static_cast<char>(toupper( ret[i] ));
00117 }
00118 return ret;
00119 }
00120
00121
00122
00123
00124
00125
00126 std::string trim( const std::string & s, const Trim trim_r )
00127 {
00128 if ( s.empty() || trim_r == NO_TRIM )
00129 return s;
00130
00131 std::string ret( s );
00132
00133 if ( trim_r && L_TRIM )
00134 {
00135 std::string::size_type p = ret.find_first_not_of( " \t\n" );
00136 if ( p == std::string::npos )
00137 return std::string();
00138
00139 ret = ret.substr( p );
00140 }
00141
00142 if ( trim_r && R_TRIM )
00143 {
00144 std::string::size_type p = ret.find_last_not_of( " \t\n" );
00145 if ( p == std::string::npos )
00146 return std::string();
00147
00148 ret = ret.substr( 0, p+1 );
00149 }
00150
00151 return ret;
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 std::string stripFirstWord( std::string & line, const bool ltrim_first )
00161 {
00162 if ( ltrim_first )
00163 line = ltrim( line );
00164
00165 if ( line.empty() )
00166 return line;
00167
00168 std::string ret;
00169 std::string::size_type p = line.find_first_of( " \t" );
00170
00171 if ( p == std::string::npos ) {
00172
00173 ret = line;
00174 line.erase();
00175 } else if ( p == 0 ) {
00176
00177
00178 line = ltrim( line );
00179 }
00180 else {
00181
00182 ret = line.substr( 0, p );
00183 line = ltrim( line.erase( 0, p ) );
00184 }
00185 return ret;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 static inline std::string _getline( std::istream & str, const Trim trim_r )
00197 {
00198 const unsigned tmpBuffLen = 1024;
00199 char tmpBuff[tmpBuffLen];
00200
00201 std::string ret;
00202 do {
00203 str.clear();
00204 str.getline( tmpBuff, tmpBuffLen );
00205 ret += tmpBuff;
00206 } while( str.rdstate() == std::ios::failbit );
00207
00208 return trim( ret, trim_r );
00209 }
00210
00211 std::string getline( std::istream & str, const Trim trim_r )
00212 {
00213 return _getline(str, trim_r);
00214 }
00215
00216 std::string getline( std::istream & str, bool trim )
00217 {
00218 return _getline(str, trim?TRIM:NO_TRIM);
00219 }
00220
00221
00222
00224 }
00227 }