libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 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 // CLASS NAME : EachLine 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; } 00140 std::string & operator*() 00141 { return _line; } 00142 00144 const std::string * operator->() const 00145 { return &_line; } 00146 00148 bool next(); 00149 00151 bool next( unsigned num_r ) 00152 { 00153 while ( num_r-- && next() ) 00154 ; /* EMPTY */ 00155 return valid(); 00156 } 00157 00158 private: 00159 friend SafeBool::operator bool_type() const; 00160 bool boolTest() const 00161 { return _valid; } 00162 00163 private: 00164 std::istream & _str; 00165 std::string _line; 00166 std::streamoff _lineStart; 00167 unsigned _lineNo; 00168 bool _valid; 00169 }; 00171 00193 template<class _Function> 00194 _Function & forEachLine( std::istream & str_r, _Function & consume_r ) 00195 { 00196 while ( str_r ) 00197 { 00198 std::string l = getline( str_r ); 00199 if ( ! (str_r.fail() || str_r.bad()) ) 00200 { 00201 // l contains valid data to be consumed. 00202 if ( ! consume_r( l ) ) 00203 break; 00204 } 00205 } 00206 return consume_r; 00207 } 00208 00210 } // namespace iostr 00213 } // namespace zypp 00215 #endif // ZYPP_BASE_IOSTREAM_H