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-2005 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 34... Line 64...
34
#include "calculus.h"
64
#include "calculus.h"
35
#include "common.h"
65
#include "common.h"
36
#include "error.h"
66
#include "error.h"
37
#include "write.h"
67
#include "write.h"
38
 
68
 
39
 
69
 
40
/*
70
/*
41
    OUTPUT FILE
71
 * OUTPUT FILE
42
 
72
 *
43
    These variables describe the output file.  There is a one byte output
73
 * These variables describe the output file.  There is a one byte output
44
    buffer.
74
 * buffer.
45
*/
75
 */
46
 
76
 
47
static FILE *output_file ;
77
static FILE *output_file;
48
static unsigned long output_buff = 0 ;
78
static unsigned long output_buff = 0;
49
static int output_bits = 0 ;
79
static int output_bits = 0;
50
 
80
 
51
 
81
 
52
/*
82
/*
53
    WRITE A NUMBER OF BITS
83
 * WRITE A NUMBER OF BITS
54
 
84
 *
55
    This routine writes the value v into n bits.
85
 * This routine writes the value v into n bits.
56
*/
86
 */
57
 
87
 
58
static void write_bits
88
static void
59
    PROTO_N ( ( n, v ) )
-
 
60
    PROTO_T ( int n X unsigned long v )
89
write_bits(int n, unsigned long v)
61
{
90
{
62
    if ( n > CHAR_BIT ) {
91
    if (n > CHAR_BIT) {
63
	write_bits ( n - CHAR_BIT, ( v >> CHAR_BIT ) ) ;
92
	write_bits(n - CHAR_BIT,(v >> CHAR_BIT));
64
	write_bits ( CHAR_BIT, ( v & ( unsigned ) MASK ( CHAR_BIT ) ) ) ;
93
	write_bits(CHAR_BIT,(v & (unsigned)MASK(CHAR_BIT)));
65
    } else {
94
    } else {
66
	int b = output_bits + n ;
95
	int b = output_bits + n;
67
	unsigned long m = ( output_buff << n ) | v ;
96
	unsigned long m = (output_buff << n) | v;
68
	int c = b - CHAR_BIT ;
97
	int c = b - CHAR_BIT;
69
	if ( c >= 0 ) {
98
	if (c >= 0) {
70
	    /* Write next byte */
99
	    /* Write next byte */
71
	    int p = ( int ) ( m >> c ) ;
100
	    int p = (int)(m >> c);
72
	    fputc_v ( p, output_file ) ;
101
	    fputc_v(p, output_file);
73
	    m &= bitmask [c] ;
102
	    m &= bitmask[c];
74
	    b = c ;
103
	    b = c;
75
	}
104
	}
76
	output_bits = b ;
105
	output_bits = b;
77
	output_buff = m ;
106
	output_buff = m;
78
    }
107
    }
79
    return ;
108
    return;
80
}
109
}
81
 
110
 
82
 
111
 
83
/*
112
/*
84
    WRITE AN INTEGER
113
 * WRITE AN INTEGER
85
 
114
 *
86
    This routine writes the integer n to the output file.  This is
115
 * This routine writes the integer n to the output file.  This is
87
    encoded as a sequence of octal digits, plus a flag to indicate the
116
 * encoded as a sequence of octal digits, plus a flag to indicate the
88
    last digit.  The variable d is used to indicate whether this last
117
 * last digit.  The variable d is used to indicate whether this last
89
    digit marker should be output.  The normal method of accessing the
118
 * digit marker should be output.  The normal method of accessing the
90
    routine is via the macro write_int.
119
 * routine is via the macro write_int.
91
*/
120
 */
92
 
121
 
93
static void write_int_aux
122
static void
94
    PROTO_N ( ( n, d ) )
-
 
95
    PROTO_T ( unsigned long n X unsigned long d )
123
write_int_aux(unsigned long n, unsigned long d)
96
{
124
{
97
    unsigned long m = ( n >> 3 ) ;
125
    unsigned long m = (n >> 3);
-
 
126
    if (m) {
98
    if ( m ) write_int_aux ( m, ( unsigned long ) 0x00 ) ;
127
	    write_int_aux(m,(unsigned long)0x00);
-
 
128
    }
99
    write_bits ( 4, ( ( n & 0x07 ) | d ) ) ;
129
    write_bits(4,((n & 0x07) | d));
100
    return ;
130
    return;
101
}
131
}
102
 
