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-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
 */
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
*/
29
 
59
 
30
 
60
 
31
/**** bistream.c --- Binary input stream handling.
61
/**** bistream.c --- Binary input stream handling.
32
 *
62
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
64
 *
35
 **** Commentary:
65
 **** Commentary:
36
 *
66
 *
37
 * This file implements the binary input stream facility specified in the file
67
 * This file implements the binary input stream facility specified in the file
38
 * "bistream.h".  See that file for more details.
68
 * "bistream.h".  See that file for more details.
39
 *
69
 *
40
 **** Change Log:
70
 **** Change Log:
41
 * $Log: bistream.c,v $
71
 * $Log: bistream.c,v $
Line 43... Line 73...
43
 * First version to be checked into rolling release.
73
 * First version to be checked into rolling release.
44
 *
74
 *
45
 * Revision 1.2  1994/12/12  11:45:16  smf
75
 * Revision 1.2  1994/12/12  11:45:16  smf
46
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
76
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
47
 * OSSG C Coding Standards.
77
 * OSSG C Coding Standards.
48
 *
78
 *
49
 * Revision 1.1.1.1  1994/07/25  16:06:12  smf
79
 * Revision 1.1.1.1  1994/07/25  16:06:12  smf
50
 * Initial import of os-interface shared files.
80
 * Initial import of os-interface shared files.
51
 *
81
 *
52
**/
82
**/
53
 
83
 
54
/****************************************************************************/
84
/****************************************************************************/
-
 
85
 
-
 
86
#include <stdio.h>
55
 
87
 
56
#include "bistream.h"
88
#include "bistream.h"
57
#include "cstring.h"
89
#include "cstring.h"
58
 
90
 
59
/*--------------------------------------------------------------------------*/
91
/*--------------------------------------------------------------------------*/
60
 
92
 
61
ExceptionP XX_bistream_read_error = EXCEPTION ("error reading from binary stream");
93
ExceptionP XX_bistream_read_error = EXCEPTION("error reading from binary stream");
62
 
94
 
63
/*--------------------------------------------------------------------------*/
95
/*--------------------------------------------------------------------------*/
64
 
96
 
65
void
97
void
66
bistream_init PROTO_N ((bistream))
98
bistream_init(BIStreamP bistream)
67
	      PROTO_T (BIStreamP bistream)
-
 
68
{
99
{
69
    bistream->name = NIL (CStringP);
100
    bistream->name = NIL(CStringP);
70
}
101
}
71
 
102
 
72
BoolT
103
BoolT
73
bistream_open PROTO_N ((bistream, name))
104
bistream_open(BIStreamP bistream,		       CStringP  name)
74
	      PROTO_T (BIStreamP bistream X
-
 
75
		       CStringP  name)
-
 
76
{
105
{
77
#ifdef FS_BINARY_STDIO
106
#ifdef FS_BINARY_STDIO
78
    if ((bistream->file = fopen (name, "rb")) == NIL (FILE *)) {
107
    if ((bistream->file = fopen(name, "rb")) == NIL(FILE *)) {
79
	return (FALSE);
108
	return(FALSE);
80
    }
109
    }
81
#else
110
#else
82
    if ((bistream->file = fopen (name, "r")) == NIL (FILE *)) {
111
    if ((bistream->file = fopen(name, "r")) == NIL(FILE *)) {
83
	return (FALSE);
112
	return(FALSE);
84
    }
113
    }
85
#endif /* defined (FS_BINARY_STDIO) */
114
#endif /* defined (FS_BINARY_STDIO) */
86
    bistream->bytes = 0;
115
    bistream->bytes = 0;
87
    bistream->name  = name;
116
    bistream->name  = name;
88
    return (TRUE);
117
    return(TRUE);
89
}
118
}
90
 
119
 
91
void
120
void
92
bistream_assign PROTO_N ((to, from))
121
bistream_assign(BIStreamP to,			 BIStreamP from)
93
		PROTO_T (BIStreamP to X
-
 
94
			 BIStreamP from)
-
 
95
{
122
{
96
    to->file  = from->file;
123
    to->file  = from->file;
97
    to->bytes = from->bytes;
124
    to->bytes = from->bytes;
98
    to->name  = from->name;
125
    to->name  = from->name;
99
}
126
}
100
 
127
 
101
BoolT
128
BoolT
102
bistream_is_open PROTO_N ((bistream))
129
bistream_is_open(BIStreamP bistream)
103
		 PROTO_T (BIStreamP bistream)
-
 
104
{
130
{
105
    return (bistream->name != NIL (CStringP));
131
    return(bistream->name != NIL(CStringP));
106
}
132
}
107
 
133
 
108
unsigned
134
unsigned
109
bistream_read_chars PROTO_N ((bistream, length, chars))
135
bistream_read_chars(BIStreamP bistream,			     unsigned  length ,
110
		    PROTO_T (BIStreamP bistream X
-
 
111
			     unsigned  length X
-
 
112
			     CStringP  chars)
136
			     CStringP  chars)
