Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
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
*/
Line 56... Line 86...
56
#include "bostream.h"
86
#include "bostream.h"
57
#include "cstring.h"
87
#include "cstring.h"
58
 
88
 
59
/*--------------------------------------------------------------------------*/
89
/*--------------------------------------------------------------------------*/
60
 
90
 
61
ExceptionP XX_bostream_write_error = EXCEPTION ("error writing to binary stream");
91
ExceptionP XX_bostream_write_error = EXCEPTION("error writing to binary stream");
62
 
92
 
63
/*--------------------------------------------------------------------------*/
93
/*--------------------------------------------------------------------------*/
64
 
94
 
65
void
95
void
66
bostream_init PROTO_N ((bostream))
96
bostream_init(BOStreamP bostream)
67
	      PROTO_T (BOStreamP bostream)
-
 
68
{
97
{
69
    bostream->name = NIL (CStringP);
98
    bostream->name = NIL(CStringP);
70
}
99
}
71
 
100
 
72
BoolT
101
BoolT
73
bostream_open PROTO_N ((bostream, name))
102
bostream_open(BOStreamP bostream,		       CStringP  name)
74
	      PROTO_T (BOStreamP bostream X
-
 
75
		       CStringP  name)
-
 
76
{
103
{
77
#ifdef FS_BINARY_STDIO
104
#ifdef FS_BINARY_STDIO
78
    if ((bostream->file = fopen (name, "wb")) == NIL (FILE *)) {
105
    if ((bostream->file = fopen(name, "wb")) == NIL(FILE *)) {
79
	return (FALSE);
106
	return(FALSE);
80
    }
107
    }
81
#else
108
#else
82
    if ((bostream->file = fopen (name, "w")) == NIL (FILE *)) {
109
    if ((bostream->file = fopen(name, "w")) == NIL(FILE *)) {
83
	return (FALSE);
110
	return(FALSE);
84
    }
111
    }
85
#endif /* defined (FS_BINARY_STDIO) */
112
#endif /* defined (FS_BINARY_STDIO) */
86
    bostream->name  = name;
113
    bostream->name  = name;
87
    return (TRUE);
114
    return(TRUE);
88
}
115
}
89
 
116
 
90
void
117
void
91
bostream_assign PROTO_N ((to, from))
118
bostream_assign(BOStreamP to,			 BOStreamP from)
92
		PROTO_T (BOStreamP to X
-
 
93
			 BOStreamP from)
-
 
94
{
119
{
95
    to->file  = from->file;
120
    to->file  = from->file;
96
    to->name  = from->name;
121
    to->name  = from->name;
97
}
122
}
98
 
123
 
99
BoolT
124
BoolT
100
bostream_is_open PROTO_N ((bostream))
125
bostream_is_open(BOStreamP bostream)
101
		 PROTO_T (BOStreamP bostream)
-
 
102
{
126
{
103
    return (bostream->name != NIL (CStringP));
127
    return(bostream->name != NIL(CStringP));
104
}
128
}
105
 
129
 
106
void
130
void
107
bostream_write_chars PROTO_N ((bostream, length, chars))
131
bostream_write_chars(BOStreamP bostream,			      unsigned  length ,
108
		     PROTO_T (BOStreamP bostream X
-
 
109
			      unsigned  length X
-
 
110
			      CStringP  chars)
132
			      CStringP  chars)
111
{
133
{
112
    unsigned bytes_read = (unsigned) fwrite ((GenericP) chars, sizeof (char),
134
    unsigned bytes_read = (unsigned)fwrite((GenericP)chars, sizeof(char),
113
					     (SizeT) length, bostream->file);
135
					    (SizeT)length, bostream->file);
114
 
136
 
115
    if ((bytes_read != length) && (ferror (bostream->file))) {
137
    if ((bytes_read != length) && (ferror(bostream->file))) {
116
	CStringP name = cstring_duplicate (bostream->name);
138
	CStringP name = cstring_duplicate(bostream->name);
117
 
139
 
118
	THROW_VALUE (XX_bostream_write_error, name);
140
	THROW_VALUE(XX_bostream_write_error, name);
119
	UNREACHED;
141
	UNREACHED;
120
    }
142
    }
121
}
143
}
122
 
144
 
123
void
145
void
124
bostream_write_bytes PROTO_N ((bostream, length, bytes))
146
bostream_write_bytes(BOStreamP bostream,			      unsigned  length ,
125
		     PROTO_T (BOStreamP bostream X
-
 
126
			      unsigned  length X
-
 
127
			      ByteP     bytes)
147
			      ByteP     bytes)
128
{
148
{
129
    unsigned bytes_read = (unsigned) fwrite ((GenericP) bytes, sizeof (ByteT),
149
    unsigned bytes_read = (unsigned)fwrite((GenericP)bytes, sizeof(ByteT),
130
					     (SizeT) length, bostream->file);
150
					    (SizeT)length, bostream->file);
131
 
151
 
132
    if ((bytes_read != length) && (ferror (bostream->file))) {
152
    if ((bytes_read != length) && (ferror(bostream->file))) {
133
	CStringP name = cstring_duplicate (bostream->name);
153
	CStringP name = cstring_duplicate(bostream->name);
134
 
154
 
135
	THROW_VALUE (XX_bostream_write_error, name);
155
	THROW_VALUE(XX_bostream_write_error, name);
136
	UNREACHED;
156
	UNREACHED;
137
    }
157
    }
138
}
158
}
139
 
159
 
140
void
160
void
141
bostream_write_byte PROTO_N ((bostream, byte))
161
bostream_write_byte(BOStreamP bostream,			     ByteT     byte)
142
		    PROTO_T (BOStreamP bostream X
-
 
143
			     ByteT     byte)
-
 
144
{
162
{
145
    if ((fputc ((int) byte, bostream->file) == EOF) &&
163
    if ((fputc((int)byte, bostream->file) == EOF) &&
146
	(ferror (bostream->file))) {
164
	(ferror(bostream->file))) {
147
	CStringP name = cstring_duplicate (bostream->name);
165
	CStringP name = cstring_duplicate(bostream->name);
148
 
166
 
149
	THROW_VALUE (XX_bostream_write_error, name);
167
	THROW_VALUE(XX_bostream_write_error, name);
150
	UNREACHED;
168
	UNREACHED;
151
    }
169
    }
152
}
170
}
153
 
171
 
154
CStringP
172
CStringP
155
bostream_name PROTO_N ((bostream))
173
bostream_name(BOStreamP bostream)
156
	      PROTO_T (BOStreamP bostream)
-
 
157
{
174
{
158
    return (bostream->name);
175
    return(bostream->name);
159
}
176
}
160
 
177
 
161
void
178
void
162
bostream_close PROTO_N ((bostream))
179
bostream_close(BOStreamP bostream)
163
	       PROTO_T (BOStreamP bostream)
-
 
164
{
180
{
165
    if (fclose (bostream->file)) {
181
    if (fclose(bostream->file)) {
166
	CStringP name = cstring_duplicate (bostream->name);
182
	CStringP name = cstring_duplicate(bostream->name);
167
 
183
 
168
	THROW_VALUE (XX_bostream_write_error, name);
184
	THROW_VALUE(XX_bostream_write_error, name);
169
	UNREACHED;
185
	UNREACHED;
170
    }
186
    }
171
    bostream_init (bostream);
187
    bostream_init(bostream);
172
}
188
}