132
 
103
#define write_int( N )	write_int_aux ( ( N ), ( unsigned long ) 0x08 )
133
#define write_int(N)	write_int_aux((N), (unsigned long)0x08)
104
 
134
 
105
 
135
 
106
/*
136
/*
107
    WRITE A STRING
137
 * WRITE A STRING
108
 
138
 *
109
    This routine writes the string s to the output file.  This is
139
 * This routine writes the string s to the output file.  This is
110
    encoded as the string length followed by the component characters
140
 * encoded as the string length followed by the component characters
111
    (8 bits each).
141
 * (8 bits each).
112
*/
142
 */
113
 
143
 
114
static void write_string
144
static void
115
    PROTO_N ( ( s ) )
-
 
116
    PROTO_T ( char *s )
145
write_string(char *s)
117
{
146
{
118
    unsigned long i, n = ( unsigned long ) strlen ( s ) ;
147
    unsigned long i, n = (unsigned long)strlen(s);
119
    write_int ( n ) ;
148
    write_int(n);
120
    for ( i = 0 ; i < n ; i++ ) {
149
    for (i = 0; i < n; i++) {
121
	write_bits ( 8, ( unsigned long ) s [i] ) ;
150
	write_bits(8,(unsigned long)s[i]);
122
    }
151
    }
123
    return ;
152
    return;
124
}
153
}
125
 
-
 
126
 
-
 
127
/*
-
 
128
    LAST FILE NAME WRITTEN
-
 
129
 
-
 
130
    This variable is used to store the last file name written.
-
 
131
*/
-
 
132
 
-
 
133
static char *last_filename = NULL ;
-
 
134
 
154
 
135
 
155
 
136
/*
156
/*
137
    WRITE A FILE NAME
157
 * LAST FILE NAME WRITTEN
-
 
158
 *
-
 
159
 * This variable is used to store the last file name written.
-
 
160
 */
138
 
161
 
-
 
162
static char *last_filename = NULL;
-
 
163
 
-
 
164
 
-
 
165
/*
-
 
166
 * WRITE A FILE NAME
-
 
167
 *
139
    This routine writes the file name s.  This is just a simple string,
168
 * This routine writes the file name s.  This is just a simple string,
140
    but file names are buffered using last_filename.
169
 * but file names are buffered using last_filename.
141
*/
170
 */
142
 
171
 
143
static void write_filename
172
static void
144
    PROTO_N ( ( s ) )
-
 
145
    PROTO_T ( char *s )
173
write_filename(char *s)
146
{
174
{
147
    char *t = last_filename ;
175
    char *t = last_filename;
148
    if ( t && streq ( t, s ) ) {
176
    if (t && streq(t, s)) {
149
	write_bits ( 1, ( unsigned long ) 1 ) ;
177
	write_bits(1,(unsigned long)1);
150
    } else {
178
    } else {
151
	write_bits ( 1, ( unsigned long ) 0 ) ;
179
	write_bits(1,(unsigned long)0);
152
	write_string ( s ) ;
180
	write_string(s);
153
	last_filename = s ;
181
	last_filename = s;
154
    }
182
    }
155
    return ;
183
    return;
156
}
184
}
157
 
185
 
158
 
186
 
159
/*
187
/*
160
    AUTOMATICALLY GENERATED DISK WRITING ROUTINES
188
 * AUTOMATICALLY GENERATED DISK WRITING ROUTINES
-
 
189
 *
-
 
190
 * The main disk writing routines are automatically generated.  The
-
 
191
 * various macros are used to customise these routines.
-
 
192
 */
161
 
193
 
162
    The main disk writing routines are automatically generated.  The
-
 
163
    various macros are used to customise these routines.
-
 
164
*/
-
 
165
 
-
 
