libzypp 17.37.17
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#include <utility>
18
23
25namespace zypp
26{
28
30 namespace iostr
31 {
32
36 using IosFmtFlagsSaver = boost::io::ios_base_all_saver;
37
38
46 std::string getline( std::istream & str ) ZYPP_API;
47
51 inline std::ostream & copy( std::istream & from_r, std::ostream & to_r )
52 {
53 if ( from_r && to_r )
54 {
55 char ch = 0;
56 while ( from_r && from_r.get( ch ) )
57 to_r.put( ch );
58 }
59 return to_r;
60 }
61
65 inline std::ostream & copyIndent( std::istream & from_r, std::ostream & to_r, const std::string & indent_r = "> " )
66 {
67 if ( from_r && to_r )
68 {
69 char ch = 0;
70 bool indent = true;
71 while ( from_r && from_r.get( ch ) )
72 {
73 if ( indent )
74 to_r << indent_r;
75 indent = ( ch == '\n' );
76 to_r.put( ch );
77 }
78 }
79 return to_r;
80 }
81
85 inline void tee( std::istream & from_r, std::ostream & to1_r, std::ostream & to2_r )
86 {
87 if ( from_r && ( to1_r ||to2_r ) )
88 {
89 char ch = 0;
90 while ( from_r && from_r.get( ch ) )
91 {
92 to1_r.put( ch );
93 to2_r.put( ch );
94 }
95 }
96 }
97
99 //
100 // CLASS NAME : EachLine
101 //
113 {
114 public:
116 EachLine( std::istream & str_r, unsigned lineNo_r = 0 );
117
119 bool valid() const
120 { return _valid; }
121
123 explicit operator bool() const
124 { return _valid; }
125
127 unsigned lineNo() const
128 { return _lineNo; }
129
130 std::streamoff lineStart() const
131 { return _lineStart; };
132
134 void setLineNo( unsigned lineNo_r )
135 { _lineNo = lineNo_r; }
136
138 const std::string & operator*() const
139 { return _line; }
140
141 std::string & operator*()
142 { return _line; }
143
145 const std::string * operator->() const
146 { return &_line; }
147
149 bool next();
150
152 bool next( unsigned num_r )
153 {
154 while ( num_r-- && next() )
155 ; /* EMPTY */
156 return valid();
157 }
158
159 private:
160 std::istream & _str;
161 std::string _line;
162 std::streamoff _lineStart;
163 unsigned _lineNo;
164 bool _valid;
165 };
166
167
186 int forEachLine( std::istream & str_r, const function<bool(int, std::string)>& consume_r ) ZYPP_API;
187
190 {
191 PF_LTRIM = 1 << 0, //< left trim whitespace
192 PF_RTRIM = 1 << 1, //< right trim whitespace
193 PF_TRIM = PF_LTRIM | PF_RTRIM, //< trim whitespace
194 PF_SKIP_EMPTY = 1 << 2, //< skip lines containing whitespace only
195 PF_SKIP_SHARP_COMMENT = 1 << 3 //< skip lines beginning with '#'
196 };
199
201 int simpleParseFile( std::istream & str_r, ParseFlags flags_r, function<bool(int, std::string)> consume_r ) ZYPP_API;
202
204 inline int simpleParseFile( std::istream & str_r, function<bool(int, std::string)> consume_r )
205 { return simpleParseFile( str_r, PF_TRIM | PF_SKIP_EMPTY | PF_SKIP_SHARP_COMMENT , std::move(consume_r) ); }
206
208 } // namespace iostr
211} // namespace zypp
213#endif // ZYPP_BASE_IOSTREAM_H
bool next(unsigned num_r)
Advance num_r lines.
Definition IOStream.h:152
std::istream & _str
Definition IOStream.h:160
void setLineNo(unsigned lineNo_r)
Set current line number.
Definition IOStream.h:134
std::string _line
Definition IOStream.h:161
std::streamoff lineStart() const
Definition IOStream.h:130
std::string & operator*()
Definition IOStream.h:141
const std::string * operator->() const
Access the current line.
Definition IOStream.h:145
unsigned lineNo() const
Return the current line number.
Definition IOStream.h:127
bool valid() const
Whether this contains a valid line to consume.
Definition IOStream.h:119
bool next()
Advance to next line.
Definition IOStream.cc:72
const std::string & operator*() const
Access the current line.
Definition IOStream.h:138
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:162
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
Iostream related utilities.
Definition IOStream.cc:26
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
ParseFlag
simpleParseFile modifications before consuming a line.
Definition IOStream.h:190
@ PF_SKIP_SHARP_COMMENT
Definition IOStream.h:195
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
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:85
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:65
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition IOStream.h:36
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:51
Easy-to use interface to the ZYPP dependency resolver.
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
Definition Flags.h:177
#define ZYPP_DECLARE_FLAGS(Name, Enum)
Definition Flags.h:174