File: textfile.cpp

package info (click to toggle)
muscle 1%3A3.8.1551-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,512 kB
  • sloc: cpp: 27,031; xml: 230; makefile: 38; sh: 10
file content (359 lines) | stat: -rw-r--r-- 6,717 bytes parent folder | download | duplicates (11)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "muscle.h"
#include "textfile.h"
#include <errno.h>

TextFile::TextFile(const char szFileName[], bool bWrite)
	{
	FILE *ptrFile = 0;
	if (bWrite)
		{
		if (0 == strcmp(szFileName, "-"))
			ptrFile = stdout;
		else
			ptrFile = fopen(szFileName, "wb");
		}
	else
		{
		if (0 == strcmp(szFileName, "-"))
			ptrFile = stdin;
		else
			ptrFile = fopen(szFileName, "rb");
		}
	if (0 == ptrFile)
		Quit("Cannot open '%s' errno=%d\n", szFileName, errno);
	Init(ptrFile, szFileName);
	}

void TextFile::Init(FILE *ptrFile, const char *ptrFileName)
	{
	m_ptrFile = ptrFile;
	m_ptrName = strdup(ptrFileName);
	m_uLineNr = 1;
	m_uColNr = 0;
	m_bLastCharWasEOL = true;
	m_cPushedBack = -1;
#if	DEBUG
	setbuf(m_ptrFile, 0);
#endif
	}

TextFile::TextFile(FILE *ptrFile, const char *ptrFileName)
	{
	Init(ptrFile, "-");
	}

TextFile::~TextFile()
	{
	if (m_ptrFile &&
	  m_ptrFile != stdin && m_ptrFile != stdout && m_ptrFile != stderr)
		fclose(m_ptrFile);
	free(m_ptrName);
	}

// Get line from file.
// Return true if end-of-file, quit if line too long.
bool TextFile::GetLine(char szLine[], unsigned uBytes)
	{
	if (0 == uBytes)
		Quit("TextFile::GetLine, buffer zero size");

	
	int FillVal = 0; // suppress warning from gcc that I don't understand
	memset(szLine, FillVal, (size_t) uBytes);

	unsigned uBytesCopied = 0;

// Loop until end of line or end of file.
	for (;;)
		{
		char c;
		bool bEof = GetChar(c);
		if (bEof)
			return true;
		if ('\r' == c)
			continue;
		if ('\n' == c)
			return false;
		if (uBytesCopied < uBytes - 1)
			szLine[uBytesCopied++] = (char) c;
		else
			Quit("TextFile::GetLine: input buffer too small, line %u",
			  m_uLineNr);
		}
	}

// As GetLine, but trim leading and trailing blanks; skip empty lines
bool TextFile::GetTrimLine(char szLine[], unsigned uBytes)
	{
	if (uBytes == 0)
		Quit("GetTrimLine");
	for (;;)
		{
		bool bEOF = GetLine(szLine, uBytes);
		if (bEOF)
			return true;
		TrimBlanks(szLine);
		if (0 != szLine[0])
			break;
		}
	return false;
	}

void TextFile::Rewind()
	{
	fseek(m_ptrFile, 0, SEEK_SET);
	m_uLineNr = 1;
	m_bLastCharWasEOL = true;
	}

void TextFile::PutChar(char c)
	{
	int i = fputc(c, m_ptrFile);
	assert(i == c);
	if ('\n' == c)
		{
		++m_uLineNr;
		m_uColNr = 1;
		}
	else
		++m_uColNr;
	}

void TextFile::PutString(const char szLine[])
	{
	int iError = fputs(szLine, m_ptrFile);
	assert(iError >= 0);
	}

void TextFile::PutFormat(const char szFormat[], ...)
	{
	char szStr[4096];
	va_list ArgList;
	va_start(ArgList, szFormat);
	vsprintf(szStr, szFormat, ArgList);
	PutString(szStr);
	}

void TextFile::GetLineX(char szLine[], unsigned uBytes)
	{
	if (uBytes == 0)
		Quit("GetLineX");
	bool bEof = GetLine(szLine, uBytes);
	if (bEof)
		Quit("end-of-file in GetLineX");
	}

