summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-1.0.9/src/InputBuffer.cpp
blob: f5e7125f80b61853e42ad32e076f234f3fd99dce (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
/*************************************************************************
** InputBuffer.cpp                                                      **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2011 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/>. **
*************************************************************************/

#include <cmath>
#include <cstring>
#include "InputBuffer.h"

using namespace std;


StreamInputBuffer::StreamInputBuffer (istream &is, unsigned bufsize)
	: _is(is), _bufsize(bufsize), _buf1(new UInt8[_bufsize]), _buf2(new UInt8[_bufsize]), _bufptr(_buf1)
{
	_size1 = fillBuffer(_buf1);
	_size2 = fillBuffer(_buf2);
}


StreamInputBuffer::~StreamInputBuffer () {
	delete [] _buf1;
	delete [] _buf2;
}


int StreamInputBuffer::get () {
	if (pos() == _size1) {
		if (_size2 == 0)
			return -1;
		swap(_buf1, _buf2);
		_size1 = _size2;
		_bufptr = _buf1;
		_size2 = fillBuffer(_buf2);
	}
	UInt8 c = *_bufptr++;
	return c;
}


/** Returns the next character to be read without skipping it.
 *  Same as peek(0). */
int StreamInputBuffer::peek () const {
	if (pos() < _size1)
		return *_bufptr;
	return _size2 > 0 ? *_buf2 : -1;
}


/** Returns the n-th next character without skipping it. */
int StreamInputBuffer::peek (unsigned n) const {
	if (pos()+n < _size1)
		return *(_bufptr+n);
	if (pos()+n < _size1+_size2)
		return *(_buf2 + pos()+n-_size1);
	return -1;
}


/** Fills the buffer by reading a sequence of characters from the assigned
 *  input stream.
 *  @param[in] buf pointer to character buffer to be filled
 *  @return number of characters read */
int StreamInputBuffer::fillBuffer (UInt8 *buf) {
	if (_is && !_is.eof()) {
		_is.read((char*)buf, _bufsize);
		return  _is.gcount();
	}
	return 0;
}

///////////////////////////////////////////////

SplittedCharInputBuffer::SplittedCharInputBuffer (const char *buf1, unsigned s1, const char *buf2, unsigned s2) {
	_buf[0] = buf1;
	_buf[1] = buf2;
	_size[0] = buf1 ? s1 : 0;
  	_size[1] = buf2 ? s2 : 0;
	_index = _size[0] ? 0 : 1;
}


int SplittedCharInputBuffer::get () {
	if (_size[_index] == 0)
		return -1;
	int ret = *_buf[_index]++;
	_size[_index]--;
	if (_index == 0 && _size[0] == 0)
		_index++;
	return ret;
}


int SplittedCharInputBuffer::peek () const {
	return _size[_index] ? *_buf[_index] : -1;
}


int SplittedCharInputBuffer::peek (unsigned n) const {
	if (n < _size[_index])
		return _buf[_index][n];
	n -= _size[_index];
	if (_index == 0 && n < _size[1])
		return _buf[1][n];
	return -1;
}

///////////////////////////////////////////////


int TextStreamInputBuffer::get () {
	int c = StreamInputBuffer::get();
	if (c == '\n') {
		_line++;
		_col = 1;
	}
	else
		_col++;
	return c;
}