summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/InputBuffer.hpp
blob: 1474ce0a528bbcad527eace5c2ea87445ffaa5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*************************************************************************
** InputBuffer.hpp                                                      **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2020 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#ifndef INPUTBUFFER_HPP
#define INPUTBUFFER_HPP

#include <algorithm>
#include <cstring>
#include <istream>
#include <string>
#include <ostream>

struct InputBuffer {
	virtual ~InputBuffer () =default;
	virtual int get () =0;
	virtual int peek () const =0;
	virtual int peek (size_t n) const =0;
	virtual bool eof () const =0;
	virtual void invalidate () =0;
};


class StreamInputBuffer : public InputBuffer {
	public:
		explicit StreamInputBuffer (std::istream &is, size_t bufsize=1024);
		~StreamInputBuffer () override;
		int get () override;
		int peek () const override;
		int peek (size_t n) const override;
		bool eof () const override {return pos() == _size1 && _size2 == 0;}
		void invalidate () override {_bufptr = _buf1+_size1; _size2 = 0;}

	protected:
		int fillBuffer (uint8_t *buf);
		size_t pos () const  {return _bufptr-_buf1;}

	private:
		std::istream &_is;
		const size_t _bufsize;  ///< maximal number of bytes each buffer can hold
		uint8_t *_buf1;         ///< pointer to first buffer
		uint8_t *_buf2;         ///< pointer to second buffer
		size_t _size1;          ///< number of bytes in buffer 1
		size_t _size2;          ///< number of bytes in buffer 2
		uint8_t *_bufptr;       ///< pointer to next byte to read
};


class StringInputBuffer : public InputBuffer {
	public:
		explicit StringInputBuffer (const std::string &str) : _str(&str) {}
		void assign (const std::string &str) {_str = &str; _pos=0;}
		int get () override                  {return _pos < _str->length() ? _str->at(_pos++) : -1;}
		int peek () const override           {return _pos < _str->length() ? _str->at(_pos) : -1;}
		int peek (size_t n) const override   {return _pos+n < _str->length() ? _str->at(_pos+n) : -1;}
		bool eof () const override           {return _pos >= _str->length();}
		void invalidate () override          {_pos = _str->length();}

	private:
		const std::string *_str;
		size_t _pos=0;
};


class CharInputBuffer : public InputBuffer {
	public:
		CharInputBuffer (const char *buf, size_t size) : _pos(buf), _size(buf ? size : 0) {}

		int get () override {
			if (_size == 0)
				return -1;
			else {
				_size--;
				return *_pos++;
			}
		}


		void assign (const char *buf, size_t size) {
			_pos = buf;
			_size = size;
		}

		void assign (const char *buf)      {assign(buf, std::strlen(buf));}
		int peek () const override         {return _size > 0 ? *_pos : -1;}
		int peek (size_t n) const override {return _size >= n ? _pos[n] : -1;}
		bool eof () const override         {return _size == 0;}
		void invalidate () override        {_size = 0;}

	private:
		const char *_pos;
		size_t _size;
};


class SplittedCharInputBuffer : public InputBuffer {
	public:
		SplittedCharInputBuffer (const char *buf1, size_t s1, const char *buf2, size_t s2);
		int get () override;
		int peek () const override;
		int peek (size_t n) const override;
		bool eof () const override  {return _size[_index] == 0;}
		void invalidate () override {_size[_index] = 0;}

	private:
		const char *_buf[2];
		size_t _size[2];
		int _index;
};


class TextStreamInputBuffer : public StreamInputBuffer {
	public:
		explicit TextStreamInputBuffer (std::istream &is) : StreamInputBuffer(is) {}
		int get () override;
		int line () const {return _line;}
		int col () const {return _col;}

	private:
		int _line=1, _col=1;
};

#endif