libzypp  10.5.0
IOStream.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 //#include "zypp/base/Logger.h"
00014 
00015 #include "zypp/base/IOStream.h"
00016 
00017 using std::endl;
00018 
00020 namespace zypp
00021 { 
00022 
00023   namespace iostr
00024   { 
00025 
00026     /******************************************************************
00027      **
00028      ** FUNCTION NAME : getline
00029      ** FUNCTION TYPE : std::string
00030      */
00031     std::string getline( std::istream & str )
00032     {
00033       static const unsigned tmpBuffLen = 1024;
00034       static char           tmpBuff[tmpBuffLen];
00035       std::string ret;
00036       do {
00037         str.clear();
00038         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
00039         ret += tmpBuff;
00040       } while( str.rdstate() == std::ios::failbit );
00041 
00042       return ret;
00043     }
00044 
00046     //
00047     //  CLASS NAME : EachLine
00048     //
00050 
00052     //
00053     //  METHOD NAME : EachLine::EachLine
00054     //  METHOD TYPE : Ctor
00055     //
00056     EachLine::EachLine( std::istream & str_r, unsigned lineNo_r )
00057       : _str( str_r )
00058       , _lineStart( -1 )
00059       , _lineNo( lineNo_r )
00060       , _valid( true )
00061     {
00062       next();
00063     }
00064 
00066     //
00067     //  METHOD NAME : EachLine::next
00068     //  METHOD TYPE : bool
00069     //
00070     bool EachLine::next()
00071     {
00072       if ( ! _valid )
00073       {
00074         return false;
00075       }
00076 
00077       if ( ! _str ) // usg: saw EOF in previous read
00078       {
00079         _line.clear();
00080         return(_valid = false);
00081       }
00082 
00083       _lineStart = _str.tellg();
00084       _line = iostr::getline( _str );
00085       ++_lineNo;
00086       if ( _str.fail() || _str.bad() )
00087       {
00088         _line.clear();
00089         return(_valid = false);
00090       }
00091       return(_valid = true);
00092     }
00093 
00095   } // namespace iostr
00098 } // namespace zypp