Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 61... Line 91...
61
 
91
 
62
#define ISTREAM_BUFSIZE 8193
92
#define ISTREAM_BUFSIZE 8193
63
 
93
 
64
/*--------------------------------------------------------------------------*/
94
/*--------------------------------------------------------------------------*/
65
 
95
 
66
ExceptionP XX_istream_read_error = EXCEPTION ("error reading from stream");
96
ExceptionP XX_istream_read_error = EXCEPTION("error reading from stream");
67
 
97
 
68
/*--------------------------------------------------------------------------*/
98
/*--------------------------------------------------------------------------*/
69
 
99
 
70
static char istream_input_buffer [ISTREAM_BUFSIZE];
100
static char istream_input_buffer[ISTREAM_BUFSIZE];
71
 
101
 
72
static IStreamT		istream_input_1 = {
102
static IStreamT		istream_input_1 = {
73
    NIL (FILE *),
103
    NIL(FILE *),
74
    &(istream_input_buffer [0]),
104
    &(istream_input_buffer[0]),
75
    &(istream_input_buffer [ISTREAM_BUFSIZE - 1]),
105
    &(istream_input_buffer[ISTREAM_BUFSIZE - 1]),
76
    &(istream_input_buffer [ISTREAM_BUFSIZE]),
106
    &(istream_input_buffer[ISTREAM_BUFSIZE]),
77
    &(istream_input_buffer [ISTREAM_BUFSIZE]),
107
    &(istream_input_buffer[ISTREAM_BUFSIZE]),
78
    1,
108
    1,
79
    "<stdin>",
109
    "<stdin>",
80
    FALSE
110
    FALSE
81
};
111
};
82
 
112
 
83
IStreamT	 *const istream_input = &istream_input_1;
113
IStreamT	 *const istream_input = &istream_input_1;
84
 
114
 
85
/*--------------------------------------------------------------------------*/
115
/*--------------------------------------------------------------------------*/
86
 
116
 
87
static IStreamStatusT
117
static IStreamStatusT
88
istream_read_hex_char PROTO_N ((istream, c_ref))
118
istream_read_hex_char(IStreamP istream, char *c_ref)
89
		      PROTO_T (IStreamP istream X
-
 
90
			       char    *c_ref)
-
 
