textwolf  0.2
istreamiterator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 
11 #ifndef __TEXTWOLF_ISTREAM_ITERATOR_HPP__
12 #define __TEXTWOLF_ISTREAM_ITERATOR_HPP__
13 #include "textwolf/exception.hpp"
14 #include "textwolf/position.hpp"
15 #include <iostream>
16 #include <fstream>
17 #include <iterator>
18 #include <cstdlib>
19 #include <cerrno>
20 #include <cstring>
21 #include <stdexcept>
22 #include <stdint.h>
23 
26 namespace textwolf {
27 
30 class IStream
31 {
32 public:
33  virtual ~IStream(){}
34  virtual std::size_t read( void* buf, std::size_t bufsize)=0;
35  virtual int errorcode() const=0;
36 };
37 
41  :public IStream
42 {
43 public:
44  StdInputStream( std::istream& input_)
45  :m_input(&input_),m_errno(0)
46  {
47  m_input->unsetf( std::ios::skipws);
48  m_input->exceptions ( std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit );
49  }
51  :m_input(o.m_input),m_errno(o.m_errno){}
52 
53  virtual ~StdInputStream(){}
54  virtual std::size_t read( void* buf, std::size_t bufsize)
55  {
56  try
57  {
58  m_errno = 0;
59  m_input->read( (char*)buf, bufsize);
60  return m_input->gcount();
61  }
62  catch (const std::istream::failure& err)
63  {
64  if (m_input->eof())
65  {
66  m_errno = 0;
67  return m_input->gcount();
68  }
69  else
70  {
71  m_errno = errno;
72  return 0;
73  }
74  }
75  catch (...)
76  {
77  m_errno = errno;
78  return 0;
79  }
80  }
81 
82  virtual int errorcode() const
83  {
84  return m_errno;
85  }
86 
87 private:
88  std::istream* m_input;
89  int m_errno;
90 };
91 
92 
96  :public throws_exception
97 {
98 public:
103  {
104  std::free(m_buf);
105  }
106 
109  IStreamIterator( IStream* input, std::size_t bufsize=8192)
110  :m_input(input),m_buf((char*)std::malloc(bufsize)),m_bufsize(bufsize),m_readsize(0),m_readpos(0),m_abspos(0)
111  {
112  if (!m_buf) throw std::bad_alloc();
113  fillbuf();
114  }
115 
119  :m_input(o.m_input),m_buf((char*)std::malloc(o.m_bufsize)),m_bufsize(o.m_bufsize),m_readsize(o.m_readsize),m_readpos(o.m_readpos),m_abspos(o.m_abspos)
120  {
121  if (!m_buf) throw std::bad_alloc();
122  std::memcpy( m_buf, o.m_buf, o.m_readsize);
123  }
124 
127  inline char operator* ()
128  {
129  return (m_readpos < m_readsize)?m_buf[m_readpos]:0;
130  }
131 
134  {
135  if (m_readpos+1 >= m_readsize)
136  {
137  fillbuf();
138  }
139  else
140  {
141  ++m_readpos;
142  }
143  return *this;
144  }
145 
146  int operator - (const IStreamIterator& o) const
147  {
148  return (int)m_readpos - o.m_readpos;
149  }
150 
152  {
153  return m_abspos + m_readpos;
154  }
155 
156 private:
157  bool fillbuf()
158  {
159  m_abspos += m_readsize;
160  m_readsize = m_input->read( m_buf, m_bufsize);
161  m_readpos = 0;
162  if (m_input->errorcode()) throw exception( FileReadError);
163  return true;
164  }
165 
166 private:
167  IStream* m_input;
168  char* m_buf;
169  std::size_t m_bufsize;
170  std::size_t m_readsize;
171  std::size_t m_readpos;
172  PositionIndex m_abspos;
173 };
174 
175 }//namespace
176 #endif
Input stream implementation based on std::istream.
Definition: istreamiterator.hpp:40
~IStreamIterator()
Destructor.
Definition: istreamiterator.hpp:102
PositionIndex position() const
Definition: istreamiterator.hpp:151
Base class for structures that can throw exceptions for non recoverable errors.
Definition: exception.hpp:20
Input stream interface.
Definition: istreamiterator.hpp:30
IStreamIterator & operator++()
Pre increment.
Definition: istreamiterator.hpp:133
StdInputStream(const StdInputStream &o)
Definition: istreamiterator.hpp:50
StdInputStream(std::istream &input_)
Definition: istreamiterator.hpp:44
virtual std::size_t read(void *buf, std::size_t bufsize)=0
virtual int errorcode() const =0
textwolf exception class
Definition: exception.hpp:48
IStreamIterator()
Default constructor.
Definition: istreamiterator.hpp:100
int operator-(const IStreamIterator &o) const
Definition: istreamiterator.hpp:146
virtual ~IStream()
Definition: istreamiterator.hpp:33
virtual ~StdInputStream()
Definition: istreamiterator.hpp:53
error reading a file. System error
Definition: exception.hpp:38
Definition of exceptions with containing error codes thrown by textwolf.
IStreamIterator(const IStreamIterator &o)
Copy constructor.
Definition: istreamiterator.hpp:118
Input iterator on an STL input stream.
Definition: istreamiterator.hpp:95
Definition of position number in source.
virtual int errorcode() const
Definition: istreamiterator.hpp:82
uint64_t PositionIndex
Definition: position.hpp:34
IStreamIterator(IStream *input, std::size_t bufsize=8192)
Constructor.
Definition: istreamiterator.hpp:109
char operator*()
Element access.
Definition: istreamiterator.hpp:127
virtual std::size_t read(void *buf, std::size_t bufsize)
Definition: istreamiterator.hpp:54