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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Count characters in a text file
///
/// Count the characters in a group of lines in a text file.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// UNIT I Logical unit connected to text file.
/// BLINE I Beginning line number.
/// ELINE I Ending line number.
/// LINE I-O Workspace.
///
/// The function returns the number of characters in a group of lines
/// in a text file.
/// ```
///
/// # Detailed Input
///
/// ```text
/// UNIT is a logical unit that has been connected to a
/// text file by the calling program. Use the routine
/// TXTOPR to open the file for read access and get its
/// logical unit. A text file is a formatted,
/// sequential file that contains only printable
/// characters: ASCII 32-126.
///
/// BLINE,
/// ELINE are line numbers in the text file. BLINE is
/// the line where the count will begin, and ELINE
/// is the line where the count will end. The
/// number of characters in the beginning and ending
/// lines are included in the total count.
///
/// By convention, line 1 is the first line of the file.
///
/// LINE on input, is an arbitrary character string whose
/// contents are ignored. LINE is used to read lines
/// from the file connected to UNIT; its function
/// is to determine the maximum length of the lines
/// that can be read from the file. Lines longer
/// than the declared length of LINE are truncated
/// as they are read.
/// ```
///
/// # Detailed Output
///
/// ```text
/// LINE on output, is undefined.
///
/// The function returns the number of characters in the group of
/// lines in the text file beginning with BLINE and ending with ELINE.
/// Trailing blanks on a line are not included in the count.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If an error occurs while reading from the input file,
/// the error SPICE(FILEREADFAILED) is signaled.
///
/// 2) If a non-printing ASCII character is encountered during
/// the count, the error SPICE(INVALIDTEXT) is signaled.
///
/// 3) If BLINE is greater than ELINE or if the file does not
/// contain both of this lines, the error SPICE(CANNOTFINDGRP)
/// is signaled.
/// ```
///
/// # Files
///
/// ```text
/// See argument UNIT. COUNTC rewinds the text file connected to
/// UNIT and then steps through the file. The next read statement
/// after calling COUNTC would return the line after ELINE.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine counts characters in a group of lines in a text
/// file. Using COUNTC, you can determine in advance how much space
/// is required to store those characters.
/// ```
///
/// # Examples
///
/// ```text
/// The following code fragment opens an existing text file for
/// read access and counts the characters that it contains in
/// the first five lines. We'll assume that the longest line
/// in the file is 80 characters.
///
/// INTEGER COUNTC
/// INTEGER UNIT
/// INTEGER N
/// CHARACTER*(80) LINE
///
/// CALL TXTOPR ( 'DATA.TXT', UNIT )
///
/// N = COUNTC ( UNIT, 1, 5, LINE )
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// J.E. McLean (JPL)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.2.0, 18-MAR-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.1.0, 17-MAY-1994 (HAN)
///
/// Set the default function value to either 0, 0.0D0, .FALSE.,
/// or blank depending on the type of the function.
///
/// - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
/// Comment section for permuted index source lines was added
/// following the header.
///
/// - SPICELIB Version 1.0.0, 05-APR-1991 (JEM)
/// ```
pub fn countc(
ctx: &mut SpiceContext,
unit: i32,
bline: i32,
eline: i32,
line: &mut str,
) -> crate::Result<i32> {
let ret = COUNTC(
unit,
bline,
eline,
fstr::StrBytes::new(line).as_mut(),
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(ret)
}
//$Procedure COUNTC ( Count characters in a text file )
pub fn COUNTC(
UNIT: i32,
BLINE: i32,
ELINE: i32,
LINE: &mut [u8],
ctx: &mut Context,
) -> f2rust_std::Result<i32> {
let mut COUNTC: i32 = 0;
let mut CHARS: i32 = 0;
let mut IOSTAT: i32 = 0;
let mut LINECT: i32 = 0;
let mut DONE: bool = false;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
COUNTC = 0;
return Ok(COUNTC);
} else {
CHKIN(b"COUNTC", ctx)?;
COUNTC = 0;
}
//
// First, see if the line numbers make sense.
//
if ((BLINE > ELINE) || (BLINE <= 0)) {
SETMSG(
b"The line numbers do not make sense: BLINE = # and ELINE = #.",
ctx,
);
ERRINT(b"#", BLINE, ctx);
ERRINT(b"#", ELINE, ctx);
SIGERR(b"SPICE(CANNOTFINDGRP)", ctx)?;
CHKOUT(b"COUNTC", ctx)?;
return Ok(COUNTC);
}
//
// Read through the file, line by line, beginning with the first
// line in the file, checking for I/O errors, and counting
// characters in the lines between and including BLINE and ELINE.
//
{
use f2rust_std::io;
let specs = io::PosSpecs {
unit: Some(UNIT),
..Default::default()
};
ctx.rewind(specs)?;
}
LINECT = 0;
CHARS = 0;
DONE = false;
while !DONE {
{
use f2rust_std::{
data::Val,
io::{self, Reader},
};
let mut reader = io::FormattedReader::new(ctx.io_unit(UNIT)?, None, b"(A)")?;
IOSTAT = io::capture_iostat(|| {
reader.start()?;
reader.read_str(LINE)?;
reader.finish()?;
Ok(())
})?;
}
//
// An end-of-file condition is indicated by a negative value
// for IOSTAT. Any other non-zero value indicates some other
// error. If IOSTAT is zero, the read was successful.
//
if (IOSTAT > 0) {
SETMSG(
b"Error reading text file named FILENAME.The value of IOSTAT is #.",
ctx,
);
ERRINT(b"#", IOSTAT, ctx);
ERRFNM(b"FILENAME", UNIT, ctx)?;
SIGERR(b"SPICE(FILEREADFAILED)", ctx)?;
CHKOUT(b"COUNTC", ctx)?;
return Ok(COUNTC);
} else if (IOSTAT < 0) {
SETMSG(b"Reached end of file unexpectedly at line # in file FILE. BLINE = # and ELINE = #.", ctx);
ERRINT(b"#", LINECT, ctx);
ERRINT(b"#", BLINE, ctx);
ERRINT(b"#", ELINE, ctx);
ERRFNM(b"FILE", UNIT, ctx)?;
SIGERR(b"SPICE(CANNOTFINDGRP)", ctx)?;
CHKOUT(b"COUNTC", ctx)?;
return Ok(COUNTC);
} else {
//
// We've read a line successfully, so add it to the line count.
// If this line is in the group delimited by BLINE and ELINE,
// count the characters in it, and if this line is ELINE, we're
// done.
//
LINECT = (LINECT + 1);
if ((LINECT >= BLINE) && (LINECT <= ELINE)) {
//
// Add the number of characters in this line to the count.
// If LINE is blank, LASTNB will return 0 which is just
// what we want.
//
CHARS = (CHARS + LASTNB(LINE));
//
// Remove the printable characters from the line. If
// any characters remain, signal an error.
//
ASTRIP(
&LINE.to_vec(),
&intrinsics::CHAR(32),
&intrinsics::CHAR(126),
LINE,
);
if fstr::ne(LINE, b" ") {
SETMSG(b"Non-printing ASCII characters were found when counting characters on line number # in file FILENAME.", ctx);
ERRINT(b"#", LINECT, ctx);
ERRFNM(b"FILENAME", UNIT, ctx)?;
SIGERR(b"SPICE(INVALIDTEXT)", ctx)?;
CHKOUT(b"COUNTC", ctx)?;
return Ok(COUNTC);
}
}
if (LINECT == ELINE) {
DONE = true;
}
}
}
//
// Assign the final character count.
//
COUNTC = CHARS;
CHKOUT(b"COUNTC", ctx)?;
Ok(COUNTC)
}