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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const BMARK: &[u8] = b"~NAIF/SPC BEGIN COMMENTS~";
const EMARK: &[u8] = b"~NAIF/SPC END COMMENTS~";
/// SPK and CK, binary to text
///
/// Convert the contents of a binary SPK or CK file to text,
/// including comments if present, and write them to a text file
/// opened by the calling program.
///
/// # Required Reading
///
/// * [SPC](crate::required_reading::spc)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// BINARY I Name of an existing binary SPK or CK file.
/// UNIT I Logical unit connected to a text file.
/// ```
///
/// # Detailed Input
///
/// ```text
/// BINARY is the name of an existing binary SPK or CK file
/// that may contain comments in its comment area.
///
/// UNIT is the logical unit connected to a text file that
/// has been opened for write access. Use the routine
/// TXTOPN to open this file. Upon exit, this file will
/// contain the same data and comments as the binary
/// file, but in text format which is more suitable for
/// transfer between heterogeneous computing environments.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If there is a problem opening or reading from the binary file,
/// an error is signaled by a routine in the call tree of this
/// routine.
///
/// 2) If there is a problem writing to the text file,
/// the error SPICE(FILEWRITEFAILED) is signaled.
/// ```
///
/// # Files
///
/// ```text
/// See arguments BINARY and UNIT above.
/// ```
///
/// # Particulars
///
/// ```text
/// The SPICELIB SPK and CK reader subroutines read binary files.
/// However, because different computing environments have different
/// binary representations of numbers, you must convert SPK and CK
/// files to text format when porting from one system to another.
/// After converting the file to text, you can transfer it using
/// a transfer protocol program like Kermit or FTP. Then, convert
/// the text file back to binary format.
///
/// The following is a list of the SPICELIB routines that convert
/// SPK and CK files between binary and text format:
///
/// SPCA2B converts text to binary. It opens the text file,
/// creates a new binary file, and closes both files.
///
/// SPCB2A converts binary to text. It opens the binary file,
/// creates a new text file, and closes both files.
///
/// SPCT2B converts text to binary. It creates a new binary
/// file and closes it. The text file is open on
/// entrance and exit.
///
/// SPCB2T converts binary to text. It opens the binary
/// file and closes it. The text file is open on
/// entrance and exit
///
/// See the SPC required reading for more information
/// about SPC routines and the SPK and CK file formats.
/// ```
///
/// # Examples
///
/// ```text
/// The following code fragment creates a text file containing
/// text format SPK data and comments preceded and followed
/// by a standard label.
///
/// The SPICELIB routine TXTOPN opens a new text file and TXTOPR
/// opens an existing text file for read access. TEXT and
/// BINARY are character strings that contain the names of the
/// text and binary files.
///
/// CALL TXTOPN ( TEXT, UNIT )
///
/// (Write header label to UNIT)
///
/// CALL SPCB2T ( BINARY, UNIT )
///
/// (Write trailing label to UNIT)
///
/// CLOSE ( UNIT )
///
///
/// The following code fragment reconverts the text format
/// SPK data and comments back into binary format.
///
/// CALL TXTOPR ( TEXT, UNIT )
///
/// (Read, or just read past, header label from UNIT)
///
/// CALL SPCT2B ( UNIT, BINARY )
///
/// (Read trailing label from UNIT, if desired )
///
/// CLOSE ( UNIT )
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) This routine assumes that the comment area of the binary SPK
/// or CK file contains only text stored by SPCAC. Comments
/// written any other way may not be handled properly.
///
/// 2) UNIT must be obtained via TXTOPN. Use TXTOPN to open new
/// text files for write access and get the logical unit.
/// System dependencies regarding opening text files have
/// been isolated in the routines TXTOPN and TXTOPR.
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// J.E. McLean (JPL)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 03-JUN-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - 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 spcb2t(ctx: &mut SpiceContext, binary: &str, unit: i32) -> crate::Result<()> {
SPCB2T(binary.as_bytes(), unit, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure SPCB2T ( SPK and CK, binary to text )
pub fn SPCB2T(BINARY: &[u8], UNIT: i32, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut HANDLE: i32 = 0;
let mut IOSTAT: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
// IFNLEN is the length of a DAF internal file name.
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"SPCB2T", ctx)?;
}
//
// First, convert the binary data to text and write it to
// the text file.
//
DAFB2T(BINARY, UNIT, ctx)?;
//
// Next, write the begin comments marker.
//
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(BMARK)?;
writer.finish()?;
Ok(())
})?;
}
if (IOSTAT != 0) {
SETMSG(
b"Error writing the begin comments marker to the text file named FNM. IOSTAT = #.",
ctx,
);
ERRFNM(b"FNM", UNIT, ctx)?;
ERRINT(b"#", IOSTAT, ctx);
SIGERR(b"SPICE(FILEWRITEFAILED)", ctx)?;
CHKOUT(b"SPCB2T", ctx)?;
return Ok(());
}
//
// Open the DAF for read access, extract the comments from
// it and write them to the text file, then close the DAF.
// If the comment area of the binary file is empty, SPCEC
// writes nothing to the text file, but even so, we still
// want the markers.
//
DAFOPR(BINARY, &mut HANDLE, ctx)?;
SPCEC(HANDLE, UNIT, ctx)?;
DAFCLS(HANDLE, ctx)?;
//
// Finally, write the end comments marker.
//
{
use f2rust_std::{
data::Val,
io::{self, Writer},
};
let mut writer = io::ListDirectedWriter::new(ctx.io_unit(UNIT)?, None)?;
IOSTAT = io::capture_iostat(|| {
writer.start()?;
writer.write_str(EMARK)?;
writer.finish()?;
Ok(())
})?;
}
if (IOSTAT != 0) {
SETMSG(
b"Error writing the end comments marker to the text file named FNM. IOSTAT = #.",
ctx,
);
ERRFNM(b"FNM", UNIT, ctx)?;
ERRINT(b"#", IOSTAT, ctx);
SIGERR(b"SPICE(FILEWRITEFAILED)", ctx)?;
CHKOUT(b"SPCB2T", ctx)?;
return Ok(());
}
CHKOUT(b"SPCB2T", ctx)?;
Ok(())
}