libzypp  11.13.5
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/SafeBool.h"
21 #include "zypp/base/Function.h"
22 #include "zypp/base/NonCopyable.h"
23 
25 namespace zypp
26 {
27 
28 
30  namespace iostr
31  {
32 
36  typedef boost::io::ios_base_all_saver IosFmtFlagsSaver;
37 
38 
46  std::string getline( std::istream & str );
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;
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;
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;
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 EachLine : private base::SafeBool<EachLine>, private base::NonCopyable
113  {
115 
116  public:
118  EachLine( std::istream & str_r, unsigned lineNo_r = 0 );
119 
121  using SafeBool::operator bool_type;
122 
124  bool valid() const
125  { return boolTest(); }
126 
128  unsigned lineNo() const
129  { return _lineNo; }
130 
131  std::streamoff lineStart() const
132  { return _lineStart; };
133 
135  void setLineNo( unsigned lineNo_r )
136  { _lineNo = lineNo_r; }
137 
139  const std::string & operator*() const
140  { return _line; }
142  std::string & operator*()
143  { return _line; }
144 
146  const std::string * operator->() const
147  { return &_line; }
148 
150  bool next();
151 
153  bool next( unsigned num_r )
154  {
155  while ( num_r-- && next() )
156  ; /* EMPTY */
157  return valid();
158  }
159 
160  private:
161  friend SafeBool::operator bool_type() const;
162  bool boolTest() const
163  { return _valid; }
164 
165  private:
166  std::istream & _str;
167  std::string _line;
168  std::streamoff _lineStart;
169  unsigned _lineNo;
170  bool _valid;
171  };
173 
192  int forEachLine( std::istream & str_r, function<bool(int, std::string)> consume_r );
193 
196  {
197  PF_LTRIM = 1 << 0, //< left trim whitespace
198  PF_RTRIM = 1 << 1, //< right trim whitespace
199  PF_TRIM = PF_LTRIM | PF_RTRIM, //< trim whitespace
200  PF_SKIP_EMPTY = 1 << 2, //< skip lines containing whitespace only
201  PF_SKIP_SHARP_COMMENT = 1 << 3 //< skip lines beginning with '#'
202  };
203  ZYPP_DECLARE_FLAGS( ParseFlags, ParseFlag );
204  ZYPP_DECLARE_OPERATORS_FOR_FLAGS( ParseFlags );
205 
207  int simpleParseFile( std::istream & str_r, ParseFlags flags_r, function<bool(int, std::string)> consume_r );
208 
210  inline int simpleParseFile( std::istream & str_r, function<bool(int, std::string)> consume_r )
211  { return simpleParseFile( str_r, PF_TRIM | PF_SKIP_EMPTY | PF_SKIP_SHARP_COMMENT , consume_r ); }
212 
214  } // namespace iostr
217 } // namespace zypp
219 #endif // ZYPP_BASE_IOSTREAM_H