libzypp: zypp/base/IOStream.h Source File

libzypp 17.25.10

IOStream.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_IOSTREAM_H
13 #define ZYPP_BASE_IOSTREAM_H
14
15 #include <iosfwd>
16 #include <boost/io/ios_state.hpp>
17
18 #include <zypp/base/Flags.h>
19 #include <zypp/base/PtrTypes.h>
20 #include <zypp/base/Function.h>
21 #include <zypp/base/NonCopyable.h>
22
24 namespace zypp
25 {
27
29 namespace iostr
30 {
31
35 typedef boost::io::ios_base_all_saver IosFmtFlagsSaver;
36
37
45 std::string getline( std::istream & str );
46
50 inline std::ostream & copy( std::istream & from_r, std::ostream & to_r )
51 {
52 if ( from_r && to_r )
53 {
54 char ch;
55 while ( from_r && from_r.get( ch ) )
56 to_r.put( ch );
57 }
58 return to_r;
59 }
60
64 inline std::ostream & copyIndent( std::istream & from_r, std::ostream & to_r, const std::string & indent_r = "> " )
65 {
66 if ( from_r && to_r )
67 {
68 char ch;
69 bool indent = true;
70 while ( from_r && from_r.get( ch ) )
71 {
72 if ( indent )
73 to_r << indent_r;
74 indent = ( ch == '\n' );
75 to_r.put( ch );
76 }
77 }
78 return to_r;
79 }
80
84 inline void tee( std::istream & from_r, std::ostream & to1_r, std::ostream & to2_r )
85 {
86 if ( from_r && ( to1_r ||to2_r ) )
87 {
88 char ch;
89 while ( from_r && from_r.get( ch ) )
90 {
91 to1_r.put( ch );
92 to2_r.put( ch );
93 }
94 }
95 }
96
98 //
99 // CLASS NAME : EachLine
100 //
111 class EachLine : private base::NonCopyable
112 {
113 public:
115 EachLine( std::istream & str_r, unsigned lineNo_r = 0 );
116
118 bool valid() const
119 { return _valid; }
120
122 explicit operator bool() const
123 { return _valid; }
124
126 unsigned lineNo() const
127 { return _lineNo; }
128
129 std::streamoff lineStart() const
130 { return _lineStart; };
131
133 void setLineNo( unsigned lineNo_r )
134 { _lineNo = lineNo_r; }
135
137 const std::string & operator*() const
138 { return _line; }
140 std::string & operator*()
141 { return _line; }
142
144 const std::string * operator->() const
145 { return &_line; }
146
148 bool next();
149
151 bool next( unsigned num_r )
152 {
153 while ( num_r-- && next() )
154 ; /* EMPTY */
155 return valid();
156 }
157
158 private:
159 std::istream & _str;
160 std::string _line;
161 std::streamoff _lineStart;
162 unsigned _lineNo;
163 bool _valid;
164 };
166
185 int forEachLine( std::istream & str_r, function<bool(int, std::string)> consume_r );
186
188 enum ParseFlag
189 {
190 PF_LTRIM = 1 << 0, //< left trim whitespace
191 PF_RTRIM = 1 << 1, //< right trim whitespace
192 PF_TRIM = PF_LTRIM | PF_RTRIM, //< trim whitespace
193 PF_SKIP_EMPTY = 1 << 2, //< skip lines containing whitespace only
194 PF_SKIP_SHARP_COMMENT = 1 << 3 //< skip lines beginning with '#'
195 };
196 ZYPP_DECLARE_FLAGS( ParseFlags, ParseFlag );
197 ZYPP_DECLARE_OPERATORS_FOR_FLAGS( ParseFlags );
198
200 int simpleParseFile( std::istream & str_r, ParseFlags flags_r, function<bool(int, std::string)> consume_r );
201
203 inline int simpleParseFile( std::istream & str_r, function<bool(int, std::string)> consume_r )
204 { return simpleParseFile( str_r, PF_TRIM | PF_SKIP_EMPTY | PF_SKIP_SHARP_COMMENT , consume_r ); }
205
207 } // namespace iostr
210 } // namespace zypp
212 #endif // ZYPP_BASE_IOSTREAM_H
Flags.h
Function.h
NonCopyable.h
PtrTypes.h
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:112
zypp::iostr::EachLine::next
bool next(unsigned num_r)
Advance num_r lines.
Definition: IOStream.h:151
zypp::iostr::EachLine::_str
std::istream & _str
Definition: IOStream.h:159
zypp::iostr::EachLine::setLineNo
void setLineNo(unsigned lineNo_r)
Set current line number.
Definition: IOStream.h:133
zypp::iostr::EachLine::_line
std::string _line
Definition: IOStream.h:160
zypp::iostr::EachLine::lineStart
std::streamoff lineStart() const
Definition: IOStream.h:129
zypp::iostr::EachLine::operator->
const std::string * operator->() const
Access the current line.
Definition: IOStream.h:144
zypp::iostr::EachLine::lineNo
unsigned lineNo() const
Return the current line number.
Definition: IOStream.h:126
zypp::iostr::EachLine::valid
bool valid() const
Whether this contains a valid line to consume.
Definition: IOStream.h:118
zypp::iostr::EachLine::operator*
std::string & operator*()
Definition: IOStream.h:140
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition: IOStream.cc:72
zypp::iostr::EachLine::operator*
const std::string & operator*() const
Access the current line.
Definition: IOStream.h:137
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:163
zypp::iostr::EachLine::_lineStart
std::streamoff _lineStart
Definition: IOStream.h:161
zypp::iostr::EachLine::_lineNo
unsigned _lineNo
Definition: IOStream.h:162
str
String related utilities and Regular expression matching.
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
zypp::iostr::copy
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition: IOStream.h:50
zypp::iostr::ParseFlag
ParseFlag
simpleParseFile modifications before consuming a line.
Definition: IOStream.h:189
zypp::iostr::PF_SKIP_EMPTY
@ PF_SKIP_EMPTY
Definition: IOStream.h:193
zypp::iostr::PF_RTRIM
@ PF_RTRIM
Definition: IOStream.h:191
zypp::iostr::PF_TRIM
@ PF_TRIM
Definition: IOStream.h:192
zypp::iostr::PF_SKIP_SHARP_COMMENT
@ PF_SKIP_SHARP_COMMENT
Definition: IOStream.h:194
zypp::iostr::PF_LTRIM
@ PF_LTRIM
Definition: IOStream.h:190
zypp::iostr::tee
void tee(std::istream &from_r, std::ostream &to1_r, std::ostream &to2_r)
Copy istream to ostream, prefixing each line with indent_r (default "> " ).
Definition: IOStream.h:84
zypp::iostr::ZYPP_DECLARE_OPERATORS_FOR_FLAGS
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(ParseFlags)
zypp::iostr::forEachLine
int forEachLine(std::istream &str_r, function< bool(int, std::string)> consume_r)
Simple lineparser: Call functor consume_r for each line.
Definition: IOStream.cc:100
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::IosFmtFlagsSaver
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition: IOStream.h:35
zypp::iostr::copyIndent
std::ostream & copyIndent(std::istream &from_r, std::ostream &to_r, const std::string &indent_r="> ")
Copy istream to ostream, prefixing each line with indent_r (default "> " ).
Definition: IOStream.h:64
zypp::iostr::getline
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
zypp::iostr::ZYPP_DECLARE_FLAGS
ZYPP_DECLARE_FLAGS(ParseFlags, ParseFlag)
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2