~reviczky/luatex/texlive-bin-git

« back to all changes in this revision

Viewing changes to texk/dvisvgm/dvisvgm-1.8.1/src/InputBuffer.h

  • Committer: Adam Reviczky
  • Date: 2015-04-26 22:40:47 UTC
  • Revision ID: adam.reviczky@kclalumni.net-20150426224047-i2p26n3wqphupq6z
TeX Live 2015 import (rev. 37052)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************************************
2
 
** InputBuffer.h                                                        **
3
 
**                                                                      **
4
 
** This file is part of dvisvgm -- the DVI to SVG converter             **
5
 
** Copyright (C) 2005-2014 Martin Gieseking <martin.gieseking@uos.de>   **
6
 
**                                                                      **
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.                  **
11
 
**                                                                      **
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.                         **
16
 
**                                                                      **
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
 
*************************************************************************/
20
 
 
21
 
#ifndef DVISVGM_INPUTBUFFER_H
22
 
#define DVISVGM_INPUTBUFFER_H
23
 
 
24
 
#include <algorithm>
25
 
#include <cstring>
26
 
#include <istream>
27
 
#include <string>
28
 
#include <ostream>
29
 
#include "types.h"
30
 
 
31
 
struct InputBuffer
32
 
{
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;
39
 
};
40
 
 
41
 
 
42
 
class StreamInputBuffer : public InputBuffer
43
 
{
44
 
        public:
45
 
                StreamInputBuffer (std::istream &is, size_t bufsize=1024);
46
 
                ~StreamInputBuffer ();
47
 
                int get ();
48
 
                int peek () const;
49
 
                int peek (size_t n) const;
50
 
                bool eof () const  {return pos() == _size1 && _size2 == 0;}
51
 
                void invalidate () {_bufptr = _buf1+_size1; _size2 = 0;}
52
 
 
53
 
        protected:
54
 
                int fillBuffer (UInt8 *buf);
55
 
                size_t pos () const  {return _bufptr-_buf1;}
56
 
 
57
 
        private:
58
 
                std::istream &_is;
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
65
 
};
66
 
 
67
 
 
68
 
class StringInputBuffer : public InputBuffer
69
 
{
70
 
        public:
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();}
77
 
 
78
 
        private:
79
 
                const std::string &_str;
80
 
                size_t _pos;
81
 
};
82
 
 
83
 
 
84
 
class CharInputBuffer : public InputBuffer
85
 
{
86
 
        public:
87
 
                CharInputBuffer (const char *buf, size_t size) : _pos(buf), _size(buf ? size : 0) {}
88
 
 
89
 
                int get () {
90
 
                        if (_size == 0)
91
 
                                return -1;
92
 
                        else {
93
 
                                _size--;
94
 
                                return *_pos++;
95
 
                        }
96
 
                }
97
 
 
98
 
 
99
 
                void assign (const char *buf, size_t size) {
100
 
                        _pos = buf;
101
 
                        _size = size;
102
 
                }
103
 
 
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;}
109
 
 
110
 
        private:
111
 
                const char *_pos;
112
 
                size_t _size;
113
 
};
114
 
 
115
 
 
116
 
class SplittedCharInputBuffer : public InputBuffer
117
 
{
118
 
        public:
119
 
                SplittedCharInputBuffer (const char *buf1, size_t s1, const char *buf2, size_t s2);
120
 
                int get ();
121
 
                int peek () const;
122
 
                int peek (size_t n) const;
123
 
                bool eof () const   {return _size[_index] == 0;}
124
 
                void invalidate ()  {_size[_index] = 0;}
125
 
 
126
 
        private:
127
 
                const char *_buf[2];
128
 
                size_t _size[2];
129
 
                int _index;
130
 
};
131
 
 
132
 
 
133
 
class TextStreamInputBuffer : public StreamInputBuffer
134
 
{
135
 
        public:
136
 
                TextStreamInputBuffer (std::istream &is) : StreamInputBuffer(is), _line(1), _col(1) {}
137
 
                int get ();
138
 
                int line () const {return _line;}
139
 
                int col () const {return _col;}
140
 
 
141
 
        private:
142
 
                int _line, _col;
143
 
};
144
 
 
145
 
#endif