113
{
137
{
114
    unsigned bytes_read = (unsigned) fread ((GenericP) chars, sizeof (char),
138
    unsigned bytes_read = (unsigned)fread((GenericP)chars, sizeof(char),
115
					    (SizeT) length, bistream->file);
139
					   (SizeT)length, bistream->file);
116
 
140
 
117
    if ((bytes_read == 0) && (ferror (bistream->file))) {
141
    if ((bytes_read == 0) && (ferror(bistream->file))) {
118
	CStringP name = cstring_duplicate (bistream->name);
142
	CStringP name = cstring_duplicate(bistream->name);
119
 
143
 
120
	THROW_VALUE (XX_bistream_read_error, name);
144
	THROW_VALUE(XX_bistream_read_error, name);
121
	UNREACHED;
145
	UNREACHED;
122
    }
146
    }
123
    bistream->bytes += bytes_read;
147
    bistream->bytes += bytes_read;
124
    return (bytes_read);
148
    return(bytes_read);
125
}
149
}
126
 
150
 
127
unsigned
151
unsigned
128
bistream_read_bytes PROTO_N ((bistream, length, bytes))
152
bistream_read_bytes(BIStreamP bistream,			     unsigned  length ,
129
		    PROTO_T (BIStreamP bistream X
-
 
130
			     unsigned  length X
-
 
131
			     ByteP     bytes)
153
			     ByteP     bytes)
132
{
154
{
133
    unsigned bytes_read = (unsigned) fread ((GenericP) bytes, sizeof (ByteT),
155
    unsigned bytes_read = (unsigned)fread((GenericP)bytes, sizeof(ByteT),
134
					    (SizeT) length, bistream->file);
156
					   (SizeT)length, bistream->file);
135
 
157
 
136
    if ((bytes_read == 0) && (ferror (bistream->file))) {
158
    if ((bytes_read == 0) && (ferror(bistream->file))) {
137
	CStringP name = cstring_duplicate (bistream->name);
159
	CStringP name = cstring_duplicate(bistream->name);
138
 
160
 
139
	THROW_VALUE (XX_bistream_read_error, name);
161
	THROW_VALUE(XX_bistream_read_error, name);
140
	UNREACHED;
162
	UNREACHED;
141
    }
163
    }
142
    bistream->bytes += bytes_read;
164
    bistream->bytes += bytes_read;
143
    return (bytes_read);
165
    return(bytes_read);
144
}
166
}
145
 
167
 
146
BoolT
168
BoolT
147
bistream_read_byte PROTO_N ((bistream, byte_ref))
169
bistream_read_byte(BIStreamP bistream,			    ByteT    *byte_ref)
148
		   PROTO_T (BIStreamP bistream X
-
 
149
			    ByteT    *byte_ref)
-
 
150
{
170
{
151
    int byte = fgetc (bistream->file);
171
    int byte = fgetc(bistream->file);
152
 
172
 
153
    if (byte == EOF) {
173
    if (byte == EOF) {
154
	if (ferror (bistream->file)) {
174
	if (ferror(bistream->file)) {
155
	    CStringP name = cstring_duplicate (bistream->name);
175
	    CStringP name = cstring_duplicate(bistream->name);
156
 
176
 
157
	    THROW_VALUE (XX_bistream_read_error, name);
177
	    THROW_VALUE(XX_bistream_read_error, name);
158
	    UNREACHED;
178
	    UNREACHED;
159
	} else if (feof (bistream->file)) {
179
	} else if (feof(bistream->file)) {
160
	    return (FALSE);
180
	    return(FALSE);
161
	}
181
	}
162
    }
182
    }
163
    bistream->bytes ++;
183
    bistream->bytes++;
164
    *byte_ref = (ByteT) byte;
184
    *byte_ref = (ByteT)byte;
165
    return (TRUE);
185
    return(TRUE);
166
}
186
}
167
 
187
 
168
unsigned
188
unsigned
169
bistream_byte PROTO_N ((bistream))
189
bistream_byte(BIStreamP bistream)
170
	      PROTO_T (BIStreamP bistream)
-
 
171
{
190
{
172
    return (bistream->bytes);
191
    return(bistream->bytes);
173
}
192
}
174
 
193
 
175
CStringP
194
CStringP
176
bistream_name PROTO_N ((bistream))
195
bistream_name(BIStreamP bistream)
177
	      PROTO_T (BIStreamP bistream)
-
 
178
{
196
{
179
    return (bistream->name);
197
    return(bistream->name);
180
}
198
}
181
 
199
 
182
void
200
void
183
bistream_rewind PROTO_N ((bistream))
201
bistream_rewind(BIStreamP bistream)
184
		PROTO_T (BIStreamP bistream)
-
 
185
{
202
{
186
#ifdef FS_ANSI_ENVIRON
203
#ifdef FS_ANSI_ENVIRON
187
    rewind (bistream->file);
204
    rewind(bistream->file);
188
#else
205
#else
189
    (void) fseek (bistream->file, (long) 0, SEEK_SET);
206
   (void)fseek(bistream->file,(long)0, SEEK_SET);
190
#endif /* defined (FS_REWIND) */
207
#endif /* defined (FS_REWIND) */
191
}
208
}
192
 
209
 
193
void
210
void
194
bistream_close PROTO_N ((bistream))
211
bistream_close(BIStreamP bistream)
195
	       PROTO_T (BIStreamP bistream)
-
 
196
{
212
{
197
    (void) fclose (bistream->file);
213
   (void)fclose(bistream->file);
198
    bistream_init (bistream);
214
    bistream_init(bistream);
199
}
215
}