bool TextFile::GetToken(char szToken[], unsigned uBytes, const char szCharTokens[])
	{
// Skip leading white space
	char c;
	for (;;)
		{
		bool bEof = GetChar(c);
		if (bEof)
			return true;
		if (!isspace(c))
			break;
		}

// Check for special case single-character tokens
	if (0 != strchr(szCharTokens, c))
		{
		assert(uBytes >= 2);
		szToken[0] = c;
		szToken[1] = 0;
		return false;
		}

// Loop until token terminated by white space, EOF or special
	unsigned uBytesCopied = 0;
	for (;;)
		{
		if (uBytesCopied < uBytes - 1)
			szToken[uBytesCopied++] = c;
		else
			Quit("TextFile::GetToken: input buffer too small, line %u",
			  m_uLineNr);
		bool bEof = GetChar(c);
		if (bEof)
			{
			szToken[uBytesCopied] = 0;
			return true;
			}
	// Check for special case single-character tokens
		if (0 != strchr(szCharTokens, c))
			{
			PushBack(c);
			assert(uBytesCopied > 0 && uBytesCopied < uBytes);
			szToken[uBytesCopied] = 0;
			return false;
			}
		if (isspace(c))
			{
			assert(uBytesCopied > 0 && uBytesCopied < uBytes);
			szToken[uBytesCopied] = 0;
			return false;
			}
		}
	}

void TextFile::GetTokenX(char szToken[], unsigned uBytes, const char szCharTokens[])
	{
	bool bEof = GetToken(szToken, uBytes, szCharTokens);
	if (bEof)
		Quit("End-of-file in GetTokenX");
	}

void TextFile::Skip()
	{
	for (;;)
		{
		char c;
		bool bEof = GetChar(c);
		if (bEof || '\n' == c)
			return;
		assert(isspace(c));
		}
	}

#ifdef _WIN32

TEXTFILEPOS TextFile::GetPos()
	{
	fpos_t p;
	int i = fgetpos(m_ptrFile, &p);
	assert(0 == i);
	assert(p >= 0);
	TEXTFILEPOS Pos;
	Pos.uOffset = (unsigned) p;
	Pos.uLineNr = m_uLineNr;
	Pos.uColNr = m_uColNr;
	return Pos;
	}

void TextFile::SetPos(TEXTFILEPOS Pos)
	{
	fpos_t p = (fpos_t) Pos.uOffset;
	int i = fsetpos(m_ptrFile, &p);
	assert(0 == i);
	m_uLineNr = Pos.uLineNr;
	m_uColNr = Pos.uColNr;
	}

#else

TEXTFILEPOS TextFile::GetPos()
	{
	TEXTFILEPOS Pos;
	Pos.uOffset = ftell(m_ptrFile);
	Pos.uLineNr = m_uLineNr;
	Pos.uColNr = m_uColNr;
	return Pos;
	}

void TextFile::SetPos(TEXTFILEPOS Pos)
	{
	fseek(m_ptrFile, Pos.uOffset, SEEK_SET);
	m_uLineNr = Pos.uLineNr;
	m_uColNr = Pos.uColNr;
	}

#endif

bool TextFile::GetChar(char &c)
	{
	if (-1 != m_cPushedBack)
		{
		c = (char) m_cPushedBack;
		m_cPushedBack = -1;
		return false;
		}

	int ic = fgetc(m_ptrFile);
	if (ic < 0)
		{
		if (feof(m_ptrFile))
			{
		// Hack to fix up a non-empty text file that is missing
		// and end-of-line character in the last line.
			if (!m_bLastCharWasEOL && m_uLineNr > 0)
				{
				c = '\n';
				m_bLastCharWasEOL = true;
				return false;
				}
			return true;
			}
		Quit("TextFile::GetChar, error %s", strerror(errno));
		}
	c = (char) ic;
	if ('\n' == c)
		{
		m_bLastCharWasEOL = true;
		++m_uLineNr;
		m_uColNr = 1;
		}
	else
		{
		m_bLastCharWasEOL = false;
		++m_uColNr;
		}
	return false;
	}

void TextFile::GetCharX(char &c)
	{
	bool bEof = GetChar(c);
	if (bEof)
		Quit("End-of-file in GetCharX");
	}

void TextFile::GetNonblankChar(char &c)
	{
	do
		{
		bool bEof = GetChar(c);
		if (bEof)
			Quit("End-of-file in GetCharX");
		}
	while (isspace(c));
	}

void TextFile::SkipLine()
	{
	if (m_bLastCharWasEOL)
		return;
	for (;;)
		{
		char c;
		bool bEof = GetChar(c);
		if (bEof)
			Quit("End-of-file in SkipLine");
		if ('\n' == c)
			break;
		}
	}

void TextFile::SkipWhite()
	{
	bool bEof = SkipWhiteX();
	if (bEof)
		Quit("End-of-file skipping white space");
	}

bool TextFile::SkipWhiteX()
	{
	for (;;)
		{
		char c;
		bool bEof = GetChar(c);
		if (bEof)
			return true;
		if (!isspace(c))
			{
			PushBack(c);
			break;
			}
		}
	return false;
	}