File: input.c

package info (click to toggle)
fhist 1.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,932 kB
  • ctags: 1,268
  • sloc: ansic: 15,486; sh: 3,176; makefile: 1,019; awk: 154; yacc: 108
file content (230 lines) | stat: -rw-r--r-- 4,461 bytes parent folder | download
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
/*
 *	fhist - file history and comparison tools
 *	Copyright (C) 2000, 2001 Peter Miller;
 *	All rights reserved.
 *
 *	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 2 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, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions for reading input
 */

#include <ac/string.h>

#include <fcheck.h>
#include <input/private.h>
#include <mem.h>


#ifdef input_name
#undef input_name
#endif

const char *
input_name(fp)
	input_ty	*fp;
{
	return fp->vptr->name(fp);
}


#ifdef input_length
#undef input_length
#endif

long
input_length(fp)
	input_ty	*fp;
{
	return fp->vptr->length(fp);
}


#ifdef input_ftell
#undef input_ftell
#endif

long
input_ftell(fp)
	input_ty	*fp;
{
	return fp->vptr->ftell(fp) - fp->pushback_len;
}


#ifdef input_read
#undef input_read
#endif

long
input_read(fp, data, len)
	input_ty	*fp;
	void		*data;
	long		len;
{
	if (len <= 0)
		return 0;
	if (fp->pushback_len > 0)
	{
		fp->pushback_len--;
		*(char *)data = fp->pushback_buf[fp->pushback_len];
		return 1;
	}
	return fp->vptr->read(fp, data, len);
}


#ifdef input_getc
#undef input_getc
#endif

int
input_getc(fp)
	input_ty	*fp;
{
	if (fp->pushback_len > 0)
	{
		fp->pushback_len--;
		return fp->pushback_buf[fp->pushback_len];
	}
	return fp->vptr->get(fp);
}


#ifdef input_ungetc
#undef input_ungetc
#endif

void
input_ungetc(fp, c)
	input_ty	*fp;
	int		c;
{
	if (c < 0)
		return;
	if (fp->pushback_len >= fp->pushback_max)
	{
		fp->pushback_max = 16 + 2 * fp->pushback_max;
		fp->pushback_buf =
			mem_change_size(fp->pushback_buf, fp->pushback_max);
	}
	fp->pushback_buf[fp->pushback_len++] = c;
}


void
input_delete(fp)
	input_ty	*fp;
{
	if (fp->vptr->destruct)
		fp->vptr->destruct(fp);
	if (fp->pushback_buf)
		mem_free(fp->pushback_buf);
	fp->pushback_buf = 0;
	fp->pushback_len = 0;
	fp->pushback_max = 0;
	fp->vptr = 0; /* paranoia */
	mem_free(fp);
}


#include <cmalloc.h>

/*
 * Routine to read in the next line from a file, no matter how long it is.
 * This returns a pointer to the string, and also indirectly its length.
 * The string is normally returned from a static string, which is reused
 * for each call.  But if keep is non-zero, then the returned string
 * belongs to the caller and must later be freed.  The returned line
 * is ended with a newline and null character, but the returned length
 * does not count the null character.  Returns 0 on an error, with the
 * error stored in the fp structure.
 */

char *
input_readline(fp, retlen, keep, is_bin_file)
	input_ty	*fp;		/* file to read line from */
	long		*retlen;	/* ret len of line if non-null */
	int		keep;
	int		*is_bin_file;
{
	char		*dp;		/* destination pointer */
	size_t		totallen;	/* total length of data */
	static char	*linebuffer;	/* common line buffer */
	static size_t	linelength;	/* total length of line */

	if (linelength == 0)
	{
		/* allocate line buffer */
		linelength = 16;
		linebuffer = r_alloc_and_check(linelength + 1);
	}
	totallen = 0;
	for (;;)
	{
		int c = input_getc(fp);
		if (c < 0)
		{
			if (totallen == 0)
				return 0;
			warning_last_line_unterminated(input_name(fp));
			c = '\n';
		}
		if (c == 0)
		{
			*is_bin_file = 1;
			c = 0x80;
		}
		if (totallen >= linelength)
		{
			/*
			 * doubling gives O(1) behaviour
			 * over the life of the prog
			 */
			linelength = linelength * 2 + 16;
			linebuffer = r_realloc_and_check(linebuffer, linelength + 1);
		}
		linebuffer[totallen++] = c;
		if (c == '\n')
			break;
	}
	linebuffer[totallen] = 0;

	if (retlen)
		*retlen = totallen;
	if (!keep)
		dp = linebuffer;
	else
	{
		dp = cm_alloc_and_check(totallen + 1);
		memcpy(dp, linebuffer, (size_t)totallen);
	}
	return dp;
}


void
input_skip_lines(fp, numlines)
	input_ty	*fp;		/* file to read line from */
	int		numlines;
{
	while (numlines > 0)
	{
		int c = input_getc(fp);
		if (c < 0)
			break;
		if (c == '\n')
			--numlines;
	}
}