91
{
119
{
92
    int value;
120
    int value;
93
    int tmp;
121
    int tmp;
94
    char c;
122
    char c;
95
 
123
 
96
  redo1:
124
  redo1:
97
    switch (c = ISTREAM_READ_CHAR (istream)) {
125
    switch (c = ISTREAM_READ_CHAR(istream)) {
98
      case '\0':
126
      case '\0':
99
	ISTREAM_HANDLE_NULL (istream, redo1, eof);
127
	ISTREAM_HANDLE_NULL(istream, redo1, eof);
100
	return (ISTREAM_STAT_SYNTAX_ERROR);
128
	return(ISTREAM_STAT_SYNTAX_ERROR);
101
      case '\n':
129
      case '\n':
102
	istream_inc_line (istream);
130
	istream_inc_line(istream);
103
	return (ISTREAM_STAT_SYNTAX_ERROR);
131
	return(ISTREAM_STAT_SYNTAX_ERROR);
104
      default:
132
      default:
105
	if (((value = syntax_value (c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
133
	if (((value = syntax_value(c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
106
	    return (ISTREAM_STAT_SYNTAX_ERROR);
134
	    return(ISTREAM_STAT_SYNTAX_ERROR);
107
	}
135
	}
108
	tmp = value;
136
	tmp = value;
109
	break;
137
	break;
110
    }
138
    }
111
  redo2:
139
  redo2:
112
    switch (c = ISTREAM_READ_CHAR (istream)) {
140
    switch (c = ISTREAM_READ_CHAR(istream)) {
113
      case '\0':
141
      case '\0':
114
	ISTREAM_HANDLE_NULL (istream, redo2, eof);
142
	ISTREAM_HANDLE_NULL(istream, redo2, eof);
115
	return (ISTREAM_STAT_SYNTAX_ERROR);
143
	return(ISTREAM_STAT_SYNTAX_ERROR);
116
      case '\n':
144
      case '\n':
117
	istream_inc_line (istream);
145
	istream_inc_line(istream);
118
	return (ISTREAM_STAT_SYNTAX_ERROR);
146
	return(ISTREAM_STAT_SYNTAX_ERROR);
119
      default:
147
      default:
120
	if (((value = syntax_value (c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
148
	if (((value = syntax_value(c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
121
	    return (ISTREAM_STAT_SYNTAX_ERROR);
149
	    return(ISTREAM_STAT_SYNTAX_ERROR);
122
	}
150
	}
123
	break;
151
	break;
124
    }
152
    }
125
    *c_ref = (char) ((tmp * 16) + value);
153
    *c_ref = (char)((tmp * 16) + value);
126
    return (ISTREAM_STAT_READ_CHAR);
154
    return(ISTREAM_STAT_READ_CHAR);
127
  eof:
155
  eof:
128
    return (ISTREAM_STAT_SYNTAX_ERROR);
156
    return(ISTREAM_STAT_SYNTAX_ERROR);
129
}
157
}
130
 
158
 
131
/*--------------------------------------------------------------------------*/
159
/*--------------------------------------------------------------------------*/
132
 
160
 
133
void
161
void
134
istream_setup PROTO_Z ()
162
istream_setup(void)
135
{
163
{
136
    istream_input_1.file = stdin;
164
    istream_input_1.file = stdin;
137
}
165
}
138
 
166
 
139
#ifdef FS_FAST
167
#ifdef FS_FAST
140
#undef istream_init
168
#undef istream_init
141
#endif /* defined (FS_FAST) */
169
#endif /* defined (FS_FAST) */
142
void
170
void
143
istream_init PROTO_N ((istream))
171
istream_init(IStreamP istream)
144
	     PROTO_T (IStreamP istream)
-
 
145
{
172
{
146
    istream->name = NIL (CStringP);
173
    istream->name = NIL(CStringP);
147
}
174
}
148
#ifdef FS_FAST
175
#ifdef FS_FAST
149
#define istream_init(is) ((is)->name = NIL (CStringP))
176
#define istream_init(is)	((is)->name = NIL(CStringP))
150
#endif /* defined (FS_FAST) */
177
#endif /* defined (FS_FAST) */
151
 
178
 
152
BoolT
179
BoolT
153
istream_open PROTO_N ((istream, name))
180
istream_open(IStreamP istream, CStringP name)
154
	     PROTO_T (IStreamP istream X
-
 
155
		      CStringP name)
-
 
156
{
181
{
157
    if ((istream->file = fopen (name, "r")) == NIL (FILE *)) {
182
    if ((istream->file = fopen(name, "r")) == NIL(FILE *)) {
158
	return (FALSE);
183
	return(FALSE);
159
    }
184
    }
160
    istream->buffer  = ALLOCATE_VECTOR (char, ISTREAM_BUFSIZE);
185
    istream->buffer  = ALLOCATE_VECTOR(char, ISTREAM_BUFSIZE);
161
    istream->limit   = &(istream->buffer [ISTREAM_BUFSIZE]);
186
    istream->limit   = &(istream->buffer[ISTREAM_BUFSIZE]);
162
    istream->line    = 1;
187
    istream->line    = 1;
163
    istream->name    = name;
188
    istream->name    = name;
164
    X__istream_fill_buffer (istream);
189
    X__istream_fill_buffer(istream);
165
    return (TRUE);
190
    return(TRUE);
166
}
191
}
167
 
192
 
168
void
193
void
169
istream_assign PROTO_N ((to, from))
194
istream_assign(IStreamP to,			IStreamP from)
170
	       PROTO_T (IStreamP to X
-
 
171
			IStreamP from)
-
 
172
{
195
{
173
    to->file      = from->file;
196
    to->file      = from->file;
174
    to->buffer    = from->buffer;
197
    to->buffer    = from->buffer;
175
    to->current   = from->current;
198
    to->current   = from->current;
176
    to->end       = from->end;
199
    to->end       = from->end;
Line 182... Line 205...
182
 
205
 
183
#ifdef FS_FAST
206
#ifdef FS_FAST
184
#undef istream_is_open
207
#undef istream_is_open
185
#endif /* defined (FS_FAST) */
208
#endif /* defined (FS_FAST) */
186
BoolT
209
BoolT
187
istream_is_open PROTO_N ((istream))
210
istream_is_open(IStreamP istream)
188
		PROTO_T (IStreamP istream)
-
 
189
{
211
{
190
    return (istream->name != NIL (CStringP));
212
    return(istream->name != NIL(CStringP));
191
}
213
}
192
#ifdef FS_FAST
214
#ifdef FS_FAST
193
#define istream_is_open(is) ((is)->name != NIL (CStringP))
215
#define istream_is_open(is)	((is)->name != NIL(CStringP))
194
#endif /* defined (FS_FAST) */
216
#endif /* defined (FS_FAST) */
195
 
217
 
196
BoolT
218
BoolT
197
istream_read_char PROTO_N ((istream, c_ref))
219
istream_read_char(IStreamP istream, char *c_ref)
198
		  PROTO_T (IStreamP istream X
-
 
199
			   char    *c_ref)
-
 
200
{
220
{
201
    char c;
221
    char c;
202
 
222
 
203
  redo:
223
  redo:
204
    switch (c = ISTREAM_READ_CHAR (istream)) {
224
    switch (c = ISTREAM_READ_CHAR(istream)) {
205
      case '\n':
225
      case '\n':
206
	istream_inc_line (istream);
226
	istream_inc_line(istream);
207
	break;
227
	break;
208
      case '\0':
228
      case '\0':
209
	ISTREAM_HANDLE_NULL (istream, redo, eof);
229
	ISTREAM_HANDLE_NULL(istream, redo, eof);
210
	break;
230
	break;
211
      default:
231
      default:
212
	break;
232
	break;
213
    }
233
    }
214
    *c_ref = c;
234
    *c_ref = c;
215
    return (TRUE);
235
    return(TRUE);
216
  eof:
236
  eof:
217
    return (FALSE);
237
    return(FALSE);
218
}
238
}
219
 
239
 
220
BoolT
240
BoolT
221
istream_peek_char PROTO_N ((istream, c_ref))
241
istream_peek_char(IStreamP istream, char *c_ref)
222
		  PROTO_T (IStreamP istream X
-
 
223
			   char    *c_ref)
-
 
224
{
242
{
225
    char c;
243
    char c;
226
 
244
 
227
  redo:
245
  redo:
228
    switch (c = ISTREAM_PEEK_CHAR (istream)) {
246
    switch (c = ISTREAM_PEEK_CHAR(istream)) {
229
      case '\0':
247
      case '\0':
230
	ISTREAM_HANDLE_NULL (istream, redo, eof);
248
	ISTREAM_HANDLE_NULL(istream, redo, eof);
231
	break;
249
	break;
232
      default:
250
      default:
233
	break;
251
	break;
234
    }
252
    }
235
    *c_ref = c;
253
    *c_ref = c;
236
    return (TRUE);
254
    return(TRUE);
237
  eof:
255
  eof:
238
    return (FALSE);
256
    return(FALSE);
239
}
257
}
240
 
258
 
241
IStreamStatusT
259
IStreamStatusT
242
istream_read_escaped_char PROTO_N ((istream, c_ref))
260
istream_read_escaped_char(IStreamP istream, char *c_ref)
243
			  PROTO_T (IStreamP istream X
-
 
244
				   char    *c_ref)
-
 
245
{
261
{
246
    char c;
262
    char c;
247
 
263
 
248
  redo:
264
  redo:
249
    switch (c = ISTREAM_READ_CHAR (istream)) {
265
    switch (c = ISTREAM_READ_CHAR(istream)) {
250
      case '\0':
266
      case '\0':
251
	ISTREAM_HANDLE_NULL (istream, redo, eof);
267
	ISTREAM_HANDLE_NULL(istream, redo, eof);
252
	*c_ref = c;
268
	*c_ref = c;
253
	return (ISTREAM_STAT_READ_CHAR);
269
	return(ISTREAM_STAT_READ_CHAR);
254
      case '\n':
270
      case '\n':
255
	istream_inc_line (istream);
271
	istream_inc_line(istream);
256
	return (ISTREAM_STAT_NO_CHAR);
272
	return(ISTREAM_STAT_NO_CHAR);
257
      case '0':
273
      case '0':
258
	*c_ref = '\0';
274
	*c_ref = '\0';
259
	return (ISTREAM_STAT_READ_CHAR);
275
	return(ISTREAM_STAT_READ_CHAR);
260
      case 'f': case 'F':
276
      case 'f': case 'F':
261
	*c_ref = '\f';
277
	*c_ref = '\f';
262
	return (ISTREAM_STAT_READ_CHAR);
278
	return(ISTREAM_STAT_READ_CHAR);
263
      case 'n': case 'N':
279
      case 'n': case 'N':
264
	*c_ref = '\n';
280
	*c_ref = '\n';
265
	return (ISTREAM_STAT_READ_CHAR);
281
	return(ISTREAM_STAT_READ_CHAR);
266
      case 'r': case 'R':
282
      case 'r': case 'R':
267
	*c_ref = '\r';
283
	*c_ref = '\r';
268
	return (ISTREAM_STAT_READ_CHAR);
284
	return(ISTREAM_STAT_READ_CHAR);
269
      case 't': case 'T':
285
      case 't': case 'T':
270
	*c_ref = '\t';
286
	*c_ref = '\t';
271
	return (ISTREAM_STAT_READ_CHAR);
287
	return(ISTREAM_STAT_READ_CHAR);
272
      case 'x': case 'X':
288
      case 'x': case 'X':
273
	return (istream_read_hex_char (istream, c_ref));
289
	return(istream_read_hex_char(istream, c_ref));
274
      default:
290
      default:
275
	*c_ref = c;
291
	*c_ref = c;
276
	return (ISTREAM_STAT_READ_CHAR);
292
	return(ISTREAM_STAT_READ_CHAR);
277
    }
293
    }
278
  eof:
294
  eof:
279
    return (ISTREAM_STAT_SYNTAX_ERROR);
295
    return(ISTREAM_STAT_SYNTAX_ERROR);
280
}
296
}
281
 
297
 
282
#ifdef FS_FAST
298
#ifdef FS_FAST
283
#undef istream_inc_line
299
#undef istream_inc_line
284
#endif /* defined (FS_FAST) */
300
#endif /* defined (FS_FAST) */
285
void
301
void
286
istream_inc_line PROTO_N ((istream))
302
istream_inc_line(IStreamP istream)
287
		 PROTO_T (IStreamP istream)
-
 
288
{
303
{
289
    istream->line ++;
304
    istream->line++;
290
}
305
}
291
#ifdef FS_FAST
306
#ifdef FS_FAST
292
#define istream_inc_line(is) ((is)->line ++)
307
#define istream_inc_line(is)	((is)->line++)
293
#endif /* defined (FS_FAST) */
308
#endif /* defined (FS_FAST) */
294
 
309
 
295
#ifdef FS_FAST
310
#ifdef FS_FAST
296
#undef istream_line
311
#undef istream_line
297
#endif /* defined (FS_FAST) */
312
#endif /* defined (FS_FAST) */
298
unsigned
313
unsigned
299
istream_line PROTO_N ((istream))
314
istream_line(IStreamP istream)
300
	     PROTO_T (IStreamP istream)
-
 
301
{
315
{
302
    return (istream->line);
316
    return(istream->line);
303
}
317
}
304
#ifdef FS_FAST
318
#ifdef FS_FAST
305
#define istream_line(is) ((is)->line)
319
#define istream_line(is)	((is)->line)
306
#endif /* defined (FS_FAST) */
320
#endif /* defined (FS_FAST) */
307
 
321
 
308
#ifdef FS_FAST
322
#ifdef FS_FAST
309
#undef istream_name
323
#undef istream_name
310
#endif /* defined (FS_FAST) */
324
#endif /* defined (FS_FAST) */
311
CStringP
325
CStringP
312
istream_name PROTO_N ((istream))
326
istream_name(IStreamP istream)
313
	     PROTO_T (IStreamP istream)
-
 
314
{
327
{
315
    return (istream->name);
328
    return(istream->name);
316
}
329
}
317
#ifdef FS_FAST
330
#ifdef FS_FAST
318
#define istream_name(is) ((is)->name)
331
#define istream_name(is)	((is)->name)
319
#endif /* defined (FS_FAST) */
332
#endif /* defined (FS_FAST) */
320
 
333
 
321
void
334
void
322
istream_close PROTO_N ((istream))
335
istream_close(IStreamP istream)
323
	      PROTO_T (IStreamP istream)
-
 
324
{
336
{
325
    (void) fclose (istream->file);
337
   (void)fclose(istream->file);
326
    if (istream != istream_input) {
338
    if (istream != istream_input) {
327
	DEALLOCATE (istream->buffer);
339
	DEALLOCATE(istream->buffer);
328
    }
340
    }
329
    istream_init (istream);
341
    istream_init(istream);
330
}
342
}
331
 
343
 
332
/*--------------------------------------------------------------------------*/
344
/*--------------------------------------------------------------------------*/
333
 
345
 
334
void
346
void
335
X__istream_fill_buffer PROTO_N ((istream))
347
X__istream_fill_buffer(IStreamP istream)
336
		       PROTO_T (IStreamP istream)
-
 
337
{
348
{
338
    SizeT bytes = fread ((GenericP) (istream->buffer), sizeof (char),
349
    SizeT bytes = fread((GenericP)(istream->buffer), sizeof(char),
339
			 (SizeT) (ISTREAM_BUFSIZE - 1), istream->file);
350
			(SizeT)(ISTREAM_BUFSIZE - 1), istream->file);
340
 
351
 
341
    if ((bytes == (SizeT) 0) && (ferror (istream->file))) {
352
    if ((bytes == (SizeT)0) && (ferror(istream->file))) {
342
	CStringP name = cstring_duplicate (istream->name);
353
	CStringP name = cstring_duplicate(istream->name);
343
 
354
 
344
	THROW_VALUE (XX_istream_read_error, name);
355
	THROW_VALUE(XX_istream_read_error, name);
345
	UNREACHED;
356
	UNREACHED;
346
    }
357
    }
347
    istream->current   = istream->buffer;
358
    istream->current   = istream->buffer;
348
    istream->end       = (istream->current + bytes);
359
    istream->end       = (istream->current + bytes);
349
    istream->read_last = FALSE;
360
    istream->read_last = FALSE;
350
    *(istream->end) ++ = '\0';
361
    *(istream->end)++ = '\0';
351
}
362
}