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 00047 // 00048 // CLASS NAME : EachLine 00049 // 00060 class EachLine : private base::SafeBool<EachLine>, private base::NonCopyable 00061 { 00062 typedef base::SafeBool<EachLine> SafeBool; 00063 00064 public: 00066 EachLine( std::istream & str_r, unsigned lineNo_r = 0 ); 00067 00069 using SafeBool::operator bool_type; 00070 00072 bool valid() const 00073 { return boolTest(); } 00074 00076 unsigned lineNo() const 00077 { return _lineNo; } 00078 00079 std::streamoff lineStart() const 00080 { return _lineStart; }; 00081 00083 void setLineNo( unsigned lineNo_r ) 00084 { _lineNo = lineNo_r; } 00085 00087 const std::string & operator*() const 00088 { return _line; } 00089 00091 bool next(); 00092 00094 bool next( unsigned num_r ) 00095 { 00096 while ( num_r-- && next() ) 00097 ; /* EMPTY */ 00098 return valid(); 00099 } 00100 00101 private: 00102 friend SafeBool::operator bool_type() const; 00103 bool boolTest() const 00104 { return _valid; } 00105 00106 private: 00107 std::istream & _str; 00108 std::string _line; 00109 std::streamoff _lineStart; 00110 unsigned _lineNo; 00111 bool _valid; 00112 }; 00114 00136 template<class _Function> 00137 _Function & forEachLine( std::istream & str_r, _Function & consume_r ) 00138 { 00139 while ( str_r ) 00140 { 00141 std::string l = getline( str_r ); 00142 if ( ! (str_r.fail() || str_r.bad()) ) 00143 { 00144 // l contains valid data to be consumed. 00145 if ( ! consume_r( l ) ) 00146 break; 00147 } 00148 } 00149 return consume_r; 00150 } 00151 00153 } // namespace iostr 00156 } // namespace zypp 00158 #endif // ZYPP_BASE_IOSTREAM_H
1.5.3