libzypp  13.10.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 
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 {
26 
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 
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
bool next()
Advance to next line.
Definition: IOStream.cc:72
bool valid() const
Whether this contains a valid line to consume.
Definition: IOStream.h:118
const std::string & operator*() const
Access the current line.
Definition: IOStream.h:137
std::streamoff lineStart() const
Definition: IOStream.h:129
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 &quot;&gt; &quot; ).
Definition: IOStream.h:64
const std::string * operator->() const
Access the current line.
Definition: IOStream.h:144
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition: IOStream.h:35
unsigned lineNo() const
Return the current line number.
Definition: IOStream.h:126
std::string _line
Definition: IOStream.h:160
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
std::streamoff _lineStart
Definition: IOStream.h:161
std::string & operator*()
Definition: IOStream.h:140
void setLineNo(unsigned lineNo_r)
Set current line number.
Definition: IOStream.h:133
ParseFlag
simpleParseFile modifications before consuming a line.
Definition: IOStream.h:188
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
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::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
std::istream & _str
Definition: IOStream.h:159
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
bool next(unsigned num_r)
Advance num_r lines.
Definition: IOStream.h:151
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(ParseFlags)
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition: IOStream.h:50
ZYPP_DECLARE_FLAGS(ParseFlags, ParseFlag)
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 &quot;&gt; &quot; ).
Definition: IOStream.h:84
EachLine(std::istream &str_r, unsigned lineNo_r=0)
Ctor taking a stream and reading the 1st line from it.
Definition: IOStream.cc:58