166
#define WRITE_BITS( A, B )	write_bits ( ( A ), ( unsigned long ) ( B ) )
194
#define WRITE_BITS(A, B)	write_bits((A), (unsigned long)(B))
167
#define WRITE_ALIAS( A )	write_int ( ( unsigned long ) ( A ) )
195
#define WRITE_ALIAS(A)		write_int((unsigned long)(A))
168
#define WRITE_DIM( A )		write_int ( ( unsigned long ) ( A ) )
196
#define WRITE_DIM(A)		write_int((unsigned long)(A))
169
#define WRITE_int( A )		write_int ( ( unsigned long ) ( A ) )
197
#define WRITE_int(A)		write_int((unsigned long)(A))
170
#define WRITE_number( A )	write_int ( ( unsigned long ) ( A ) )
198
#define WRITE_number(A)		write_int((unsigned long)(A))
171
#define WRITE_string( A )	write_string ( A )
199
#define WRITE_string(A)		write_string(A)
172
#define WRITE_name_string( A )	write_filename ( A )
200
#define WRITE_name_string(A)	write_filename(A)
173
#define WRITE_zero_int( A )	UNUSED ( A )
201
#define WRITE_zero_int(A)	UNUSED(A)
174
#define crt_disk_alias		crt_calculus_alias
202
#define crt_disk_alias		crt_calculus_alias
175
 
203
 
176
#include "write_def.h"
204
#include "write_def.h"
177
 
205
 
178
 
206
 
179
/*
207
/*
180
    WRITE A FILE
208
 * WRITE A FILE
181
 
209
 *
182
    This routine writes the current algebra to disk into the file nm.
210
 * This routine writes the current algebra to disk into the file nm.
183
*/
211
 */
184
 
212
 
185
void write_file
213
void
186
    PROTO_N ( ( nm ) )
-
 
187
    PROTO_T ( char *nm )
214
write_file(char *nm)
188
{
215
{
189
    /* Open file */
216
    /* Open file */
190
    if ( streq ( nm, "." ) ) {
217
    if (streq(nm, ".")) {
191
	error ( ERROR_SERIOUS, "Output file not specified" ) ;
218
	error(ERROR_SERIOUS, "Output file not specified");
192
	return ;
219
	return;
193
    }
220
    }
194
    output_file = fopen ( nm, "wb" ) ;
221
    output_file = fopen(nm, "wb");
195
    if ( output_file == NULL ) {
222
    if (output_file == NULL) {
196
	error ( ERROR_SERIOUS, "Can't open output file, '%s'", nm ) ;
223
	error(ERROR_SERIOUS, "Can't open output file, '%s'", nm);
197
	return ;
224
	return;
198
    }
225
    }
199
    init_bitmask () ;
226
    init_bitmask();
200
    output_buff = 0 ;
227
    output_buff = 0;
201
    output_bits = 0 ;
228
    output_bits = 0;
202
    last_filename = NULL ;
229
    last_filename = NULL;
203
 
230
 
204
    /* Write the file header */
231
    /* Write the file header */
205
    WRITE_string ( calculus_NAME ) ;
232
    WRITE_string(calculus_NAME);
206
    WRITE_string ( calculus_VERSION ) ;
233
    WRITE_string(calculus_VERSION);
207
 
234
 
208
    /* Write the algebra */
235
    /* Write the algebra */
209
    WRITE_string ( algebra->name ) ;
236
    WRITE_string(algebra->name);
210
    WRITE_int ( algebra->major_no ) ;
237
    WRITE_int(algebra->major_no);
211
    WRITE_int ( algebra->minor_no ) ;
238
    WRITE_int(algebra->minor_no);
212
    WRITE_list_ptr_type ( algebra->types ) ;
239
    WRITE_list_ptr_type(algebra->types);
213
 
240
 
214
    /* Close file */
241
    /* Close file */
215
    if ( output_bits ) {
242
    if (output_bits) {
216
	/* Tidy up any odd bits */
243
	/* Tidy up any odd bits */
217
	write_bits ( CHAR_BIT - output_bits, ( unsigned long ) 0 ) ;
244
	write_bits(CHAR_BIT - output_bits,(unsigned long)0);
218
    }
245
    }
219
    clear_calculus_alias () ;
246
    clear_calculus_alias();
220
    fclose_v ( output_file ) ;
247
    fclose_v(output_file);
221
    return ;
248
    return;
222
}
249
}