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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Does the file exist?
///
/// Determine whether a file exists.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// FNAME I Name of the file in question.
///
/// The function returns the value .TRUE. if the file exists, .FALSE.
/// otherwise.
/// ```
///
/// # Detailed Input
///
/// ```text
/// FNAME is the name of the file in question. This may be any
/// unambiguous file name valid on the user's computer, for
/// example
///
/// '/usr/dir1/dir2/DATA.DAT'
/// './DATA.DAT'
/// 'c:\usr\dir1\dir2\data.dat'
///
/// Environment or shell variables may not be used.
/// ```
///
/// # Detailed Output
///
/// ```text
/// The function returns the value .TRUE. if the file exists, .FALSE.
/// otherwise.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the filename is blank, the error SPICE(BLANKFILENAME) is
/// signaled.
///
/// 2) If an I/O error occurs while checking the existence of the
/// indicated file, the error SPICE(INQUIREFAILED) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
/// Use the Fortran INQUIRE statement to determine the existence
/// of FNAME.
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as
/// input, the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
/// 1) Given two arbitrary files (one of them the actual code example
/// source file), determine if they exists.
///
/// Example code begins here.
///
///
/// PROGRAM EXISTS_EX1
/// IMPLICIT NONE
///
/// C
/// C SPICELIB functions.
/// C
/// LOGICAL EXISTS
/// INTEGER RTRIM
///
/// C
/// C Local constants.
/// C
/// INTEGER FILEN
/// PARAMETER ( FILEN = 14 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(FILEN) FNAME ( 2 )
///
/// INTEGER I
///
/// C
/// C Define an array of file names.
/// C
/// DATA FNAME / 'exists_ex1.txt',
/// . 'exists_ex1.pgm' /
///
/// DO I = 1, 2
///
/// IF ( EXISTS ( FNAME(I) ) ) THEN
///
/// WRITE(*,*) 'The file ', FNAME(I)(:RTRIM(FNAME(I))),
/// . ' exists.'
///
/// ELSE
///
/// WRITE(*,*) 'Cannot find the file ', FNAME(I)
///
/// END IF
///
/// END DO
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Cannot find the file exists_ex1.txt
/// The file exists_ex1.pgm exists.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// K.R. Gehringer (JPL)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.3.0, 17-JUN-2021 (JDR)
///
/// Edited the header to comply with NAIF standard.
/// Added complete code example.
///
/// Changed input argument name FILE to FNAME for consistency with
/// other routines.
///
/// Added IMPLICIT NONE statement.
///
/// - SPICELIB Version 2.2.1, 01-JUL-2014 (NJB)
///
/// VAX examples were deleted from the header.
///
/// - SPICELIB Version 2.2.0, 09-DEC-1999 (WLT)
///
/// The input file name is now "trimmed" of trailing blanks
/// before checking its existence.
///
/// - SPICELIB Version 2.1.0, 04-MAR-1996 (KRG)
///
/// Added a local logical variable that is used as temporary
/// storage for the results from the INQUIRE statement rather
/// than using the function name. This solved a problem on the
/// macintosh.
///
/// - SPICELIB Version 2.0.0, 04-AUG-1994 (KRG)
///
/// Added a test to see if the filename was blank before the
/// INQUIRE statement. This allows a meaningful error message to
/// be presented.
///
/// - SPICELIB Version 1.1.0, 17-MAY-1994 (HAN)
///
/// If the value of the function RETURN is .TRUE. upon execution of
/// this module, this function is assigned a default value of
/// 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, 31-JAN-1990 (IMU)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 2.0.0, 29-DEC-1988 (HAN)
///
/// The IOSTAT specifier was added to the INQUIRE statement.
/// If the value of IOSTAT is not equal to zero, an error
/// occurred during the execution of the INQUIRE statement.
/// In this case, a SPICELIB error is signaled and the routine
/// checks out.
/// ```
pub fn exists(ctx: &mut SpiceContext, fname: &str) -> crate::Result<bool> {
let ret = EXISTS(fname.as_bytes(), ctx.raw_context())?;
ctx.handle_errors()?;
Ok(ret)
}
//$Procedure EXISTS ( Does the file exist? )
pub fn EXISTS(FNAME: &[u8], ctx: &mut Context) -> f2rust_std::Result<bool> {
let mut EXISTS: bool = false;
let mut IOSTAT: i32 = 0;
let mut R: i32 = 0;
let mut MYEXST: bool = false;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
EXISTS = false;
return Ok(EXISTS);
} else {
CHKIN(b"EXISTS", ctx)?;
}
//
// Initialize the local variable MYEXST to be .FALSE.
//
MYEXST = false;
//
// First we test to see if the filename is blank.
//
if fstr::eq(FNAME, b" ") {
EXISTS = false;
SETMSG(b"The file name is blank. ", ctx);
SIGERR(b"SPICE(BLANKFILENAME)", ctx)?;
CHKOUT(b"EXISTS", ctx)?;
return Ok(EXISTS);
}
R = RTRIM(FNAME);
//
// So simple, it defies explanation.
//
{
use f2rust_std::io;
let specs = io::InquireSpecs {
file: Some(fstr::substr(FNAME, 1..=R)),
exist: Some(&mut MYEXST),
..Default::default()
};
IOSTAT = io::capture_iostat(|| ctx.inquire(specs))?;
}
if (IOSTAT != 0) {
EXISTS = false;
SETMSG(b"Value of IOSTAT was *.", ctx);
ERRINT(b"*", IOSTAT, ctx);
SIGERR(b"SPICE(INQUIREFAILED)", ctx)?;
CHKOUT(b"EXISTS", ctx)?;
return Ok(EXISTS);
}
//
// Set the value of the function, check out and return.
//
EXISTS = MYEXST;
CHKOUT(b"EXISTS", ctx)?;
Ok(EXISTS)
}