extend line parser to retrieve file position
[platform/upstream/libzypp.git] / zypp / base / IOStream.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/IOStream.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/base/IOStream.h"
16
17 using std::endl;
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22   ///////////////////////////////////////////////////////////////////
23   namespace iostr
24   { /////////////////////////////////////////////////////////////////
25
26     /******************************************************************
27      **
28      ** FUNCTION NAME : getline
29      ** FUNCTION TYPE : std::string
30      */
31     std::string getline( std::istream & str )
32     {
33       static const unsigned tmpBuffLen = 1024;
34       static char           tmpBuff[tmpBuffLen];
35       std::string ret;
36       do {
37         str.clear();
38         str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
39         ret += tmpBuff;
40       } while( str.rdstate() == std::ios::failbit );
41
42       return ret;
43     }
44
45     ///////////////////////////////////////////////////////////////////
46     //
47     //  CLASS NAME : EachLine
48     //
49     ///////////////////////////////////////////////////////////////////
50
51     ///////////////////////////////////////////////////////////////////
52     //
53     //  METHOD NAME : EachLine::EachLine
54     //  METHOD TYPE : Ctor
55     //
56     EachLine::EachLine( std::istream & str_r, unsigned lineNo_r )
57       : _str( str_r )
58       , _lineStart( -1 )
59       , _lineNo( lineNo_r )
60       , _valid( true )
61     {
62       next();
63     }
64
65     ///////////////////////////////////////////////////////////////////
66     //
67     //  METHOD NAME : EachLine::next
68     //  METHOD TYPE : bool
69     //
70     bool EachLine::next()
71     {
72       if ( ! _valid )
73       {
74         return false;
75       }
76
77       if ( ! _str ) // usg: saw EOF in previous read
78       {
79         _line.clear();
80         return(_valid = false);
81       }
82
83       _lineStart = _str.tellg();
84       _line = iostr::getline( _str );
85       ++_lineNo;
86       if ( _str.fail() || _str.bad() )
87       {
88         _line.clear();
89         return(_valid = false);
90       }
91       return(_valid = true);
92     }
93
94     /////////////////////////////////////////////////////////////////
95   } // namespace iostr
96   ///////////////////////////////////////////////////////////////////
97   /////////////////////////////////////////////////////////////////
98 } // namespace zypp
99 ///////////////////////////////////////////////////////////////////