Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5/src/tools/tld/bistream.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/**** bistream.c --- Binary input stream handling.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 **** Commentary:
36
 *
37
 * This file implements the binary input stream facility specified in the file
38
 * "bistream.h".  See that file for more details.
39
 *
40
 **** Change Log:
41
 * $Log: bistream.c,v $
42
 * Revision 1.1.1.1  1998/01/17  15:57:17  release
43
 * First version to be checked into rolling release.
44
 *
45
 * Revision 1.2  1994/12/12  11:45:16  smf
46
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
47
 * OSSG C Coding Standards.
48
 *
49
 * Revision 1.1.1.1  1994/07/25  16:06:12  smf
50
 * Initial import of os-interface shared files.
51
 *
52
**/
53
 
54
/****************************************************************************/
55
 
56
#include "bistream.h"
57
#include "cstring.h"
58
 
59
/*--------------------------------------------------------------------------*/
60
 
61
ExceptionP XX_bistream_read_error = EXCEPTION ("error reading from binary stream");
62
 
63
/*--------------------------------------------------------------------------*/
64
 
65
void
66
bistream_init PROTO_N ((bistream))
67
	      PROTO_T (BIStreamP bistream)
68
{
69
    bistream->name = NIL (CStringP);
70
}
71
 
72
BoolT
73
bistream_open PROTO_N ((bistream, name))
74
	      PROTO_T (BIStreamP bistream X
75
		       CStringP  name)
76
{
77
#ifdef FS_BINARY_STDIO
78
    if ((bistream->file = fopen (name, "rb")) == NIL (FILE *)) {
79
	return (FALSE);
80
    }
81
#else
82
    if ((bistream->file = fopen (name, "r")) == NIL (FILE *)) {
83
	return (FALSE);
84
    }
85
#endif /* defined (FS_BINARY_STDIO) */
86
    bistream->bytes = 0;
87
    bistream->name  = name;
88
    return (TRUE);
89
}
90
 
91
void
92
bistream_assign PROTO_N ((to, from))
93
		PROTO_T (BIStreamP to X
94
			 BIStreamP from)
95
{
96
    to->file  = from->file;
97
    to->bytes = from->bytes;
98
    to->name  = from->name;
99
}
100
 
101
BoolT
102
bistream_is_open PROTO_N ((bistream))
103
		 PROTO_T (BIStreamP bistream)
104
{
105
    return (bistream->name != NIL (CStringP));
106
}
107
 
108
unsigned
109
bistream_read_chars PROTO_N ((bistream, length, chars))
110
		    PROTO_T (BIStreamP bistream X
111
			     unsigned  length X
112
			     CStringP  chars)
113
{
114
    unsigned bytes_read = (unsigned) fread ((GenericP) chars, sizeof (char),
115
					    (SizeT) length, bistream->file);
116
 
117
    if ((bytes_read == 0) && (ferror (bistream->file))) {
118
	CStringP name = cstring_duplicate (bistream->name);
119
 
120
	THROW_VALUE (XX_bistream_read_error, name);
121
	UNREACHED;
122
    }
123
    bistream->bytes += bytes_read;
124
    return (bytes_read);
125
}
126
 
127
unsigned
128
bistream_read_bytes PROTO_N ((bistream, length, bytes))
129
		    PROTO_T (BIStreamP bistream X
130
			     unsigned  length X
131
			     ByteP     bytes)
132
{
133
    unsigned bytes_read = (unsigned) fread ((GenericP) bytes, sizeof (ByteT),
134
					    (SizeT) length, bistream->file);
135
 
136
    if ((bytes_read == 0) && (ferror (bistream->file))) {
137
	CStringP name = cstring_duplicate (bistream->name);
138
 
139
	THROW_VALUE (XX_bistream_read_error, name);
140
	UNREACHED;
141
    }
142
    bistream->bytes += bytes_read;
143
    return (bytes_read);
144
}
145
 
146
BoolT
147
bistream_read_byte PROTO_N ((bistream, byte_ref))
148
		   PROTO_T (BIStreamP bistream X
149
			    ByteT    *byte_ref)
150
{
151
    int byte = fgetc (bistream->file);
152
 
153
    if (byte == EOF) {
154
	if (ferror (bistream->file)) {
155
	    CStringP name = cstring_duplicate (bistream->name);
156
 
157
	    THROW_VALUE (XX_bistream_read_error, name);
158
	    UNREACHED;
159
	} else if (feof (bistream->file)) {
160
	    return (FALSE);
161
	}
162
    }
163
    bistream->bytes ++;
164
    *byte_ref = (ByteT) byte;
165
    return (TRUE);
166
}
167
 
168
unsigned
169
bistream_byte PROTO_N ((bistream))
170
	      PROTO_T (BIStreamP bistream)
171
{
172
    return (bistream->bytes);
173
}
174
 
175
CStringP
176
bistream_name PROTO_N ((bistream))
177
	      PROTO_T (BIStreamP bistream)
178
{
179
    return (bistream->name);
180
}
181
 
182
void
183
bistream_rewind PROTO_N ((bistream))
184
		PROTO_T (BIStreamP bistream)
185
{
186
#ifdef FS_ANSI_ENVIRON
187
    rewind (bistream->file);
188
#else
189
    (void) fseek (bistream->file, (long) 0, SEEK_SET);
190
#endif /* defined (FS_REWIND) */
191
}
192
 
193
void
194
bistream_close PROTO_N ((bistream))
195
	       PROTO_T (BIStreamP bistream)
196
{
197
    (void) fclose (bistream->file);
198
    bistream_init (bistream);
199
}