e342e2c63cbb4dbe672242e3ea0c7d8008fc4dfd
[platform/upstream/libzypp.git] / zypp / base / String.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/String.cc
10  *
11 */
12 #include <cstdio>
13 #include <cstdarg>
14
15 #include <iostream>
16
17 #include "zypp/base/String.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22   ///////////////////////////////////////////////////////////////////
23   namespace str
24   { /////////////////////////////////////////////////////////////////
25
26     /******************************************************************
27      **
28      **      FUNCTION NAME : form
29      **      FUNCTION TYPE : std::string
30     */
31     std::string form( const char * format, ... )
32     {
33       SafeBuf safe;
34
35       va_list ap;
36       va_start( ap, format );
37       vasprintf( &safe._buf, format, ap );
38       va_end( ap );
39
40       return safe.asString();
41     }
42
43     /******************************************************************
44      **
45      **      FUNCTION NAME : strerror
46      **      FUNCTION TYPE : std::string
47     */
48     std::string strerror( int errno_r )
49     {
50       return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
51     }
52
53     /******************************************************************
54      **
55      **      FUNCTION NAME : toLower
56      **      FUNCTION TYPE : std::string
57     */
58     std::string toLower( const std::string & s )
59     {
60       if ( s.empty() )
61         return s;
62
63       std::string ret( s );
64       for ( std::string::size_type i = 0; i < ret.length(); ++i )
65         {
66           if ( isupper( ret[i] ) )
67             ret[i] = static_cast<char>(tolower( ret[i] ));
68         }
69       return ret;
70     }
71
72     /******************************************************************
73      **
74      **      FUNCTION NAME : toUpper
75      **      FUNCTION TYPE : std::string
76     */
77     std::string toUpper( const std::string & s )
78     {
79       if ( s.empty() )
80         return s;
81
82       std::string ret( s );
83       for ( std::string::size_type i = 0; i < ret.length(); ++i )
84         {
85           if ( islower( ret[i] ) )
86             ret[i] = static_cast<char>(toupper( ret[i] ));
87         }
88       return ret;
89     }
90
91     /******************************************************************
92      **
93      **      FUNCTION NAME : trim
94      **      FUNCTION TYPE : std::string
95     */
96     std::string trim( const std::string & s, const Trim trim_r )
97     {
98       if ( s.empty() || trim_r == NO_TRIM )
99         return s;
100
101       std::string ret( s );
102
103       if ( trim_r && L_TRIM )
104         {
105           std::string::size_type p = ret.find_first_not_of( " \t\n" );
106           if ( p == std::string::npos )
107             return std::string();
108
109           ret = ret.substr( p );
110         }
111
112       if ( trim_r && R_TRIM )
113         {
114           std::string::size_type p = ret.find_last_not_of( " \t\n" );
115           if ( p == std::string::npos )
116             return std::string();
117
118           ret = ret.substr( 0, p+1 );
119         }
120
121       return ret;
122     }
123     /******************************************************************
124     **
125     **  FUNCTION NAME : stripFirstWord
126     **  FUNCTION TYPE : std::string
127     **
128     **  DESCRIPTION :
129     */
130     std::string stripFirstWord( std::string & line, const bool ltrim_first )
131     {
132       if ( ltrim_first )
133         line = ltrim( line );
134
135       if ( line.empty() )
136         return line;
137
138       std::string ret;
139       std::string::size_type p = line.find_first_of( " \t" );
140
141       if ( p == std::string::npos ) {
142         // no ws on line
143         ret = line;
144         line.erase();
145       } else if ( p == 0 ) {
146         // starts with ws
147         // ret remains empty
148         line = ltrim( line );
149       }
150       else {
151         // strip word and ltim line
152         ret = line.substr( 0, p );
153         line = ltrim( line.erase( 0, p ) );
154       }
155       return ret;
156     }
157
158     /******************************************************************
159     **
160     **
161     **      FUNCTION NAME : getline
162     **      FUNCTION TYPE : std::string
163     **
164     **      DESCRIPTION :
165     */
166     static inline std::string _getline( std::istream & str, const Trim trim_r )
167     {
168       const unsigned tmpBuffLen = 1024;
169       char           tmpBuff[tmpBuffLen];
170
171       std::string ret;
172       do {
173         str.clear();
174         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
175         ret += tmpBuff;
176       } while( str.rdstate() == std::ios::failbit );
177
178       return trim( ret, trim_r );
179     }
180
181     std::string getline( std::istream & str, const Trim trim_r )
182     {
183       return _getline(str, trim_r);
184     }
185
186     std::string getline( std::istream & str, bool trim )
187     {
188       return _getline(str, trim?TRIM:NO_TRIM);
189     }
190
191     std::ostream & dumpRegexpResult( const boost::smatch &what, std::ostream & str )
192     {
193       for ( unsigned int k=0; k < what.size(); k++)
194       {
195         str << "[match "<< k << "] [" << what[k] << "]" << std::endl;
196       }
197
198       return str;
199     }
200     /////////////////////////////////////////////////////////////////
201   } // namespace str
202   ///////////////////////////////////////////////////////////////////
203   ////////////////////////////////////////////////////////////////
204 } // namespace zypp
205 //////////////////////////////////////////////////////////////////