Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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
/**** bistream.c --- Binary input stream handling.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file implements the binary input stream facility specified in the file
68
 * "bistream.h".  See that file for more details.
69
 *
70
 **** Change Log:
71
 * $Log: bistream.c,v $
72
 * Revision 1.1.1.1  1998/01/17  15:57:17  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/12  11:45:16  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:12  smf
80
 * Initial import of os-interface shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
6 7u83 86
#include <stdio.h>
87
 
2 7u83 88
#include "bistream.h"
89
#include "cstring.h"
90
 
91
/*--------------------------------------------------------------------------*/
92
 
6 7u83 93
ExceptionP XX_bistream_read_error = EXCEPTION("error reading from binary stream");
2 7u83 94
 
95
/*--------------------------------------------------------------------------*/
96
 
97
void
6 7u83 98
bistream_init(BIStreamP bistream)
2 7u83 99
{
6 7u83 100
    bistream->name = NIL(CStringP);
2 7u83 101
}
102
 
103
BoolT
6 7u83 104
bistream_open(BIStreamP bistream,		       CStringP  name)
2 7u83 105
{
106
#ifdef FS_BINARY_STDIO
6 7u83 107
    if ((bistream->file = fopen(name, "rb")) == NIL(FILE *)) {
108
	return(FALSE);
2 7u83 109
    }
110
#else
6 7u83 111
    if ((bistream->file = fopen(name, "r")) == NIL(FILE *)) {
112
	return(FALSE);
2 7u83 113
    }
114
#endif /* defined (FS_BINARY_STDIO) */
115
    bistream->bytes = 0;
116
    bistream->name  = name;
6 7u83 117
    return(TRUE);
2 7u83 118
}
119
 
120
void
6 7u83 121
bistream_assign(BIStreamP to,			 BIStreamP from)
2 7u83 122
{
123
    to->file  = from->file;
124
    to->bytes = from->bytes;
125
    to->name  = from->name;
126
}
127
 
128
BoolT
6 7u83 129
bistream_is_open(BIStreamP bistream)
2 7u83 130
{
6 7u83 131
    return(bistream->name != NIL(CStringP));
2 7u83 132
}
133
 
134
unsigned
6 7u83 135
bistream_read_chars(BIStreamP bistream,			     unsigned  length ,
2 7u83 136
			     CStringP  chars)
137
{
6 7u83 138
    unsigned bytes_read = (unsigned)fread((GenericP)chars, sizeof(char),
139
					   (SizeT)length, bistream->file);
2 7u83 140
 
6 7u83 141
    if ((bytes_read == 0) && (ferror(bistream->file))) {
142
	CStringP name = cstring_duplicate(bistream->name);
2 7u83 143
 
6 7u83 144
	THROW_VALUE(XX_bistream_read_error, name);
2 7u83 145
	UNREACHED;
146
    }
147
    bistream->bytes += bytes_read;
6 7u83 148
    return(bytes_read);
2 7u83 149
}
150
 
151
unsigned
6 7u83 152
bistream_read_bytes(BIStreamP bistream,			     unsigned  length ,
2 7u83 153
			     ByteP     bytes)
154
{
6 7u83 155
    unsigned bytes_read = (unsigned)fread((GenericP)bytes, sizeof(ByteT),
156
					   (SizeT)length, bistream->file);
2 7u83 157
 
6 7u83 158
    if ((bytes_read == 0) && (ferror(bistream->file))) {
159
	CStringP name = cstring_duplicate(bistream->name);
2 7u83 160
 
6 7u83 161
	THROW_VALUE(XX_bistream_read_error, name);
2 7u83 162
	UNREACHED;
163
    }
164
    bistream->bytes += bytes_read;
6 7u83 165
    return(bytes_read);
2 7u83 166
}
167
 
168
BoolT
6 7u83 169
bistream_read_byte(BIStreamP bistream,			    ByteT    *byte_ref)
2 7u83 170
{
6 7u83 171
    int byte = fgetc(bistream->file);
2 7u83 172
 
173
    if (byte == EOF) {
6 7u83 174
	if (ferror(bistream->file)) {
175
	    CStringP name = cstring_duplicate(bistream->name);
2 7u83 176
 
6 7u83 177
	    THROW_VALUE(XX_bistream_read_error, name);
2 7u83 178
	    UNREACHED;
6 7u83 179
	} else if (feof(bistream->file)) {
180
	    return(FALSE);
2 7u83 181
	}
182
    }
6 7u83 183
    bistream->bytes++;
184
    *byte_ref = (ByteT)byte;
185
    return(TRUE);
2 7u83 186
}
187
 
188
unsigned
6 7u83 189
bistream_byte(BIStreamP bistream)
2 7u83 190
{
6 7u83 191
    return(bistream->bytes);
2 7u83 192
}
193
 
194
CStringP
6 7u83 195
bistream_name(BIStreamP bistream)
2 7u83 196
{
6 7u83 197
    return(bistream->name);
2 7u83 198
}
199
 
200
void
6 7u83 201
bistream_rewind(BIStreamP bistream)
2 7u83 202
{
203
#ifdef FS_ANSI_ENVIRON
6 7u83 204
    rewind(bistream->file);
2 7u83 205
#else
6 7u83 206
   (void)fseek(bistream->file,(long)0, SEEK_SET);
2 7u83 207
#endif /* defined (FS_REWIND) */
208
}
209
 
210
void
6 7u83 211
bistream_close(BIStreamP bistream)
2 7u83 212
{
6 7u83 213
   (void)fclose(bistream->file);
214
    bistream_init(bistream);
2 7u83 215
}