1 ///###////////////////////////////////////////////////////////////////////////
2 //
3 // Burton Computer Corporation
4 // http://www.burton-computer.com
5 // http://www.cooldevtools.com
6 // $Id: IstreamCharReader.cc 272 2007-01-06 19:37:27Z brian $
7 //
8 // Copyright (C) 2007 Burton Computer Corporation
9 // ALL RIGHTS RESERVED
10 //
11 // This program is open source software; you can redistribute it
12 // and/or modify it under the terms of the Q Public License (QPL)
13 // version 1.0. Use of this software in whole or in part, including
14 // linking it (modified or unmodified) into other programs is
15 // subject to the terms of the QPL.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // Q Public License for more details.
21 //
22 // You should have received a copy of the Q Public License
23 // along with this program; see the file LICENSE.txt.  If not, visit
24 // the Burton Computer Corporation or CoolDevTools web site
25 // QPL pages at:
26 //
27 //    http://www.burton-computer.com/qpl.html
28 //    http://www.cooldevtools.com/qpl.html
29 //
30 
31 #include "IstreamCharReader.h"
32 
33 class IstreamReaderPosition : public AbstractCharReaderPosition
34 {
35 public:
IstreamReaderPosition(streampos _position)36   IstreamReaderPosition(streampos _position) : position(_position)
37   {
38   }
39 
~IstreamReaderPosition()40   virtual ~IstreamReaderPosition()
41   {
42   }
43 
44   streampos position;
45 };
46 
IstreamCharReader(istream * stream,bool can_seek)47 IstreamCharReader::IstreamCharReader(istream *stream,
48                                      bool can_seek)
49   : m_stream(stream),
50     m_rdbuf(stream->rdbuf()),
51     m_canSeek(can_seek)
52 {
53 }
54 
~IstreamCharReader()55 IstreamCharReader::~IstreamCharReader()
56 {
57 }
58 
forward()59 bool IstreamCharReader::forward()
60 {
61   int ichr = m_rdbuf->sbumpc();
62   if (ichr == EOF) {
63     return false;
64   }
65   setCurrentChar(safe_char(ichr));
66   return true;
67 }
68 
hasChar()69 bool IstreamCharReader::hasChar()
70 {
71   return m_rdbuf->sgetc() != EOF;
72 }
73 
atEnd()74 bool IstreamCharReader::atEnd()
75 {
76   return m_rdbuf->sgetc() == EOF;
77 }
78 
skip(int nchars)79 bool IstreamCharReader::skip(int nchars)
80 {
81   while (nchars-- > 0) {
82     if (!forward()) {
83       return false;
84     }
85   }
86   return true;
87 }
88 
createMark()89 OWNED AbstractCharReaderPosition *IstreamCharReader::createMark()
90 {
91   return m_canSeek ? new IstreamReaderPosition(m_rdbuf->pubseekoff(0,ios::cur,ios::in)) : 0;
92 }
93 
returnToMark(AbstractCharReaderPosition * pos)94 void IstreamCharReader::returnToMark(AbstractCharReaderPosition *pos)
95 {
96   if (m_canSeek && pos) {
97     m_rdbuf->pubseekpos(dynamic_cast<IstreamReaderPosition*>(pos)->position);
98   }
99 }
100