libzypp 17.31.23
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-core/base/Flags.h>
19#include <zypp-core/base/PtrTypes.h>
20#include <zypp-core/base/Function.h>
21#include <zypp-core/base/NonCopyable.h>
22
24namespace 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 );
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
std::istream & _str
Definition: IOStream.h:159
void setLineNo(unsigned lineNo_r)
Set current line number.
Definition: IOStream.h:133
std::string _line
Definition: IOStream.h:160
std::streamoff lineStart() const
Definition: IOStream.h:129
const std::string * operator->() const
Access the current line.
Definition: IOStream.h:144
unsigned lineNo() const
Return the current line number.
Definition: IOStream.h:126
bool valid() const
Whether this contains a valid line to consume.
Definition: IOStream.h:118
bool next()
Advance to next line.
Definition: IOStream.cc:72
const std::string & operator*() const
Access the current line.
Definition: IOStream.h:137
EachLine(std::istream &str_r, unsigned lineNo_r=0)
Ctor taking a stream and reading the 1st line from it.
Definition: IOStream.cc:58
std::streamoff _lineStart
Definition: IOStream.h:161
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
ParseFlag
simpleParseFile modifications before consuming a line.
Definition: IOStream.h:189
@ PF_SKIP_EMPTY
Definition: IOStream.h:193
@ PF_SKIP_SHARP_COMMENT
Definition: IOStream.h:194
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
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
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
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
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition: IOStream.h:35
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition: IOStream.h:50
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
Definition: Flags.h:177
#define ZYPP_DECLARE_FLAGS(Name, Enum)
Definition: Flags.h:174