textwolf  0.2
sourceiterator.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_SOURCE_ITERATOR_HPP__
12 #define __TEXTWOLF_SOURCE_ITERATOR_HPP__
13 #include "textwolf/exception.hpp"
14 #include "textwolf/position.hpp"
15 #include <cstdlib>
16 #include <stdexcept>
17 #include <setjmp.h>
18 
21 namespace textwolf {
22 
26  :public throws_exception
27 {
28 public:
31  :m_start(0)
32  ,m_itr(0)
33  ,m_end(0)
34  ,m_eom(0)
35  ,m_abspos(0){}
36 
40  :m_start(o.m_start)
41  ,m_itr(o.m_itr)
42  ,m_end(o.m_end)
43  ,m_eom(o.m_eom)
44  ,m_abspos(o.m_abspos){}
45 
50  SrcIterator( const char* buf, std::size_t size, jmp_buf* eom_=0)
51  :m_start(const_cast<char*>(buf))
52  ,m_itr(const_cast<char*>(buf))
53  ,m_end(m_itr+size)
54  ,m_eom(eom_)
55  ,m_abspos(0){}
56 
59  {
60  m_start = o.m_start;
61  m_itr = o.m_itr;
62  m_end = o.m_end;
63  m_eom = o.m_eom;
64  m_abspos = o.m_abspos;
65  return *this;
66  }
67 
69  inline char operator*()
70  {
71  if (m_itr >= m_end)
72  {
73  if (m_eom) longjmp(*m_eom,1);
74  return 0;
75  }
76  return *m_itr;
77  }
78 
81  {
82  ++m_itr;
83  return *this;
84  }
85 
87  inline std::size_t operator-( const SrcIterator& b) const
88  {
89  if (b.m_end != m_end || m_itr < b.m_itr) throw exception( IllegalParam);
90  return m_itr - b.m_itr;
91  }
92 
97  void putInput( const char* buf, std::size_t size, jmp_buf* eom=0)
98  {
99  m_abspos += size;
100  m_start = m_itr = const_cast<char*>(buf);
101  m_end = m_itr+size;
102  m_eom = eom;
103  }
104 
107  std::size_t getPosition() const
108  {
109  return (m_end >= m_itr)?(m_itr-m_start):0;
110  }
111 
113  {
114  return m_abspos - (m_end - m_itr);
115  }
116 
117  bool endOfChunk() const
118  {
119  return (m_itr >= m_end);
120  }
121 
122 private:
123  char* m_start;
124  char* m_itr;
125  char* m_end;
126  jmp_buf* m_eom;
127  PositionIndex m_abspos;
128 };
129 
130 }//namespace
131 #endif
132 
133 
SrcIterator(const char *buf, std::size_t size, jmp_buf *eom_=0)
Constructor.
Definition: sourceiterator.hpp:50
SrcIterator & operator=(const SrcIterator &o)
Assingment operator.
Definition: sourceiterator.hpp:58
SrcIterator & operator++()
Prefix increment operator (required by textwolf for an input iterator)
Definition: sourceiterator.hpp:80
Input iterator as source for the XML scanner with the possibility of being fed chunk by chunk...
Definition: sourceiterator.hpp:25
Base class for structures that can throw exceptions for non recoverable errors.
Definition: exception.hpp:20
PositionIndex position() const
Definition: sourceiterator.hpp:112
char operator*()
Element access operator (required by textwolf for an input iterator)
Definition: sourceiterator.hpp:69
SrcIterator(const SrcIterator &o)
Copy constructor.
Definition: sourceiterator.hpp:39
void putInput(const char *buf, std::size_t size, jmp_buf *eom=0)
Feed input to the source iterator.
Definition: sourceiterator.hpp:97
parameter check in automaton definition failed. Internal textwolf error
Definition: exception.hpp:33
textwolf exception class
Definition: exception.hpp:48
Definition of exceptions with containing error codes thrown by textwolf.
Definition of position number in source.
SrcIterator()
Empty constructor.
Definition: sourceiterator.hpp:30
bool endOfChunk() const
Definition: sourceiterator.hpp:117
std::size_t getPosition() const
Get the current position in the current chunk parsed.
Definition: sourceiterator.hpp:107
uint64_t PositionIndex
Definition: position.hpp:34
std::size_t operator-(const SrcIterator &b) const
Get the iterator difference in bytes.
Definition: sourceiterator.hpp:87