1
/*************************************************************************
4
** This file is part of dvisvgm -- the DVI to SVG converter **
5
** Copyright (C) 2005-2014 Martin Gieseking <martin.gieseking@uos.de> **
7
** This program is free software; you can redistribute it and/or **
8
** modify it under the terms of the GNU General Public License as **
9
** published by the Free Software Foundation; either version 3 of **
10
** the License, or (at your option) any later version. **
12
** This program is distributed in the hope that it will be useful, but **
13
** WITHOUT ANY WARRANTY; without even the implied warranty of **
14
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
15
** GNU General Public License for more details. **
17
** You should have received a copy of the GNU General Public License **
18
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
19
*************************************************************************/
21
#ifndef DVISVGM_INPUTBUFFER_H
22
#define DVISVGM_INPUTBUFFER_H
33
virtual ~InputBuffer () {}
34
virtual int get () =0;
35
virtual int peek () const =0;
36
virtual int peek (size_t n) const =0;
37
virtual bool eof () const =0;
38
virtual void invalidate () =0;
42
class StreamInputBuffer : public InputBuffer
45
StreamInputBuffer (std::istream &is, size_t bufsize=1024);
46
~StreamInputBuffer ();
49
int peek (size_t n) const;
50
bool eof () const {return pos() == _size1 && _size2 == 0;}
51
void invalidate () {_bufptr = _buf1+_size1; _size2 = 0;}
54
int fillBuffer (UInt8 *buf);
55
size_t pos () const {return _bufptr-_buf1;}
59
const size_t _bufsize; ///< maximal number of bytes each buffer can hold
60
UInt8 *_buf1; ///< pointer to first buffer
61
UInt8 *_buf2; ///< pointer to second buffer
62
size_t _size1; ///< number of bytes in buffer 1
63
size_t _size2; ///< number of bytes in buffer 2
64
UInt8 *_bufptr; ///< pointer to next byte to read
68
class StringInputBuffer : public InputBuffer
71
StringInputBuffer (const std::string &str) : _str(str), _pos(0) {}
72
int get () {return _pos < _str.length() ? _str[_pos++] : -1;}
73
int peek () const {return _pos < _str.length() ? _str[_pos] : -1;}
74
int peek (size_t n) const {return _pos+n < _str.length() ? _str[_pos+n] : -1;}
75
bool eof () const {return _pos >= _str.length();}
76
void invalidate () {_pos = _str.length();}
79
const std::string &_str;
84
class CharInputBuffer : public InputBuffer
87
CharInputBuffer (const char *buf, size_t size) : _pos(buf), _size(buf ? size : 0) {}
99
void assign (const char *buf, size_t size) {
104
void assign (const char *buf) {assign(buf, std::strlen(buf));}
105
int peek () const {return _size > 0 ? *_pos : -1;}
106
int peek (size_t n) const {return _size >= n ? _pos[n] : -1;}
107
bool eof () const {return _size == 0;}
108
void invalidate () {_size = 0;}
116
class SplittedCharInputBuffer : public InputBuffer
119
SplittedCharInputBuffer (const char *buf1, size_t s1, const char *buf2, size_t s2);
122
int peek (size_t n) const;
123
bool eof () const {return _size[_index] == 0;}
124
void invalidate () {_size[_index] = 0;}
133
class TextStreamInputBuffer : public StreamInputBuffer
136
TextStreamInputBuffer (std::istream &is) : StreamInputBuffer(is), _line(1), _col(1) {}
138
int line () const {return _line;}
139
int col () const {return _col;}