1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/String.cc
17 #include "zypp/base/String.h"
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
26 /******************************************************************
28 ** FUNCTION NAME : form
29 ** FUNCTION TYPE : std::string
31 std::string form( const char * format, ... )
36 va_start( ap, format );
37 vasprintf( &safe._buf, format, ap );
40 return safe.asString();
43 /******************************************************************
45 ** FUNCTION NAME : strerror
46 ** FUNCTION TYPE : std::string
48 std::string strerror( int errno_r )
50 return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
53 /******************************************************************
55 ** FUNCTION NAME : toLower
56 ** FUNCTION TYPE : std::string
58 std::string toLower( const std::string & s )
64 for ( std::string::size_type i = 0; i < ret.length(); ++i )
66 if ( isupper( ret[i] ) )
67 ret[i] = static_cast<char>(tolower( ret[i] ));
72 /******************************************************************
74 ** FUNCTION NAME : toUpper
75 ** FUNCTION TYPE : std::string
77 std::string toUpper( const std::string & s )
83 for ( std::string::size_type i = 0; i < ret.length(); ++i )
85 if ( islower( ret[i] ) )
86 ret[i] = static_cast<char>(toupper( ret[i] ));
91 /******************************************************************
93 ** FUNCTION NAME : trim
94 ** FUNCTION TYPE : std::string
96 std::string trim( const std::string & s, const Trim trim_r )
98 if ( s.empty() || trim_r == NO_TRIM )
101 std::string ret( s );
103 if ( trim_r && L_TRIM )
105 std::string::size_type p = ret.find_first_not_of( " \t\n" );
106 if ( p == std::string::npos )
107 return std::string();
109 ret = ret.substr( p );
112 if ( trim_r && R_TRIM )
114 std::string::size_type p = ret.find_last_not_of( " \t\n" );
115 if ( p == std::string::npos )
116 return std::string();
118 ret = ret.substr( 0, p+1 );
123 /******************************************************************
125 ** FUNCTION NAME : stripFirstWord
126 ** FUNCTION TYPE : std::string
130 std::string stripFirstWord( std::string & line, const bool ltrim_first )
133 line = ltrim( line );
139 std::string::size_type p = line.find_first_of( " \t" );
141 if ( p == std::string::npos ) {
145 } else if ( p == 0 ) {
148 line = ltrim( line );
151 // strip word and ltim line
152 ret = line.substr( 0, p );
153 line = ltrim( line.erase( 0, p ) );
158 /******************************************************************
161 ** FUNCTION NAME : getline
162 ** FUNCTION TYPE : std::string
166 static inline std::string _getline( std::istream & str, const Trim trim_r )
168 const unsigned tmpBuffLen = 1024;
169 char tmpBuff[tmpBuffLen];
174 str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
176 } while( str.rdstate() == std::ios::failbit );
178 return trim( ret, trim_r );
181 std::string getline( std::istream & str, const Trim trim_r )
183 return _getline(str, trim_r);
186 std::string getline( std::istream & str, bool trim )
188 return _getline(str, trim?TRIM:NO_TRIM);
191 /////////////////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////////////////
194 ////////////////////////////////////////////////////////////////
196 //////////////////////////////////////////////////////////////////