2 |
7u83 |
1 |
/*
|
7 |
7u83 |
2 |
* Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
|
|
|
3 |
* All rights reserved.
|
|
|
4 |
*
|
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
|
6 |
* modification, are permitted provided that the following conditions are met:
|
|
|
7 |
*
|
|
|
8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
9 |
* this list of conditions and the following disclaimer.
|
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
11 |
* this list of conditions and the following disclaimer in the documentation
|
|
|
12 |
* and/or other materials provided with the distribution.
|
|
|
13 |
* 3. Neither the name of The TenDRA Project nor the names of its contributors
|
|
|
14 |
* may be used to endorse or promote products derived from this software
|
|
|
15 |
* without specific, prior written permission.
|
|
|
16 |
*
|
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
|
|
|
18 |
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
19 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
|
|
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
22 |
* EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
28 |
*
|
|
|
29 |
* $Id$
|
|
|
30 |
*/
|
|
|
31 |
/*
|
2 |
7u83 |
32 |
Crown Copyright (c) 1997
|
7 |
7u83 |
33 |
|
2 |
7u83 |
34 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
35 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
36 |
acting through the Defence Evaluation and Research Agency
|
|
|
37 |
(DERA). It is made available to Recipients with a
|
|
|
38 |
royalty-free licence for its use, reproduction, transfer
|
|
|
39 |
to other parties and amendment for any purpose not excluding
|
|
|
40 |
product development provided that any such use et cetera
|
|
|
41 |
shall be deemed to be acceptance of the following conditions:-
|
7 |
7u83 |
42 |
|
2 |
7u83 |
43 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
44 |
reproduced upon any copies or amended versions of it;
|
7 |
7u83 |
45 |
|
2 |
7u83 |
46 |
(2) Any amended version of it shall be clearly marked to
|
|
|
47 |
show both the nature of and the organisation responsible
|
|
|
48 |
for the relevant amendment or amendments;
|
7 |
7u83 |
49 |
|
2 |
7u83 |
50 |
(3) Its onward transfer from a recipient to another
|
|
|
51 |
party shall be deemed to be that party's acceptance of
|
|
|
52 |
these conditions;
|
7 |
7u83 |
53 |
|
2 |
7u83 |
54 |
(4) DERA gives no warranty or assurance as to its
|
|
|
55 |
quality or suitability for any purpose and DERA accepts
|
|
|
56 |
no liability whatsoever in relation to any use to which
|
|
|
57 |
it may be put.
|
|
|
58 |
*/
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
/**** istream.h --- Input stream handling.
|
|
|
62 |
*
|
|
|
63 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
64 |
*
|
|
|
65 |
**** Commentary:
|
|
|
66 |
*
|
|
|
67 |
***=== INTRODUCTION =========================================================
|
|
|
68 |
*
|
|
|
69 |
* This file specifies the interface to the input stream facility.
|
|
|
70 |
*
|
|
|
71 |
***=== TYPES ================================================================
|
|
|
72 |
*
|
|
|
73 |
** Type: IStreamT
|
|
|
74 |
** Type: IStreamP
|
|
|
75 |
** Repr: <private>
|
|
|
76 |
*
|
|
|
77 |
* This is the input stream type.
|
|
|
78 |
*
|
|
|
79 |
** Type: IStreamStatusT
|
|
|
80 |
** Type: IStreamStatusP
|
|
|
81 |
** Repr: enum {ISTREAM_STAT_READ_CHAR, ISTREAM_STAT_NO_CHAR,
|
|
|
82 |
* ISTREAM_STAT_SYNTAX_ERROR}
|
|
|
83 |
*
|
|
|
84 |
* This is the return type of the ``istream_read_escaped_char'' function. The
|
|
|
85 |
* constants represent the results: a character was read, no character was
|
|
|
86 |
* read, and a syntax error occured respectively.
|
|
|
87 |
*
|
|
|
88 |
***=== CONSTANTS ============================================================
|
|
|
89 |
*
|
|
|
90 |
** Constant: istream_input
|
|
|
91 |
*
|
|
|
92 |
* This value is a pointer to an input stream object that will read from the
|
|
|
93 |
* standard input. The ``istream_setup'' function must be called before this
|
|
|
94 |
* constant is used.
|
|
|
95 |
*
|
|
|
96 |
***=== FUNCTIONS ============================================================
|
|
|
97 |
*
|
|
|
98 |
** Function: void istream_setup
|
7 |
7u83 |
99 |
* (void)
|
2 |
7u83 |
100 |
** Exceptions:
|
|
|
101 |
*
|
|
|
102 |
* This function initialises the input stream facility. It should be called
|
|
|
103 |
* before any other istream manipulation function.
|
|
|
104 |
*
|
|
|
105 |
** Function: void istream_init
|
7 |
7u83 |
106 |
* (IStreamP istream)
|
2 |
7u83 |
107 |
** Exceptions:
|
|
|
108 |
*
|
|
|
109 |
* This function initialises the specified istream not to read from any file.
|
|
|
110 |
*
|
|
|
111 |
** Function: BoolT istream_open
|
7 |
7u83 |
112 |
* (IStreamP istream, CStringP name)
|
2 |
7u83 |
113 |
** Exceptions: XX_dalloc_no_memory, XX_istream_read_error
|
|
|
114 |
*
|
|
|
115 |
* This function initialises the specified istream to read from the file with
|
|
|
116 |
* the specified name. The name should not be modified or deallocated until
|
|
|
117 |
* the istream has been closed. If the file cannot be opened, the function
|
|
|
118 |
* returns false. If the file is opened successfully, the function returns
|
|
|
119 |
* true.
|
|
|
120 |
*
|
|
|
121 |
** Function: void istream_assign
|
7 |
7u83 |
122 |
* (IStreamP to, IStreamP from)
|
2 |
7u83 |
123 |
** Exceptions:
|
|
|
124 |
*
|
|
|
125 |
* This function assigns the from istream to the to istream. The from istream
|
|
|
126 |
* should not be used again.
|
|
|
127 |
*
|
|
|
128 |
** Function: BoolT istream_is_open
|
7 |
7u83 |
129 |
* (IStreamP istream)
|
2 |
7u83 |
130 |
*
|
|
|
131 |
* This function returns true if the specified istream is reading from a file,
|
|
|
132 |
* and false otherwise.
|
|
|
133 |
*
|
|
|
134 |
** Function: BoolT istream_read_char
|
7 |
7u83 |
135 |
* (IStreamP istream, char *c_ref)
|
2 |
7u83 |
136 |
** Exceptions: XX_dalloc_no_memory, XX_istream_read_error
|
|
|
137 |
*
|
|
|
138 |
* This function reads the next character from the specified istream (and
|
|
|
139 |
* advances the character pointer). If a character is read then the character
|
|
|
140 |
* is assigned to the reference argument, and the function returns true. If
|
|
|
141 |
* the end of file is reached, the function returns false. If the character
|
|
|
142 |
* read is a newline, then the istream's line count is incremented.
|
|
|
143 |
*
|
|
|
144 |
** Function: BoolT istream_peek_char
|
7 |
7u83 |
145 |
* (IStreamP istream, char *c_ref)
|
2 |
7u83 |
146 |
** Exceptions: XX_dalloc_no_memory, XX_istream_read_error
|
|
|
147 |
*
|
|
|
148 |
* This function reads the next character from the specified istream (but does
|
|
|
149 |
* not advance the character pointer). If a character is read then the
|
|
|
150 |
* character is assigned to the reference argument, and the function returns
|
|
|
151 |
* true. If the end of file is reached, the function returns false.
|
|
|
152 |
*
|
|
|
153 |
** Function: IStreamStatusT istream_read_escaped_char
|
7 |
7u83 |
154 |
* (IStreamP istream, char *c_ref)
|
2 |
7u83 |
155 |
** Exceptions: XX_dalloc_no_memory, XX_istream_read_error
|
|
|
156 |
*
|
|
|
157 |
* This function reads a character sequence from the specified istream, and
|
|
|
158 |
* parses it as an escaped character sequence. Normally, the character to
|
|
|
159 |
* which the sequence evaluates is assigned to the reference argument and
|
|
|
160 |
* ``ISTREAM_STAT_READ_CHAR'' is returned. If if it evaluates to no
|
|
|
161 |
* character, then ``ISTREAM_STAT_NO_CHAR'' is returned (this is not an
|
|
|
162 |
* error). If there is an error in the syntax of the character sequence, then
|
|
|
163 |
* ``ISTREAM_STAT_SYNTAX_ERROR'' is returned. If any of the characters read
|
|
|
164 |
* are newline characters, then the istream's line counter will be incremented
|
|
|
165 |
* for each newline.
|
|
|
166 |
*
|
|
|
167 |
** Function: void istream_inc_line
|
7 |
7u83 |
168 |
* (IStreamP istream)
|
2 |
7u83 |
169 |
** Exceptions:
|
|
|
170 |
*
|
|
|
171 |
* This function increments the specified istream's line counter. It should
|
|
|
172 |
* only really be called as specified in the documentation for the
|
|
|
173 |
* ``ISTREAM_READ_CHAR'' macro.
|
|
|
174 |
*
|
|
|
175 |
** Function: unsigned istream_line
|
7 |
7u83 |
176 |
* (IStreamP istream)
|
2 |
7u83 |
177 |
** Exceptions:
|
|
|
178 |
*
|
|
|
179 |
* This function returns the line number of the specified istream (one more
|
|
|
180 |
* than the number of newlines that have been read).
|
|
|
181 |
*
|
|
|
182 |
** Function: CStringP istream_name
|
7 |
7u83 |
183 |
* (IStreamP istream)
|
2 |
7u83 |
184 |
** Exceptions:
|
|
|
185 |
*
|
|
|
186 |
* This function returns the name of the file from which the specified istream
|
|
|
187 |
* is reading. The return value should not be modified or deallocated.
|
|
|
188 |
*
|
|
|
189 |
** Function: void istream_close
|
7 |
7u83 |
190 |
* (IStreamP istream)
|
2 |
7u83 |
191 |
** Exceptions:
|
|
|
192 |
*
|
|
|
193 |
* This function closes the specified istream.
|
|
|
194 |
*
|
|
|
195 |
***=== MACROS ===============================================================
|
|
|
196 |
*
|
|
|
197 |
** Macro: ISTREAM_READ_CHAR (istream)
|
|
|
198 |
** Exceptions:
|
|
|
199 |
*
|
|
|
200 |
* This macro returns the next character from the specified istream. It is a
|
|
|
201 |
* slightly faster alternative to the ``istream_read_char'' function. In
|
|
|
202 |
* order to get the speed improvement, the program needs to do some extra
|
|
|
203 |
* work: if the character returned is a newline, then the program must call
|
|
|
204 |
* the ``istream_inc_line'' function to increment the line count; if the
|
|
|
205 |
* character returned is a null character, then the program must call the
|
|
|
206 |
* ``ISTREAM_HANDLE_NULL'' macro on the istream that was read. It is not
|
|
|
207 |
* obvious that the speed increase is worth the extra effort in coding.
|
|
|
208 |
*
|
|
|
209 |
** Macro: ISTREAM_PEEK_CHAR (istream)
|
|
|
210 |
** Exceptions:
|
|
|
211 |
*
|
|
|
212 |
* This macro returns the next character from the specified istream, without
|
|
|
213 |
* reading it. It is a slightly faster alternative to the
|
|
|
214 |
* ``istream_peek_char'' function. In order to get the speed improvement, the
|
|
|
215 |
* program needs to do some extra work: if the character returned is the null
|
|
|
216 |
* character, then the program must call the ``ISTREAM_HANDLE_NULL'' macro on
|
|
|
217 |
* the istream that was read. Unlike the ``ISTREAM_READ_CHAR'' macro, it is
|
|
|
218 |
* not necessary to increment the istream's line count. It is not obvious
|
|
|
219 |
* that the speed increase is worth the extra effort in coding.
|
|
|
220 |
*
|
|
|
221 |
** Macro: ISTREAM_HANDLE_NULL (istream, redo, eof)
|
|
|
222 |
** Exceptions: XX_dalloc_no_memory, XX_istream_read_error
|
|
|
223 |
*
|
|
|
224 |
* This macro should be called when either of the previous two macros returns
|
|
|
225 |
* the null character. It checks to see if the null character is really a
|
|
|
226 |
* null character, a refill buffer instruction, or an end of file. If it is a
|
|
|
227 |
* real null character, then the program continues normally. If it is a
|
|
|
228 |
* refill buffer instruction, the istream's buffer is refilled, and the
|
|
|
229 |
* program goes to the label ``redo''. If it is an end of file, then the
|
|
|
230 |
* program goes to the label ``eof''.
|
|
|
231 |
*
|
|
|
232 |
***=== EXCEPTIONS ===========================================================
|
|
|
233 |
*
|
|
|
234 |
** Exception: XX_istream_read_error (CStringP name)
|
|
|
235 |
*
|
|
|
236 |
* This exception is raised if a read attempt fails. The data thrown is a
|
|
|
237 |
* copy of the name of the file that the read error occured on. The copy
|
|
|
238 |
* should be deallocated when finished with.
|
|
|
239 |
*
|
|
|
240 |
**** Change Log:
|
|
|
241 |
* $Log: istream.h,v $
|
|
|
242 |
* Revision 1.1.1.1 1998/01/17 15:57:45 release
|
|
|
243 |
* First version to be checked into rolling release.
|
|
|
244 |
*
|
|
|
245 |
* Revision 1.2 1994/12/12 11:45:43 smf
|
|
|
246 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
247 |
* OSSG C Coding Standards.
|
|
|
248 |
*
|
|
|
249 |
* Revision 1.1.1.1 1994/07/25 16:06:10 smf
|
|
|
250 |
* Initial import of os-interface shared files.
|
|
|
251 |
*
|
|
|
252 |
**/
|
|
|
253 |
|
|
|
254 |
/****************************************************************************/
|
|
|
255 |
|
|
|
256 |
#ifndef H_ISTREAM
|
|
|
257 |
#define H_ISTREAM
|
|
|
258 |
|
|
|
259 |
#include "os-interface.h"
|
|
|
260 |
#include "dalloc.h"
|
|
|
261 |
#include "exception.h"
|
|
|
262 |
|
|
|
263 |
/*--------------------------------------------------------------------------*/
|
|
|
264 |
|
|
|
265 |
typedef struct IStreamT {
|
|
|
266 |
FILE *file;
|
|
|
267 |
CStringP buffer;
|
|
|
268 |
CStringP current;
|
|
|
269 |
CStringP end;
|
|
|
270 |
CStringP limit;
|
|
|
271 |
unsigned line;
|
|
|
272 |
CStringP name;
|
|
|
273 |
BoolT read_last;
|
|
|
274 |
} IStreamT, *IStreamP;
|
|
|
275 |
|
|
|
276 |
#ifdef FS_NO_ENUM
|
|
|
277 |
typedef int IStreamStatusT, *IStreamStatusP;
|
|
|
278 |
#define ISTREAM_STAT_READ_CHAR (0)
|
|
|
279 |
#define ISTREAM_STAT_NO_CHAR (1)
|
|
|
280 |
#define ISTREAM_STAT_SYNTAX_ERROR (2)
|
|
|
281 |
#else
|
|
|
282 |
typedef enum {
|
|
|
283 |
ISTREAM_STAT_READ_CHAR,
|
|
|
284 |
ISTREAM_STAT_NO_CHAR,
|
|
|
285 |
ISTREAM_STAT_SYNTAX_ERROR
|
|
|
286 |
} IStreamStatusT, *IStreamStatusP;
|
|
|
287 |
#endif /* defined (FS_NO_ENUM) */
|
|
|
288 |
|
|
|
289 |
/*--------------------------------------------------------------------------*/
|
|
|
290 |
|
7 |
7u83 |
291 |
extern ExceptionP XX_istream_read_error;
|
|
|
292 |
extern IStreamT *const istream_input;
|
2 |
7u83 |
293 |
|
|
|
294 |
/*--------------------------------------------------------------------------*/
|
|
|
295 |
|
7 |
7u83 |
296 |
extern void istream_setup(void);
|
|
|
297 |
extern void istream_init(IStreamP);
|
|
|
298 |
extern BoolT istream_open(IStreamP, CStringP);
|
|
|
299 |
extern void istream_assign(IStreamP, IStreamP);
|
|
|
300 |
extern BoolT istream_is_open(IStreamP);
|
|
|
301 |
extern BoolT istream_read_char(IStreamP, char *);
|
|
|
302 |
extern BoolT istream_peek_char(IStreamP, char *);
|
|
|
303 |
extern IStreamStatusT istream_read_escaped_char(IStreamP, char *);
|
|
|
304 |
extern void istream_inc_line(IStreamP);
|
|
|
305 |
extern unsigned istream_line(IStreamP);
|
|
|
306 |
extern CStringP istream_name(IStreamP);
|
|
|
307 |
extern void istream_close(IStreamP);
|
2 |
7u83 |
308 |
|
|
|
309 |
/*--------------------------------------------------------------------------*/
|
|
|
310 |
|
7 |
7u83 |
311 |
extern void X__istream_fill_buffer(IStreamP);
|
2 |
7u83 |
312 |
|
|
|
313 |
/*--------------------------------------------------------------------------*/
|
|
|
314 |
|
7 |
7u83 |
315 |
#define ISTREAM_READ_CHAR(istream)\
|
|
|
316 |
(((istream)->read_last = TRUE), (*((istream)->current)++))
|
2 |
7u83 |
317 |
|
7 |
7u83 |
318 |
#define ISTREAM_PEEK_CHAR(istream)\
|
2 |
7u83 |
319 |
(((istream)->read_last = FALSE), (*((istream)->current)))
|
|
|
320 |
|
7 |
7u83 |
321 |
#define ISTREAM_HANDLE_NULL(istream,redo,eof)\
|
2 |
7u83 |
322 |
{ \
|
|
|
323 |
IStreamP X___is = (istream); \
|
|
|
324 |
if (X___is->read_last) { \
|
|
|
325 |
if (X___is->current == X___is->end) { \
|
|
|
326 |
if (X___is->end == X___is->limit) { \
|
7 |
7u83 |
327 |
X__istream_fill_buffer(X___is); \
|
2 |
7u83 |
328 |
goto redo; \
|
|
|
329 |
} else { \
|
7 |
7u83 |
330 |
X___is->current--; \
|
2 |
7u83 |
331 |
goto eof; \
|
|
|
332 |
} \
|
|
|
333 |
} \
|
|
|
334 |
} else { \
|
|
|
335 |
if (X___is->current == (X___is->end - 1)) { \
|
|
|
336 |
if (X___is->end == X___is->limit) { \
|
7 |
7u83 |
337 |
X__istream_fill_buffer(X___is); \
|
2 |
7u83 |
338 |
goto redo; \
|
|
|
339 |
} else { \
|
|
|
340 |
goto eof; \
|
|
|
341 |
} \
|
|
|
342 |
} \
|
|
|
343 |
} \
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/*--------------------------------------------------------------------------*/
|
|
|
347 |
|
|
|
348 |
#ifdef FS_FAST
|
7 |
7u83 |
349 |
#define istream_init(is) ((is)->name = NIL(CStringP))
|
|
|
350 |
#define istream_is_open(is) ((is)->name != NIL(CStringP))
|
|
|
351 |
#define istream_inc_line(is) ((is)->line++)
|
|
|
352 |
#define istream_line(is) ((is)->line)
|
|
|
353 |
#define istream_name(is) ((is)->name)
|
2 |
7u83 |
354 |
#endif /* defined (FS_FAST) */
|
|
|
355 |
|
|
|
356 |
#endif /* !defined (H_ISTREAM) */
|