libzypp: zypp-core/base/IOStream.h Source File

libzypp 17.38.6

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
19#include <zypp-core/base/Flags.h>
20#include <zypp-core/base/PtrTypes.h>
21#include <zypp-core/base/Function.h>
22#include <zypp-core/base/NonCopyable.h>
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 //
112 class ZYPP_API EachLine : private base::NonCopyable
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
189 enum ParseFlag
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 };
197 ZYPP_DECLARE_FLAGS( ParseFlags, ParseFlag );
198 ZYPP_DECLARE_OPERATORS_FOR_FLAGS( ParseFlags );
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
Flags.h
ZYPP_DECLARE_OPERATORS_FOR_FLAGS
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
relates: Flags
Definition Flags.h:177
ZYPP_DECLARE_FLAGS
#define ZYPP_DECLARE_FLAGS(Name, Enum)
relates: Flags
Definition Flags.h:174
Function.h
NonCopyable.h
PtrTypes.h
bool
zypp::iostr::EachLine::next
bool next(unsigned num_r)
Advance num_r lines.
Definition IOStream.h:152
zypp::iostr::EachLine::_str
std::istream & _str
Definition IOStream.h:160
zypp::iostr::EachLine::setLineNo
void setLineNo(unsigned lineNo_r)
Set current line number.
Definition IOStream.h:134
zypp::iostr::EachLine::_line
std::string _line
Definition IOStream.h:161
zypp::iostr::EachLine::lineStart
std::streamoff lineStart() const
Definition IOStream.h:130
zypp::iostr::EachLine::operator*
std::string & operator*()
Definition IOStream.h:141
zypp::iostr::EachLine::operator->
const std::string * operator->() const
Access the current line.
Definition IOStream.h:145
zypp::iostr::EachLine::lineNo
unsigned lineNo() const
Return the current line number.
Definition IOStream.h:127
zypp::iostr::EachLine::valid
bool valid() const
Whether this contains a valid line to consume.
Definition IOStream.h:119
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:138
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::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::iostr
Iostream related utilities.
Definition IOStream.cc:26
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::ParseFlag
ParseFlag
simpleParseFile modifications before consuming a line.
Definition IOStream.h:190
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::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:85
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:65
zypp::iostr::IosFmtFlagsSaver
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition IOStream.h:36
zypp::iostr::getline
std::string getline(std::istream &str)
Read one line from stream.
Definition IOStream.cc:33
zypp::iostr::copy
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition IOStream.h:51
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173