1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
const DSCLOC: i32 = 9;
const FWDLOC: i32 = 2;
const IDWLEN: i32 = 8;
const IFNLEN: i32 = 60;
const NWC: i32 = 1024;
const NWD: i32 = 128;
const NWI: i32 = 256;
const RNGLOC: i32 = 3;
const TYPLEN: i32 = 4;
const CHR: i32 = 1;
const DP: i32 = 2;
const INT: i32 = 3;
struct SaveVars {
NEXT: StackArray<i32, 3>,
NATBFF: i32,
PREV: StackArray<i32, 3>,
RECSIZ: StackArray<i32, 3>,
PASS1: bool,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut NEXT = StackArray::<i32, 3>::new(1..=3);
let mut NATBFF: i32 = 0;
let mut PREV = StackArray::<i32, 3>::new(1..=3);
let mut RECSIZ = StackArray::<i32, 3>::new(1..=3);
let mut PASS1: bool = false;
{
use f2rust_std::data::Val;
let mut clist = [Val::I(2), Val::I(3), Val::I(1)].into_iter();
NEXT.iter_mut()
.for_each(|n| *n = clist.next().unwrap().into_i32());
debug_assert!(clist.next().is_none(), "DATA not fully initialised");
}
PASS1 = true;
{
use f2rust_std::data::Val;
let mut clist = [Val::I(3), Val::I(1), Val::I(2)].into_iter();
PREV.iter_mut()
.for_each(|n| *n = clist.next().unwrap().into_i32());
debug_assert!(clist.next().is_none(), "DATA not fully initialised");
}
{
use f2rust_std::data::Val;
let mut clist = [Val::I(NWC), Val::I(NWD), Val::I(NWI)].into_iter();
RECSIZ
.iter_mut()
.for_each(|n| *n = clist.next().unwrap().into_i32());
debug_assert!(clist.next().is_none(), "DATA not fully initialised");
}
Self {
NEXT,
NATBFF,
PREV,
RECSIZ,
PASS1,
}
}
}
//$Procedure T_BGODAS ( BINGO: Process DAS files to alternate BFFs )
pub fn T_BGODAS(
INAME: &[u8],
ONAME: &[u8],
OBFF: i32,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let mut DATAC = [b' '; NWC as usize];
let mut FTYPE = [b' '; TYPLEN as usize];
let mut IDWORD = [b' '; IDWLEN as usize];
let mut IFNAME = [b' '; IFNLEN as usize];
let mut DATAD = StackArray::<f64, 128>::new(1..=NWD);
let mut CLTYPE: i32 = 0;
let mut CLSIZE: i32 = 0;
let mut DATAI = StackArray::<i32, 256>::new(1..=NWI);
let mut DIRLOC: i32 = 0;
let mut DIRREC = StackArray::<i32, 256>::new(1..=NWI);
let mut FREE: i32 = 0;
let mut HIADDR = StackArray::<i32, 3>::new(1..=3);
let mut IHAN: i32 = 0;
let mut J: i32 = 0;
let mut LASTLA = StackArray::<i32, 3>::new(1..=3);
let mut LASTRC = StackArray::<i32, 3>::new(1..=3);
let mut LASTWD = StackArray::<i32, 3>::new(1..=3);
let mut LOC: i32 = 0;
let mut FWDPTR: i32 = 0;
let mut OHAN: i32 = 0;
let mut MXADDR = StackArray::<i32, 3>::new(1..=3);
let mut NCC: i32 = 0;
let mut NCR: i32 = 0;
let mut NRC: i32 = 0;
let mut NRR: i32 = 0;
let mut RECNO: i32 = 0;
let mut UNIT: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// DAS data type codes
//
//
// Local variables
//
//
// Saved variables
//
//
// Initial values
//
if spicelib::RETURN(ctx) {
return Ok(());
}
spicelib::CHKIN(b"T_BGODAS", ctx)?;
if save.PASS1 {
//
// Obtain the code for the native binary file format.
//
spicelib::ZZDDHNFC(&mut save.NATBFF, ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
save.PASS1 = false;
}
//
// Open the existing input DAS file. Read the file's file record.
//
spicelib::DASOPR(INAME, &mut IHAN, ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
//
// Read the file record of the input DAS.
//
spicelib::DASRFR(
IHAN,
&mut IDWORD,
&mut IFNAME,
&mut NRR,
&mut NRC,
&mut NCR,
&mut NCC,
ctx,
)?;
fstr::assign(&mut FTYPE, fstr::substr(&IDWORD, 5..));
//
// Get the file summary of the input DAS.
//
spicelib::DASHFS(
IHAN,
&mut NRR,
&mut NRC,
&mut NCR,
&mut NCC,
&mut FREE,
LASTLA.as_slice_mut(),
LASTRC.as_slice_mut(),
LASTWD.as_slice_mut(),
ctx,
)?;
if spicelib::EXISTS(ONAME, ctx)? {
spicelib::DELFIL(ONAME, ctx)?;
}
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
spicelib::DASONW(ONAME, &FTYPE, &IFNAME, NCR, &mut OHAN, ctx)?;
spicelib::ZZDDHHLU(OHAN, b"DAS", true, &mut UNIT, ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
//
// Transfer the file record to the output file.
//
if (OBFF == save.NATBFF) {
//
// The output format is native.
//
spicelib::DASWFR(OHAN, &IDWORD, &IFNAME, NRR, NRC, NCR, NCC, ctx)?;
} else {
//
// This is a non-native write. We always add the FTP
// string to the output file record.
//
T_DASWFR(UNIT, OBFF, &IDWORD, &IFNAME, NRR, NRC, NCR, NCC, true, ctx)?;
}
//
// Transfer any reserved records and the comment area.
//
for I in 1..=NRR {
RECNO = (I + 1);
spicelib::DASRRC(IHAN, RECNO, 1, NWC, &mut DATAC, ctx)?;
spicelib::DASWRC(OHAN, RECNO, &DATAC, ctx)?;
}
for I in 1..=NCR {
RECNO = ((I + NRR) + 1);
spicelib::DASRRC(IHAN, RECNO, 1, NWC, &mut DATAC, ctx)?;
spicelib::DASWRC(OHAN, RECNO, &DATAC, ctx)?;
}
//
// Transfer directories and the data records that follow them.
//
DIRLOC = ((2 + NRR) + NCR);
while (DIRLOC != 0) {
//
// Read the current directory record; extract the forward pointer,
// data address ranges, and first cluster descriptor.
//
spicelib::DASRRI(IHAN, DIRLOC, 1, NWI, DIRREC.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
FWDPTR = DIRREC[FWDLOC];
for I in 1..=3 {
//
// HIADDR contains the highest address of each cluster that
// has been processed, for each data type. MXADDR is the
// maximum address of each data type for the current
// directory.
//
// Directories containing no clusters of a given data type
// have address ranges of 0:0 for that type. Set HIADDR
// accordingly.
//
J = ((2 * I) - 1);
MXADDR[I] = DIRREC[(RNGLOC + J)];
if (MXADDR[I] > 0) {
HIADDR[I] = (DIRREC[((RNGLOC + J) - 1)] - 1);
} else {
HIADDR[I] = 0;
}
}
CLTYPE = DIRREC[DSCLOC];
CLSIZE = DIRREC[(DSCLOC + 1)];
LOC = (DSCLOC + 1);
//
// Copy the directory record to the output file.
//
if (OBFF == save.NATBFF) {
spicelib::DASWRI(OHAN, DIRLOC, DIRREC.as_slice(), ctx)?;
} else {
//
// This is the non-native output case.
//
T_XLTFWI(DIRREC.as_slice(), NWI, OBFF, &mut DATAC, ctx)?;
spicelib::DASIOC(b"WRITE", UNIT, DIRLOC, &mut DATAC, ctx)?;
}
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
//
// Traverse the directory record and copy the data records from
// its associated clusters. The code below does not assume that
// directory records are zero-padded.
//
RECNO = (DIRLOC + 1);
while (((HIADDR[1] < MXADDR[1]) || (HIADDR[2] < MXADDR[2])) || (HIADDR[3] < MXADDR[3])) {
for I in 1..=CLSIZE {
if (CLTYPE == CHR) {
//
// Character records are written as is.
//
spicelib::DASRRC(IHAN, RECNO, 1, NWC, &mut DATAC, ctx)?;
spicelib::DASWRC(OHAN, RECNO, &DATAC, ctx)?;
} else if (CLTYPE == DP) {
spicelib::DASRRD(IHAN, RECNO, 1, NWD, DATAD.as_slice_mut(), ctx)?;
if (OBFF == save.NATBFF) {
//
// The output binary file format is native.
// Write the record as is.
//
spicelib::DASWRD(OHAN, RECNO, DATAD.as_slice(), ctx)?;
} else {
//
// Translate the d.p. record to the output
// format. The result is stored in a character
// string.
//
T_XLTFWD(DATAD.as_slice(), NWD, OBFF, &mut DATAC, ctx)?;
spicelib::DASIOC(b"WRITE", UNIT, RECNO, &mut DATAC, ctx)?;
}
} else if (CLTYPE == INT) {
spicelib::DASRRI(IHAN, RECNO, 1, NWI, DATAI.as_slice_mut(), ctx)?;
if (OBFF == save.NATBFF) {
spicelib::DASWRI(OHAN, RECNO, DATAI.as_slice(), ctx)?;
} else {
T_XLTFWI(DATAI.as_slice(), NWI, OBFF, &mut DATAC, ctx)?;
spicelib::DASIOC(b"WRITE", UNIT, RECNO, &mut DATAC, ctx)?;
}
} else {
//
// We found a "new" data type code. The input file is
// probably corrupted.
//
spicelib::SETMSG(b"In DAS file #; directory record having record number # contains cluster type of #. The valid range is 1-3.", ctx);
spicelib::ERRHAN(b"#", IHAN, ctx)?;
spicelib::ERRINT(b"#", DIRLOC, ctx);
spicelib::ERRINT(b"#", CLTYPE, ctx);
spicelib::SIGERR(b"SPICE(DASBADDIRECTORY)", ctx)?;
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
return Ok(());
}
//
// Process the next record of the cluster.
//
RECNO = (RECNO + 1);
}
//
// Update the highest data address read for the data
// type of the cluster that was just processed.
//
HIADDR[CLTYPE] = (HIADDR[CLTYPE] + (CLSIZE * save.RECSIZ[CLTYPE]));
//
// Get the type and size of the next cluster.
//
LOC = (LOC + 1);
if (DIRREC[LOC] < 0) {
CLTYPE = save.PREV[CLTYPE];
} else {
CLTYPE = save.NEXT[CLTYPE];
}
CLSIZE = i32::abs(DIRREC[LOC]);
}
//
// Set the directory record to the forward pointer.
// Note that the indicated directory may be empty;
// we still want to copy it to the output file.
//
DIRLOC = FWDPTR;
}
//
// Make sure all buffered data are written to the output file.
//
// Note that for non-native output files, the only buffered
// records will be of character type. These records don't
// require translation, so DASWBR may be used to write them.
//
// An important side effect of this call is to deallocate
// buffer entries for this file in DASRWR. If these entries
// remain allocated, later calls to DASRRC will probably fail,
// as the buffered entries can't be written to make room for
// new data.
//
spicelib::DASWBR(OHAN, ctx)?;
//
// Close the output file without segregating it, since we're
// duplicating the structure of the input file. If the input file is
// segregated, the output file will be segregated as well,
// automatically.
//
spicelib::DASLLC(OHAN, ctx)?;
//
// We're done with the output DAS logical unit. Unlock it.
//
spicelib::ZZDDHUNL(OHAN, b"DAS", ctx)?;
//
// We're done with the input file.
//
spicelib::DASCLS(IHAN, ctx)?;
spicelib::CHKOUT(b"T_BGODAS", ctx)?;
Ok(())
}