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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const LLNSIZ: i32 = 2048;
struct SaveVars {
HKEY: Vec<u8>,
HLINE: Vec<u8>,
HLNGWD: Vec<u8>,
LNGWD: Vec<u8>,
ULINE: Vec<u8>,
BEGPOS: i32,
CLIDX: i32,
ENDPOS: i32,
PCLIDX: i32,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut HKEY = vec![b' '; LLNSIZ as usize];
let mut HLINE = vec![b' '; LLNSIZ as usize];
let mut HLNGWD = vec![b' '; LLNSIZ as usize];
let mut LNGWD = vec![b' '; LLNSIZ as usize];
let mut ULINE = vec![b' '; LLNSIZ as usize];
let mut BEGPOS: i32 = 0;
let mut CLIDX: i32 = 0;
let mut ENDPOS: i32 = 0;
let mut PCLIDX: i32 = 0;
Self {
HKEY,
HLINE,
HLNGWD,
LNGWD,
ULINE,
BEGPOS,
CLIDX,
ENDPOS,
PCLIDX,
}
}
}
//$Procedure PARCML ( Parse command line )
pub fn PARCML(
LINE: &mut [u8],
NKEYS: i32,
CLKEYS: CharArray,
CLFLAG: &mut [bool],
CLVALS: CharArrayMut,
FOUND: &mut bool,
UNPRSD: &mut [u8],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let CLKEYS = DummyCharArray::new(CLKEYS, None, 1..);
let mut CLFLAG = DummyArrayMut::new(CLFLAG, 1..);
let mut CLVALS = DummyCharArrayMut::new(CLVALS, None, 1..);
//
// Local variables.
//
//
// Save everything to prevent potential memory problems in f2c'ed
// version.
//
//
// SPICELIB functions.
//
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(b"PARCML", ctx)?;
}
//
// Set initial values of keys to blanks and flags to .FALSE.
//
for I in 1..=NKEYS {
CLFLAG[I] = false;
fstr::assign(CLVALS.get_mut(I), b" ");
}
*FOUND = false;
//
// Parsing loop. We will set the sub-string buffer HLINE to as many
// characters from the input line as it will fit, starting with the
// initial part of the line on the first iteration and resetting to
// sub-strings starting at the first character of each value after
// the previous key-value pair was processed, and will pick at HLINE
// word by word looking for recognized keys. The loop will
// continue until we reach the end of the string -- all key-value
// pairs were processed and the sub-string buffer HLINE was set to
// blank.
//
fstr::assign(&mut save.HLINE, LINE);
save.PCLIDX = 0;
save.CLIDX = 0;
fstr::assign(UNPRSD, LINE);
while fstr::ne(&save.HLINE, b" ") {
//
// Get next word; uppercase it; look for it in the input keys
// array.
//
spicelib::NEXTWD(&save.HLINE.to_vec(), &mut save.LNGWD, &mut save.HLINE);
spicelib::UCASE(&save.LNGWD, &mut save.HLNGWD, ctx);
save.CLIDX = spicelib::ISRCHC(&save.HLNGWD, NKEYS, CLKEYS.as_arg());
//
// Is the token that we found a recognized key?
//
if (save.CLIDX != 0) {
//
// Yes, it is. Is it the first key that we have found?
//
if (save.PCLIDX != 0) {
//
// No it is not. We need to save the value of the previous
// key.
//
// Compute the begin and end positions of the sub-string
// that contains the previous value by looking for the
// previous and current keys in the upper-cased remainder of
// the input line.
//
// The begin position is the position of the previous key
// plus its length. The end position is the position of the
// front-n-back blank-padded current key.
//
spicelib::UCASE(LINE, &mut save.ULINE, ctx);
save.BEGPOS = (spicelib::POS(
&save.ULINE,
fstr::substr(
&CLKEYS[save.PCLIDX],
1..=spicelib::RTRIM(&CLKEYS[save.PCLIDX]),
),
1,
) + spicelib::RTRIM(&CLKEYS[save.PCLIDX]));
fstr::assign(
&mut save.HKEY,
&fstr::concat(
b" ",
fstr::substr(
CLKEYS.get(save.CLIDX),
1..=spicelib::RTRIM(&CLKEYS[save.CLIDX]),
),
),
);
save.ENDPOS = spicelib::POS(
&fstr::concat(&save.ULINE, b" "),
fstr::substr(&save.HKEY, 1..=(spicelib::RTRIM(&save.HKEY) + 1)),
save.BEGPOS,
);
//
// Extract the value, left-justify it, and RTRIM it. Set
// "value found" flag to .TRUE.
//
fstr::assign(
CLVALS.get_mut(save.PCLIDX),
fstr::substr(LINE, save.BEGPOS..=save.ENDPOS),
);
spicelib::LJUST(&CLVALS[save.PCLIDX].to_vec(), &mut CLVALS[save.PCLIDX]);
let val = fstr::substr(
CLVALS.get(save.PCLIDX),
1..=spicelib::RTRIM(&CLVALS[save.PCLIDX]),
)
.to_vec();
fstr::assign(CLVALS.get_mut(save.PCLIDX), &val);
CLFLAG[save.PCLIDX] = true;
//
// Check whether we already parsed the whole line. It will
// be so if the remainder of the buffer holding the
// sub-string that we examine word-by-word is a blank
// string.
//
if fstr::ne(&save.HLINE, b" ") {
//
// No, we did not parse the whole line yet. There is
// more stuff to parse and we reset the temporary
// sub-string buffer to hold the part of the input string
// starting with the first character after the current
// key -- the end position plus the length of the
// current key.
//
//
fstr::assign(
&mut save.HLINE,
fstr::substr(
LINE,
((save.ENDPOS + 1) + spicelib::RTRIM(&CLKEYS[save.CLIDX]))..,
),
);
}
//
// Now reset the line to its portion starting with the
// first character of the current key.
//
let val = fstr::substr(LINE, (save.ENDPOS + 1)..).to_vec();
fstr::assign(LINE, &val);
} else {
//
// This is the first key that we have found. Set UNPRSD
// to the part of the line from the start to this key.
//
spicelib::UCASE(LINE, &mut save.ULINE, ctx);
fstr::assign(
&mut save.HKEY,
&fstr::concat(
b" ",
fstr::substr(
CLKEYS.get(save.CLIDX),
1..=spicelib::RTRIM(&CLKEYS[save.CLIDX]),
),
),
);
save.BEGPOS = spicelib::POS(
&fstr::concat(b" ", &save.ULINE),
fstr::substr(&save.HKEY, 1..=(spicelib::RTRIM(&save.HKEY) + 1)),
1,
);
if (save.BEGPOS <= 1) {
fstr::assign(UNPRSD, b" ");
} else {
fstr::assign(UNPRSD, fstr::substr(LINE, 1..=(save.BEGPOS - 1)));
}
}
//
// Save the current key index in as previous.
//
save.PCLIDX = save.CLIDX;
}
}
//
// If we found at least one recognized key, we need to save the last
// value.
//
if (save.PCLIDX != 0) {
//
// Set "found any" output flag and "found previous key" flags to
// .TRUE.
//
*FOUND = true;
CLFLAG[save.PCLIDX] = true;
//
// Check if there was any value following the last key (there was
// if the non-blank length of what's left in the line starting
// with the last key if greater than the non-blank length of the
// last key).
//
if (spicelib::RTRIM(LINE) > spicelib::RTRIM(&CLKEYS[save.PCLIDX])) {
//
// Compute begin position of, extract, left justify and
// RTRIM the last value.
//
spicelib::UCASE(LINE, &mut save.ULINE, ctx);
save.BEGPOS = (spicelib::POS(
&save.ULINE,
fstr::substr(
&CLKEYS[save.PCLIDX],
1..=spicelib::RTRIM(&CLKEYS[save.PCLIDX]),
),
1,
) + spicelib::RTRIM(&CLKEYS[save.PCLIDX]));
fstr::assign(
CLVALS.get_mut(save.PCLIDX),
fstr::substr(LINE, save.BEGPOS..),
);
spicelib::LJUST(&CLVALS[save.PCLIDX].to_vec(), &mut CLVALS[save.PCLIDX]);
let val = fstr::substr(
CLVALS.get(save.PCLIDX),
1..=spicelib::RTRIM(&CLVALS[save.PCLIDX]),
)
.to_vec();
fstr::assign(CLVALS.get_mut(save.PCLIDX), &val);
} else {
//
// The key was the last thing on the line. So, it's value is
// blank.
//
fstr::assign(CLVALS.get_mut(save.PCLIDX), b" ");
}
}
spicelib::CHKOUT(b"PARCML", ctx)?;
Ok(())
}