libzypp 17.37.10

IOStream.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <boost/mpl/assert.hpp>
14#include <boost/mpl/int.hpp>
15//#include <zypp-core/base/Logger.h>
16
17#include <zypp-core/base/IOStream.h>
18#include <zypp-core/base/String.h>
19
20 using std::endl;
22 namespace zypp
23{
25 namespace iostr
26 {
27
28 /******************************************************************
29 **
30 ** FUNCTION NAME : getline
31 ** FUNCTION TYPE : std::string
32 */
33 std::string getline( std::istream & str )
34 {
35 static const unsigned tmpBuffLen = 1024;
36 static char tmpBuff[tmpBuffLen];
37 std::string ret;
38 do {
39 str.clear();
40 str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
41 ret += tmpBuff;
42 } while( str.rdstate() == std::ios::failbit );
43
44 return ret;
45 }
46
48 //
49 // CLASS NAME : EachLine
50 //
52
54 //
55 // METHOD NAME : EachLine::EachLine
56 // METHOD TYPE : Ctor
57 //
58 EachLine::EachLine( std::istream & str_r, unsigned lineNo_r )
59 : _str( str_r )
60 , _lineStart( -1 )
61 , _lineNo( lineNo_r )
62 , _valid( true )
63 {
64 next();
65 }
66
68 //
69 // METHOD NAME : EachLine::next
70 // METHOD TYPE : bool
71 //
72 bool EachLine::next()
73 {
74 if ( ! _valid )
75 {
76 return false;
77 }
78
79 if ( ! _str ) // usg: saw EOF in previous read
80 {
81 _line.clear();
82 return(_valid = false);
83 }
84
85 _lineStart = _str.tellg();
86 _line = iostr::getline( _str );
87 ++_lineNo;
88 if ( _str.fail() || _str.bad() )
89 {
90 _line.clear();
91 return(_valid = false);
92 }
93 return(_valid = true);
94 }
95
97 // forEachLine
99
100 int forEachLine( std::istream & str_r, const function<bool(int, std::string)>& consume_r )
101 {
102 int lineno = 0;
103 while ( str_r )
104 {
105 std::string line( getline( str_r ) );
106 if ( ! (str_r.fail() || str_r.bad()) )
107 {
108 // line contains valid data to be consumed.
109 ++lineno;
110 if ( consume_r && ! consume_r( lineno, line ) )
111 {
112 lineno = -lineno;
113 break;
114 }
115 }
116 }
117 return lineno;
118 }
119
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) );
123
124 int simpleParseFile( std::istream & str_r, ParseFlags flags_r, function<bool(int, std::string)> consume_r )
125 {
126 return forEachLine( str_r,
127 [&]( int num_r, std::string line_r )->bool
128 {
129 if ( ! consume_r )
130 return true;
131
132 if ( flags_r )
133 {
134 if ( flags_r & PF_TRIM )
135 line_r = str::trim( line_r, str::Trim( unsigned(flags_r & PF_TRIM) ) );
136
137 if ( flags_r & ~PF_TRIM )
138 {
139 const char* firstNW = line_r.c_str();
140 while ( *firstNW == ' ' || *firstNW == '\t' )
141 ++firstNW;
142 switch ( *firstNW )
143 {
144 case '\0': if ( flags_r & PF_SKIP_EMPTY ) return true; break;
145 case '#': if ( flags_r & PF_SKIP_SHARP_COMMENT ) return true; break;
146 }
147 }
148 }
149 return consume_r( num_r, line_r );
150 } );
151 }
152
154 } // namespace iostr
155
157} // namespace zypp
zypp::iostr::EachLine::_str
std::istream & _str
Definition IOStream.h:160
zypp::iostr::EachLine::_line
std::string _line
Definition IOStream.h:161
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition IOStream.cc:72
zypp::iostr::EachLine::EachLine
EachLine(std::istream &str_r, unsigned lineNo_r=0)
Ctor taking a stream and reading the 1st line from it.
Definition IOStream.cc:58
zypp::iostr::EachLine::_valid
bool _valid
Definition IOStream.h:164
zypp::iostr::EachLine::_lineStart
std::streamoff _lineStart
Definition IOStream.h:162
zypp::iostr::EachLine::_lineNo
unsigned _lineNo
Definition IOStream.h:163
str
String related utilities and Regular expression matching.
zypp::iostr
Iostream related utilities.
Definition IOStream.cc:26
zypp::iostr::BOOST_MPL_ASSERT_RELATION
BOOST_MPL_ASSERT_RELATION(int(PF_LTRIM),==, int(str::L_TRIM))
zypp::iostr::forEachLine
int forEachLine(std::istream &str_r, const function< bool(int, std::string)> &consume_r)
Simple lineparser: Call functor consume_r for each line.
Definition IOStream.cc:100
zypp::iostr::PF_SKIP_EMPTY
@ PF_SKIP_EMPTY
Definition IOStream.h:194
zypp::iostr::PF_RTRIM
@ PF_RTRIM
Definition IOStream.h:192
zypp::iostr::PF_TRIM
@ PF_TRIM
Definition IOStream.h:193
zypp::iostr::PF_SKIP_SHARP_COMMENT
@ PF_SKIP_SHARP_COMMENT
Definition IOStream.h:195
zypp::iostr::PF_LTRIM
@ PF_LTRIM
Definition IOStream.h:191
zypp::iostr::simpleParseFile
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
Definition IOStream.cc:124
zypp::iostr::getline
std::string getline(std::istream &str)
Read one line from stream.
Definition IOStream.cc:33
zypp::str::Trim
Trim
To define how to trim.
Definition String.h:567
zypp::str::R_TRIM
@ R_TRIM
Definition String.h:570
zypp::str::L_TRIM
@ L_TRIM
Definition String.h:569
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
IOStream.h
String.h