Subversion Repositories tendra.SVN

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2006 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.c --- Input stream handling.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file implements the input stream facility specified in the file
68
 * "istream.h".  See that file for more details.
69
 *
70
 **** Change Log:
71
 * $Log: istream.c,v $
72
 * Revision 1.1.1.1  1998/01/17  15:57:18  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/12  11:45:41  smf
76
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
77
 * OSSG C Coding Standards.
78
 *
79
 * Revision 1.1.1.1  1994/07/25  16:06:10  smf
80
 * Initial import of os-interface shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
86
#include "istream.h"
87
#include "cstring.h"
88
#include "syntax.h"
89
 
90
/*--------------------------------------------------------------------------*/
91
 
92
#define ISTREAM_BUFSIZE 8193
93
 
94
/*--------------------------------------------------------------------------*/
95
 
7 7u83 96
ExceptionP XX_istream_read_error = EXCEPTION("error reading from stream");
2 7u83 97
 
98
/*--------------------------------------------------------------------------*/
99
 
7 7u83 100
static char istream_input_buffer[ISTREAM_BUFSIZE];
2 7u83 101
 
102
static IStreamT		istream_input_1 = {
7 7u83 103
    NIL(FILE *),
104
    & (istream_input_buffer[0]),
105
    & (istream_input_buffer[ISTREAM_BUFSIZE - 1]),
106
    & (istream_input_buffer[ISTREAM_BUFSIZE]),
107
    & (istream_input_buffer[ISTREAM_BUFSIZE]),
2 7u83 108
    1,
109
    "<stdin>",
110
    FALSE
111
};
112
 
113
IStreamT	 *const istream_input = &istream_input_1;
114
 
115
/*--------------------------------------------------------------------------*/
116
 
117
static IStreamStatusT
7 7u83 118
istream_read_hex_char(IStreamP istream,			       char    *c_ref)
2 7u83 119
{
120
    int value;
121
    int tmp;
122
    char c;
123
 
124
  redo1:
7 7u83 125
    switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 126
      case '\0':
7 7u83 127
	ISTREAM_HANDLE_NULL(istream, redo1, eof);
128
	return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 129
      case '\n':
7 7u83 130
	istream_inc_line(istream);
131
	return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 132
      default:
7 7u83 133
	if (((value = syntax_value(c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
134
	    return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 135
	}
136
	tmp = value;
137
	break;
138
    }
139
  redo2:
7 7u83 140
    switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 141
      case '\0':
7 7u83 142
	ISTREAM_HANDLE_NULL(istream, redo2, eof);
143
	return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 144
      case '\n':
7 7u83 145
	istream_inc_line(istream);
146
	return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 147
      default:
7 7u83 148
	if (((value = syntax_value(c)) == SYNTAX_NO_VALUE) || (value >= 16)) {
149
	    return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 150
	}
151
	break;
152
    }
7 7u83 153
    *c_ref = (char)((tmp * 16) + value);
154
    return(ISTREAM_STAT_READ_CHAR);
2 7u83 155
  eof:
7 7u83 156
    return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 157
}
158
 
159
/*--------------------------------------------------------------------------*/
160
 
161
void
7 7u83 162
istream_setup(void)
2 7u83 163
{
164
    istream_input_1.file = stdin;
165
}
166
 
167
#ifdef FS_FAST
168
#undef istream_init
169
#endif /* defined (FS_FAST) */
170
void
7 7u83 171
istream_init(IStreamP istream)
2 7u83 172
{
7 7u83 173
    istream->name = NIL(CStringP);
2 7u83 174
}
175
#ifdef FS_FAST
7 7u83 176
#define istream_init(is)	((is)->name = NIL(CStringP))
2 7u83 177
#endif /* defined (FS_FAST) */
178
 
179
BoolT
7 7u83 180
istream_open(IStreamP istream,		      CStringP name)
2 7u83 181
{
7 7u83 182
    if ((istream->file = fopen(name, "r")) == NIL(FILE *)) {
183
	return(FALSE);
2 7u83 184
    }
7 7u83 185
    istream->buffer  = ALLOCATE_VECTOR(char, ISTREAM_BUFSIZE);
186
    istream->limit   = & (istream->buffer[ISTREAM_BUFSIZE]);
2 7u83 187
    istream->line    = 1;
188
    istream->name    = name;
7 7u83 189
    X__istream_fill_buffer(istream);
190
    return(TRUE);
2 7u83 191
}
192
 
193
void
7 7u83 194
istream_assign(IStreamP to,			IStreamP from)
2 7u83 195
{
196
    to->file      = from->file;
197
    to->buffer    = from->buffer;
198
    to->current   = from->current;
199
    to->end       = from->end;
200
    to->limit     = from->limit;
201
    to->line      = from->line;
202
    to->name      = from->name;
203
    to->read_last = from->read_last;
204
}
205
 
206
#ifdef FS_FAST
207
#undef istream_is_open
208
#endif /* defined (FS_FAST) */
209
BoolT
7 7u83 210
istream_is_open(IStreamP istream)
2 7u83 211
{
7 7u83 212
    return(istream->name != NIL(CStringP));
2 7u83 213
}
214
#ifdef FS_FAST
7 7u83 215
#define istream_is_open(is)	((is)->name != NIL(CStringP))
2 7u83 216
#endif /* defined (FS_FAST) */
217
 
218
BoolT
7 7u83 219
istream_read_char(IStreamP istream,			   char    *c_ref)
2 7u83 220
{
221
    char c;
222
 
223
  redo:
7 7u83 224
    switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 225
      case '\n':
7 7u83 226
	istream_inc_line(istream);
2 7u83 227
	break;
228
      case '\0':
7 7u83 229
	ISTREAM_HANDLE_NULL(istream, redo, eof);
2 7u83 230
	break;
231
      default:
232
	break;
233
    }
234
    *c_ref = c;
7 7u83 235
    return(TRUE);
2 7u83 236
  eof:
7 7u83 237
    return(FALSE);
2 7u83 238
}
239
 
