Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/slurper.cc

raw
1 // -*- related-file-name: "../include/lcdf/slurper.hh" -*-
2
3 /* slurper.{cc,hh} -- reading from files a line at a time
4 *
5 * Copyright (c) 1998-2023 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <lcdf/slurper.hh>
20 #include <string.h>
21
22 static const int DefChunkCap = 2048;
23 static const int WorthMoving = 256;
24
25
26 Slurper::Slurper(const Filename &filename, FILE *f)
27 : _filename(filename), _lineno(0),
28 _data(new unsigned char[DefChunkCap]), _cap(DefChunkCap),
29 _pos(0), _len(0), _line(0), _line_len(0),
30 _saved_line(false), _at_eof(false)
31 {
32 if (f) {
33 _f = f;
34 _own_f = false;
35 } else {
36 _f = _filename.open_read();
37 _own_f = true;
38 }
39 }
40
41 Slurper::~Slurper()
42 {
43 delete[] _data;
44 if (_f && _own_f) fclose(_f);
45 }
46
47
48 void
49 Slurper::grow_buffer()
50 {
51 // If we need to keep the upper part of the buffer, shift it down (moving
52 // any data we care about).
53 if (_pos >= _cap - WorthMoving) {
54 // I used to move it myself, with a comment that SunOS didn't have a
55 // correct implementation of memmove. This should be detected by
56 // configure, if it's really a problem.
57 memmove(_data, _data + _pos, _len - _pos);
58 _len -= _pos;
59 _pos = 0;
60 }
61
62 // Grow the buffer if necessary.
63 if (_len >= _cap) {
64 unsigned char *new_data = new unsigned char[ 2 * _cap ];
65 // I can't believe I didn't have the line below!!
66 memcpy(new_data, _data, _len);
67 delete[] _data;
68 _data = new_data;
69 _cap = 2 * _cap;
70 }
71 }
72
73
74 inline int
75 Slurper::more_data()
76 {
77 grow_buffer();
78
79 // Read new data into the buffer.
80 int amount = fread(_data + _len, 1, _cap - _len, _f);
81 _len += amount;
82 return amount;
83 }
84
85 char *
86 Slurper::get_line_at(unsigned pos)
87 {
88 while (1) {
89 for (; pos < _len; pos++)
90 if (_data[pos] == '\n' || _data[pos] == '\r')
91 goto line_ends_at_pos;
92
93 // no line end? look for more data. save and reset `pos', since _pos
94 // may change.
95 int offset = pos - _pos;
96 bool got_more_data = more_data() != 0;
97 pos = _pos + offset;
98 if (!got_more_data) {
99 _at_eof = true;
100 goto line_ends_at_pos;
101 }
102 }
103
104 line_ends_at_pos:
105
106 // PRECONDITION: the line starts at _pos and ends at pos.
107 unsigned next_pos;
108
109 // Find beginning of next line. 3 cases:
110 // 1. line ends in \r\n -> _pos = pos + 2;
111 // 2. line ends in \r OR \n -> _pos = pos + 1;
112 // 3. neither -> must be last line in file; _pos = pos,
113 // since pos == _len
114 if (pos == _len) {
115 // last line in file didn't end in `\n': must have no data left
116 // ensure we have enough space for terminating nul
117 if (pos == _cap) grow_buffer();
118 next_pos = pos;
119 // if already at EOF, don't increment the line number
120 if (pos == _pos) _lineno--;
121
122 } else if (_data[pos] == '\n')
123 next_pos = pos + 1;
124
125 else {
126 assert(_data[pos] == '\r');
127 // If `\r' is last char in buffer, `\n' might be next char. Must read more
128 // data to check for it, or we'd report an empty line that didn't really
129 // exist.
130 if (pos == _len - 1) {
131 // be careful about the possible shift of _pos and _len!
132 int offset = pos - _pos;
133 more_data();
134 pos = _pos + offset;
135 }
136 if (pos < _len - 1 && _data[pos + 1] == '\n')
137 next_pos = pos + 2;
138 else
139 next_pos = pos + 1;
140 }
141
142 _line = _data + _pos;
143 _line_len = pos - _pos;
144 _data[pos] = 0;
145 _pos = next_pos;
146 _lineno++;
147 return (char *)_line;
148 }
149
150 char *
151 Slurper::next_line()
152 {
153 if (_saved_line) {
154 _saved_line = false;
155 return (char *)_line;
156 }
157 get_line_at(_pos);
158 if (_line_len == 0 && _at_eof)
159 _line = 0;
160 return (char *)_line;
161 }
162
163 char *
164 Slurper::peek_line()
165 {
166 next_line();
167 _saved_line = true;
168 return (char *)_line;
169 }
170
171 char *
172 Slurper::append_next_line()
173 {
174 unsigned delta = (_data + _pos) - (_line + _line_len);
175 if (_len - _pos > _line_len) {
176 memmove(_line + delta, _line, _line_len);
177 _line += delta;
178 } else {
179 memmove(_data + _pos - delta, _data + _pos, _len - _pos);
180 _pos -= delta;
181 _len -= delta;
182 }
183
184 unsigned append_at = _pos;
185 _pos = _line - _data;
186 return get_line_at(append_at);
187 }