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 |
/**** bostream.c --- Binary output stream handling.
|
|
|
32 |
*
|
|
|
33 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
34 |
*
|
|
|
35 |
**** Commentary:
|
|
|
36 |
*
|
|
|
37 |
* This file implements the binary output stream facility specified in the
|
|
|
38 |
* file "bostream.h". See that file for more details.
|
|
|
39 |
*
|
|
|
40 |
**** Change Log:
|
|
|
41 |
* $Log: bostream.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:21 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:14 smf
|
|
|
50 |
* Initial import of os-interface shared files.
|
|
|
51 |
*
|
|
|
52 |
**/
|
|
|
53 |
|
|
|
54 |
/****************************************************************************/
|
|
|
55 |
|
|
|
56 |
#include "bostream.h"
|
|
|
57 |
#include "cstring.h"
|
|
|
58 |
|
|
|
59 |
/*--------------------------------------------------------------------------*/
|
|
|
60 |
|
|
|
61 |
ExceptionP XX_bostream_write_error = EXCEPTION ("error writing to binary stream");
|
|
|
62 |
|
|
|
63 |
/*--------------------------------------------------------------------------*/
|
|
|
64 |
|
|
|
65 |
void
|
|
|
66 |
bostream_init PROTO_N ((bostream))
|
|
|
67 |
PROTO_T (BOStreamP bostream)
|
|
|
68 |
{
|
|
|
69 |
bostream->name = NIL (CStringP);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
BoolT
|
|
|
73 |
bostream_open PROTO_N ((bostream, name))
|
|
|
74 |
PROTO_T (BOStreamP bostream X
|
|
|
75 |
CStringP name)
|
|
|
76 |
{
|
|
|
77 |
#ifdef FS_BINARY_STDIO
|
|
|
78 |
if ((bostream->file = fopen (name, "wb")) == NIL (FILE *)) {
|
|
|
79 |
return (FALSE);
|
|
|
80 |
}
|
|
|
81 |
#else
|
|
|
82 |
if ((bostream->file = fopen (name, "w")) == NIL (FILE *)) {
|
|
|
83 |
return (FALSE);
|
|
|
84 |
}
|
|
|
85 |
#endif /* defined (FS_BINARY_STDIO) */
|
|
|
86 |
bostream->name = name;
|
|
|
87 |
return (TRUE);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
void
|
|
|
91 |
bostream_assign PROTO_N ((to, from))
|
|
|
92 |
PROTO_T (BOStreamP to X
|
|
|
93 |
BOStreamP from)
|
|
|
94 |
{
|
|
|
95 |
to->file = from->file;
|
|
|
96 |
to->name = from->name;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
BoolT
|
|
|
100 |
bostream_is_open PROTO_N ((bostream))
|
|
|
101 |
PROTO_T (BOStreamP bostream)
|
|
|
102 |
{
|
|
|
103 |
return (bostream->name != NIL (CStringP));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
void
|
|
|
107 |
bostream_write_chars PROTO_N ((bostream, length, chars))
|
|
|
108 |
PROTO_T (BOStreamP bostream X
|
|
|
109 |
unsigned length X
|
|
|
110 |
CStringP chars)
|
|
|
111 |
{
|
|
|
112 |
unsigned bytes_read = (unsigned) fwrite ((GenericP) chars, sizeof (char),
|
|
|
113 |
(SizeT) length, bostream->file);
|
|
|
114 |
|
|
|
115 |
if ((bytes_read != length) && (ferror (bostream->file))) {
|
|
|
116 |
CStringP name = cstring_duplicate (bostream->name);
|
|
|
117 |
|
|
|
118 |
THROW_VALUE (XX_bostream_write_error, name);
|
|
|
119 |
UNREACHED;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
void
|
|
|
124 |
bostream_write_bytes PROTO_N ((bostream, length, bytes))
|
|
|
125 |
PROTO_T (BOStreamP bostream X
|
|
|
126 |
unsigned length X
|
|
|
127 |
ByteP bytes)
|
|
|
128 |
{
|
|
|
129 |
unsigned bytes_read = (unsigned) fwrite ((GenericP) bytes, sizeof (ByteT),
|
|
|
130 |
(SizeT) length, bostream->file);
|
|
|
131 |
|
|
|
132 |
if ((bytes_read != length) && (ferror (bostream->file))) {
|
|
|
133 |
CStringP name = cstring_duplicate (bostream->name);
|
|
|
134 |
|
|
|
135 |
THROW_VALUE (XX_bostream_write_error, name);
|
|
|
136 |
UNREACHED;
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
void
|
|
|
141 |
bostream_write_byte PROTO_N ((bostream, byte))
|
|
|
142 |
PROTO_T (BOStreamP bostream X
|
|
|
143 |
ByteT byte)
|
|
|
144 |
{
|
|
|
145 |
if ((fputc ((int) byte, bostream->file) == EOF) &&
|
|
|
146 |
(ferror (bostream->file))) {
|
|
|
147 |
CStringP name = cstring_duplicate (bostream->name);
|
|
|
148 |
|
|
|
149 |
THROW_VALUE (XX_bostream_write_error, name);
|
|
|
150 |
UNREACHED;
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
CStringP
|
|
|
155 |
bostream_name PROTO_N ((bostream))
|
|
|
156 |
PROTO_T (BOStreamP bostream)
|
|
|
157 |
{
|
|
|
158 |
return (bostream->name);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
void
|
|
|
162 |
bostream_close PROTO_N ((bostream))
|
|
|
163 |
PROTO_T (BOStreamP bostream)
|
|
|
164 |
{
|
|
|
165 |
if (fclose (bostream->file)) {
|
|
|
166 |
CStringP name = cstring_duplicate (bostream->name);
|
|
|
167 |
|
|
|
168 |
THROW_VALUE (XX_bostream_write_error, name);
|
|
|
169 |
UNREACHED;
|
|
|
170 |
}
|
|
|
171 |
bostream_init (bostream);
|
|
|
172 |
}
|