240
BoolT
7 7u83 241
istream_peek_char(IStreamP istream,			   char    *c_ref)
2 7u83 242
{
243
    char c;
244
 
245
  redo:
7 7u83 246
    switch (c = ISTREAM_PEEK_CHAR(istream)) {
2 7u83 247
      case '\0':
7 7u83 248
	ISTREAM_HANDLE_NULL(istream, redo, eof);
2 7u83 249
	break;
250
      default:
251
	break;
252
    }
253
    *c_ref = c;
7 7u83 254
    return(TRUE);
2 7u83 255
  eof:
7 7u83 256
    return(FALSE);
2 7u83 257
}
258
 
259
IStreamStatusT
7 7u83 260
istream_read_escaped_char(IStreamP istream,				   char    *c_ref)
2 7u83 261
{
262
    char c;
263
 
264
  redo:
7 7u83 265
    switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 266
      case '\0':
7 7u83 267
	ISTREAM_HANDLE_NULL(istream, redo, eof);
2 7u83 268
	*c_ref = c;
7 7u83 269
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 270
      case '\n':
7 7u83 271
	istream_inc_line(istream);
272
	return(ISTREAM_STAT_NO_CHAR);
2 7u83 273
      case '0':
274
	*c_ref = '\0';
7 7u83 275
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 276
      case 'f': case 'F':
277
	*c_ref = '\f';
7 7u83 278
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 279
      case 'n': case 'N':
280
	*c_ref = '\n';
7 7u83 281
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 282
      case 'r': case 'R':
283
	*c_ref = '\r';
7 7u83 284
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 285
      case 't': case 'T':
286
	*c_ref = '\t';
7 7u83 287
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 288
      case 'x': case 'X':
7 7u83 289
	return(istream_read_hex_char(istream, c_ref));
2 7u83 290
      default:
291
	*c_ref = c;
7 7u83 292
	return(ISTREAM_STAT_READ_CHAR);
2 7u83 293
    }
294
  eof:
7 7u83 295
    return(ISTREAM_STAT_SYNTAX_ERROR);
2 7u83 296
}
297
 
298
#ifdef FS_FAST
299
#undef istream_inc_line
300
#endif /* defined (FS_FAST) */
301
void
7 7u83 302
istream_inc_line(IStreamP istream)
2 7u83 303
{
7 7u83 304
    istream->line++;
2 7u83 305
}
306
#ifdef FS_FAST
7 7u83 307
#define istream_inc_line(is)	((is)->line++)
2 7u83 308
#endif /* defined (FS_FAST) */
309
 
310
#ifdef FS_FAST
311
#undef istream_line
312
#endif /* defined (FS_FAST) */
313
unsigned
7 7u83 314
istream_line(IStreamP istream)
2 7u83 315
{
7 7u83 316
    return(istream->line);
2 7u83 317
}
318
#ifdef FS_FAST
7 7u83 319
#define istream_line(is)	((is)->line)
2 7u83 320
#endif /* defined (FS_FAST) */
321
 
322
#ifdef FS_FAST
323
#undef istream_name
324
#endif /* defined (FS_FAST) */
325
CStringP
7 7u83 326
istream_name(IStreamP istream)
2 7u83 327
{
7 7u83 328
    return(istream->name);
2 7u83 329
}
330
#ifdef FS_FAST
7 7u83 331
#define istream_name(is)	((is)->name)
2 7u83 332
#endif /* defined (FS_FAST) */
333
 
334
void
7 7u83 335
istream_close(IStreamP istream)
2 7u83 336
{
7 7u83 337
   (void)fclose(istream->file);
2 7u83 338
    if (istream != istream_input) {
7 7u83 339
	DEALLOCATE(istream->buffer);
2 7u83 340
    }
7 7u83 341
    istream_init(istream);
2 7u83 342
}
343
 
344
/*--------------------------------------------------------------------------*/
345
 
346
void
7 7u83 347
X__istream_fill_buffer(IStreamP istream)
2 7u83 348
{
7 7u83 349
    SizeT bytes = fread((GenericP)(istream->buffer), sizeof(char),
350
			(SizeT)(ISTREAM_BUFSIZE - 1), istream->file);
2 7u83 351
 
7 7u83 352
    if ((bytes == (SizeT)0) && (ferror(istream->file))) {
353
	CStringP name = cstring_duplicate(istream->name);
2 7u83 354
 
7 7u83 355
	THROW_VALUE(XX_istream_read_error, name);
2 7u83 356
	UNREACHED;
357
    }
358
    istream->current   = istream->buffer;
359
    istream->end       = (istream->current + bytes);
360
    istream->read_last = FALSE;
7 7u83 361
    *(istream->end)++ = '\0';
2 7u83 362
}