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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
pub const MXCREC: i32 = 1000;
pub const LINLEN: i32 = 255;
const INTEOC: i32 = 4;
const INTEOL: i32 = 0;
const CASTRT: i32 = 2;
//$ Procedure SPCACB ( SPK and CK add comments from a buffer )
pub fn SPCACB(DAFHDL: i32, BUFFER: CharArray, ctx: &mut Context) -> f2rust_std::Result<()> {
let BUFFER = DummyCharArray::new(BUFFER, None, LBCELL..);
let mut LINE = [b' '; LINLEN as usize];
let mut CRECRD = [b' '; MXCREC as usize];
let mut EOCMRK = [b' '; 1 as usize];
let mut EOLMRK = [b' '; 1 as usize];
let mut IFNAME = [b' '; LINLEN as usize];
let mut I: i32 = 0;
let mut J: i32 = 0;
let mut LENGTH: i32 = 0;
let mut CURPOS: i32 = 0;
let mut EOCPOS: i32 = 0;
let mut NCRECS: i32 = 0;
let mut NNRECS: i32 = 0;
let mut NRRECS: i32 = 0;
let mut RECNO: i32 = 0;
let mut NCHARS: i32 = 0;
let mut NLINES: i32 = 0;
let mut SPACE: i32 = 0;
let mut ND: i32 = 0;
let mut NI: i32 = 0;
let mut FIRST: i32 = 0;
let mut LAST: i32 = 0;
let mut FREE: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
//
// This is needed for the call to DAFRFR to get some of the
// information needed. It is not used anywhere else.
//
//
// These are needed to call DAFRFR to get some of the information
// needed. Only FIRST will be used, and this is to determine the
// number of reserved records which exist.
//
//
// Initial values
//
fstr::assign(&mut EOCMRK, &intrinsics::CHAR(INTEOC));
fstr::assign(&mut EOLMRK, &intrinsics::CHAR(INTEOL));
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(b"SPCACB", ctx)?;
}
//
// Give some of the variables a value so that they have one.
//
NCRECS = 0;
NNRECS = 0;
NRRECS = 0;
//
// First, extract the number of lines in the buffer
//
NLINES = spicelib::CARDC(BUFFER.as_arg(), ctx)?;
//
// Check for a nonpositive number of lines.
//
if (NLINES <= 0) {
spicelib::SETMSG(b"An invalid buffer length was found: #", ctx);
spicelib::ERRINT(b"#", NLINES, ctx);
spicelib::SIGERR(b"SPICE(NONPOSBUFLENGTH)", ctx)?;
spicelib::CHKOUT(b"SPCACB", ctx)?;
return Ok(());
}
//
// Count the number of characters in the buffer, ignoring leading
// and trailing blanks on nonblank lines. Blank lines will not count
// here, their contribution to the size of the comment area will be
// incorporated later. This is for determining the number of
// character records to add to the file attached to handle DAFHDL.
//
NCHARS = 0;
I = 0;
while (I < NLINES) {
I = (I + 1);
fstr::assign(&mut LINE, BUFFER.get(I));
LENGTH = spicelib::LASTNB(&LINE);
NCHARS = (NCHARS + LENGTH);
}
//
// Add NLINES + 1 to NCHARS to allow for the end of line markers
// ( EOLMRK ) and the end of comments marker ( EOCMRK ).
//
NCHARS = ((NCHARS + NLINES) + 1);
//
// Get the number of reserved records from the file.
//
spicelib::DAFRFR(
DAFHDL,
&mut ND,
&mut NI,
&mut IFNAME,
&mut FIRST,
&mut LAST,
&mut FREE,
ctx,
)?;
//
// Subtract 1 from FIRST to obtain the number of reserved records.
//
// Note that this should be one more than the number of comment
// records in the comment area for the SPK or CK file comment area
// to conform to the SPC comment area conventions. That is, the
// number of reserved records = the number of comment records + 1.
//
NRRECS = (FIRST - 1);
//
// If the number of reserved records, NRRECS, is greater then 1,
// determine the number of comment records in the comment area.
// The comments begin on record CASTRT and should continue to record
// NRRECS - 1. The comments are terminated by and end of comment
// marker EOCMRK = CHAR(4).
//
EOCPOS = 0;
I = 0;
while ((I < (NRRECS - 1)) && (EOCPOS == 0)) {
RECNO = (CASTRT + I);
spicelib::DAFRCR(DAFHDL, RECNO, &mut CRECRD, ctx)?;
EOCPOS = spicelib::CPOS(&CRECRD, &EOCMRK, 1);
I = (I + 1);
}
if ((EOCPOS == 0) && (NRRECS > 1)) {
spicelib::SETMSG(
b"End-of-transmission character missing in comment area of binary file.",
ctx,
);
spicelib::SIGERR(b"SPICE(MISSINGEOT)", ctx)?;
spicelib::CHKOUT(b"SPCACB", ctx)?;
return Ok(());
}
NCRECS = I;
//
// Check to see if the number of comment records is one less than
// the number of reserved records. If not, signal an error.
//
// IF ( NCRECS .NE. NRRECS - 1 ) THEN
// CALL SETMSG ( 'The number of comment records and the'//
// . ' number of reserved records do not agree.'//
// . ' The comment area could be bad.' )
// CALL SIGERR ( 'SPICE(BADCOMMENTAREA)' )
// CALL CHKOUT ( 'SPCACB' )
// RETURN
// END IF
//
// Determine the amount of free space in the comment area. This
// will be the space remaining on the last comment record, i.e.,
// the maximum length of a DAF character record - the position
// of the end of comments marker - 1.
//
if (NCRECS > 0) {
SPACE = (MXCREC - EOCPOS);
} else {
SPACE = 0;
}
//
// Determine the number of extra reserved records which are
// necessary to store the comments in the buffer.
//
if (NCHARS > SPACE) {
NNRECS = (1 + ((NCHARS - SPACE) / MXCREC));
} else {
NNRECS = 0;
}
//
// Now call the DAF routine to add reserved records to the file,
// if we need to.
//
if (NNRECS > 0) {
spicelib::DAFARR(DAFHDL, NNRECS, ctx)?;
}
//
// At this point, we know that we have enough space to write the
// comments in the buffer to the comment area. Either there was
// enough space already there, or we figured out how many new
// character records were needed, and we added them to the file.
// So, now we begin 'packing' the comments into the character record.
//
// We begin by reading the last comment record if there is one,
// otherwise we just initialize the appropriate variables.
//
if (NCRECS == 0) {
RECNO = CASTRT;
CURPOS = 0;
fstr::assign(&mut CRECRD, b" ");
} else {
RECNO = ((CASTRT + NCRECS) - 1);
spicelib::DAFRCR(DAFHDL, RECNO, &mut CRECRD, ctx)?;
//
// Find the end of comment marker again. This is really not
// necessary, but it is here to localize all the info needed.
//
EOCPOS = spicelib::CPOS(&CRECRD, &EOCMRK, 1);
//
// Set the current record position
//
CURPOS = EOCPOS;
//
// Put an end of line marker here to separate the new comments
// from the old ones, and increment the current record position.
//
fstr::assign(fstr::substr_mut(&mut CRECRD, CURPOS..=CURPOS), &EOLMRK);
}
I = 0;
while (I < NLINES) {
I = (I + 1);
fstr::assign(&mut LINE, BUFFER.get(I));
LENGTH = spicelib::LASTNB(&LINE);
J = 0;
while (J < LENGTH) {
if (CURPOS < MXCREC) {
J = (J + 1);
CURPOS = (CURPOS + 1);
fstr::assign(
fstr::substr_mut(&mut CRECRD, CURPOS..=CURPOS),
fstr::substr(&LINE, J..=J),
);
} else {
spicelib::DAFWCR(DAFHDL, RECNO, &CRECRD, ctx)?;
RECNO = (RECNO + 1);
CURPOS = 0;
fstr::assign(&mut CRECRD, b" ");
}
}
//
// Check to see if we happened to get exactly MXCREC characters
// when we stopped moving characters from LINE. If we did, then
// we need to write out the current record and appropriately
// adjust the necessary variables.
//
if (CURPOS == MXCREC) {
spicelib::DAFWCR(DAFHDL, RECNO, &CRECRD, ctx)?;
RECNO = (RECNO + 1);
CURPOS = 0;
fstr::assign(&mut CRECRD, b" ");
}
CURPOS = (CURPOS + 1);
fstr::assign(fstr::substr_mut(&mut CRECRD, CURPOS..=CURPOS), &EOLMRK);
}
//
// We have now finished processing all of the lines, so we
// need to append the end of comment marker to the current
// record and write it to the file.
//
if (CURPOS == MXCREC) {
spicelib::DAFWCR(DAFHDL, RECNO, &CRECRD, ctx)?;
RECNO = (RECNO + 1);
CURPOS = 0;
fstr::assign(&mut CRECRD, b" ");
}
CURPOS = (CURPOS + 1);
fstr::assign(fstr::substr_mut(&mut CRECRD, CURPOS..=CURPOS), &EOCMRK);
spicelib::DAFWCR(DAFHDL, RECNO, &CRECRD, ctx)?;
spicelib::CHKOUT(b"SPCACB", ctx)?;
Ok(())
}