IOStream.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00012 #ifndef ZYPP_BASE_IOSTREAM_H
00013 #define ZYPP_BASE_IOSTREAM_H
00014
00015 #include <iosfwd>
00016 #include <boost/io/ios_state.hpp>
00017
00018 #include "zypp/base/PtrTypes.h"
00019 #include <zypp/base/SafeBool.h>
00020 #include <zypp/base/NonCopyable.h>
00021
00023 namespace zypp
00024 {
00025
00026
00028 namespace iostr
00029 {
00030
00034 typedef boost::io::ios_base_all_saver IosFmtFlagsSaver;
00035
00036
00044 std::string getline( std::istream & str );
00045
00049 inline std::ostream & copy( std::istream & from_r, std::ostream & to_r )
00050 {
00051 if ( from_r && to_r )
00052 {
00053 char ch;
00054 while ( from_r && from_r.get( ch ) )
00055 to_r.put( ch );
00056 }
00057 return to_r;
00058 }
00059
00063 inline std::ostream & copyIndent( std::istream & from_r, std::ostream & to_r, const std::string & indent_r = "> " )
00064 {
00065 if ( from_r && to_r )
00066 {
00067 char ch;
00068 bool indent = true;
00069 while ( from_r && from_r.get( ch ) )
00070 {
00071 if ( indent )
00072 to_r << indent_r;
00073 indent = ( ch == '\n' );
00074 to_r.put( ch );
00075 }
00076 }
00077 return to_r;
00078 }
00079
00083 inline void tee( std::istream & from_r, std::ostream & to1_r, std::ostream & to2_r )
00084 {
00085 if ( from_r && ( to1_r ||to2_r ) )
00086 {
00087 char ch;
00088 while ( from_r && from_r.get( ch ) )
00089 {
00090 to1_r.put( ch );
00091 to2_r.put( ch );
00092 }
00093 }
00094 }
00095
00097
00098
00099
00110 class EachLine : private base::SafeBool<EachLine>, private base::NonCopyable
00111 {
00112 typedef base::SafeBool<EachLine> SafeBool;
00113
00114 public:
00116 EachLine( std::istream & str_r, unsigned lineNo_r = 0 );
00117
00119 using SafeBool::operator bool_type;
00120
00122 bool valid() const
00123 { return boolTest(); }
00124
00126 unsigned lineNo() const
00127 { return _lineNo; }
00128
00129 std::streamoff lineStart() const
00130 { return _lineStart; };
00131
00133 void setLineNo( unsigned lineNo_r )
00134 { _lineNo = lineNo_r; }
00135
00137 const std::string & operator*() const
00138 { return _line; }
00139
00141 const std::string * operator->() const
00142 { return &_line; }
00143
00145 bool next();
00146
00148 bool next( unsigned num_r )
00149 {
00150 while ( num_r-- && next() )
00151 ;
00152 return valid();
00153 }
00154
00155 private:
00156 friend SafeBool::operator bool_type() const;
00157 bool boolTest() const
00158 { return _valid; }
00159
00160 private:
00161 std::istream & _str;
00162 std::string _line;
00163 std::streamoff _lineStart;
00164 unsigned _lineNo;
00165 bool _valid;
00166 };
00168
00190 template<class _Function>
00191 _Function & forEachLine( std::istream & str_r, _Function & consume_r )
00192 {
00193 while ( str_r )
00194 {
00195 std::string l = getline( str_r );
00196 if ( ! (str_r.fail() || str_r.bad()) )
00197 {
00198
00199 if ( ! consume_r( l ) )
00200 break;
00201 }
00202 }
00203 return consume_r;
00204 }
00205
00207 }
00210 }
00212 #endif // ZYPP_BASE_IOSTREAM_H