1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/IOStream.cc
13 #include <boost/mpl/assert.hpp>
14 #include <boost/mpl/int.hpp>
15 //#include "zypp/base/Logger.h"
17 #include "zypp/base/IOStream.h"
18 #include "zypp/base/String.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 /******************************************************************
30 ** FUNCTION NAME : getline
31 ** FUNCTION TYPE : std::string
33 std::string getline( std::istream & str )
35 static const unsigned tmpBuffLen = 1024;
36 static char tmpBuff[tmpBuffLen];
40 str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
42 } while( str.rdstate() == std::ios::failbit );
47 ///////////////////////////////////////////////////////////////////
49 // CLASS NAME : EachLine
51 ///////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////
55 // METHOD NAME : EachLine::EachLine
58 EachLine::EachLine( std::istream & str_r, unsigned lineNo_r )
67 ///////////////////////////////////////////////////////////////////
69 // METHOD NAME : EachLine::next
79 if ( ! _str ) // usg: saw EOF in previous read
82 return(_valid = false);
85 _lineStart = _str.tellg();
86 _line = iostr::getline( _str );
88 if ( _str.fail() || _str.bad() )
91 return(_valid = false);
93 return(_valid = true);
96 ///////////////////////////////////////////////////////////////////
98 ///////////////////////////////////////////////////////////////////
100 int forEachLine( std::istream & str_r, function<bool(int, std::string)> consume_r )
105 std::string line( getline( str_r ) );
106 if ( ! (str_r.fail() || str_r.bad()) )
108 // line contains valid data to be consumed.
110 if ( consume_r && ! consume_r( lineno, line ) )
120 // MPL checks to assert equal values for PF_?TRIM and str::?TRIM
121 BOOST_MPL_ASSERT_RELATION( int(PF_LTRIM), ==, int(str::L_TRIM) );
122 BOOST_MPL_ASSERT_RELATION( int(PF_RTRIM), ==, int(str::R_TRIM) );
124 int simpleParseFile( std::istream & str_r, ParseFlags flags_r, function<bool(int, std::string)> consume_r )
126 return forEachLine( str_r,
127 [&]( int num_r, std::string line_r )->bool
134 if ( flags_r & PF_TRIM )
135 line_r = str::trim( line_r, str::Trim( unsigned(flags_r & PF_TRIM) ) );
137 if ( flags_r & ~PF_TRIM )
139 const char* firstNW = line_r.c_str();
140 while ( *firstNW == ' ' || *firstNW == '\t' )
144 case '\0': if ( flags_r & PF_SKIP_EMPTY ) return true; break;
145 case '#': if ( flags_r & PF_SKIP_SHARP_COMMENT ) return true; break;
149 return consume_r( num_r, line_r );
153 /////////////////////////////////////////////////////////////////
155 ///////////////////////////////////////////////////////////////////
156 /////////////////////////////////////////////////////////////////
158 ///////////////////////////////////////////////////////////////////