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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const MINLEN: i32 = 5;
const CHBASE: i32 = 128;
/// Encode a character string, portably
///
/// Encode a nonnegative integer number into a character string,
/// portably, using 128 as the base for encoding.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// NUMBER I Number to be encoded.
/// STRING O Encoded string.
/// MINLEN P Minimum length of string.
/// ```
///
/// # Detailed Input
///
/// ```text
/// NUMBER is an arbitrary nonnegative integer.
/// ```
///
/// # Detailed Output
///
/// ```text
/// STRING is the character string implied by the ASCII
/// interpretation of NUMBER when converted to its
/// base 128 representation.
///
/// Let L be the declared length of STRING, and let
/// NUMBER be given by
///
/// 0 1 L-1
/// NUMBER = a 128 + a 128 + ... + a 128
/// 1 2 L
///
/// Then
///
/// STRING(i:i) = CHAR(a ) for i = 1, L
/// i
///
/// Note that, just as for any other "numbers",
/// the "digits" in STRING are arranged from right
/// to left in order of increasing significance.
/// The string is, in effect, "padded with nulls"
/// on the left.
/// ```
///
/// # Parameters
///
/// ```text
/// MINLEN is the minimum length of a string into which a
/// number may be encoded. In order to avoid padding
/// long strings with hundreds, possibly thousands
/// of null characters, only the first MINLEN characters
/// of the string are actually used. Note that this
/// also allows the encoded number to be preserved
/// during assignments,
///
/// STR1 = STR2
///
/// so long as both strings are of length MINLEN or
/// greater.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the length of the output string is less than MINLEN,
/// the error SPICE(INSUFFLEN) is signaled.
///
/// 2) If the number to be encoded is negative, the error
/// SPICE(OUTOFRANGE) is signaled.
///
/// MINLEN
/// 3) If the number to be encoded is larger than 128 - 1,
/// the error SPICE(OUTOFRANGE) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine is identical to ENCHAR, except that this routine
/// does not use the machine-dependent encoding base returned by
/// the SPICELIB routine CHBASE. Instead, the base 128 is used.
/// This base is expected to work on all systems supporting ASCII
/// encoding of characters.
/// ```
///
/// # Examples
///
/// ```text
/// See: SCARDC, SSIZEC.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 12-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.0, 19-DEC-1995 (NJB) (WLT)
/// ```
pub fn prtenc(ctx: &mut SpiceContext, number: i32, string: &mut str) -> crate::Result<()> {
PRTENC(
number,
fstr::StrBytes::new(string).as_mut(),
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure PRTENC ( Encode a character string, portably )
pub fn PRTENC(NUMBER: i32, STRING: &mut [u8], ctx: &mut Context) -> f2rust_std::Result<()> {
let mut BASE: i32 = 0;
let mut NUM: i32 = 0;
let mut REMAIN: i32 = 0;
//
// Local parameters
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if (intrinsics::LEN(STRING) < MINLEN) {
CHKIN(b"PRTENC", ctx)?;
SIGERR(b"SPICE(INSUFFLEN)", ctx)?;
CHKOUT(b"PRTENC", ctx)?;
return Ok(());
} else if (NUMBER < 0) {
CHKIN(b"PRTENC", ctx)?;
SIGERR(b"SPICE(OUTOFRANGE)", ctx)?;
CHKOUT(b"PRTENC", ctx)?;
return Ok(());
}
//
// Generate the digits from right to left.
//
BASE = CHBASE;
NUM = NUMBER;
for I in intrinsics::range(MINLEN, 1, -1) {
REMAIN = intrinsics::MOD(NUM, BASE);
fstr::assign(fstr::substr_mut(STRING, I..=I), &intrinsics::CHAR(REMAIN));
NUM = (NUM / BASE);
}
//
// More error handling.
//
if (NUM > 0) {
CHKIN(b"PRTENC", ctx)?;
SIGERR(b"SPICE(OUTOFRANGE)", ctx)?;
CHKOUT(b"PRTENC", ctx)?;
}
Ok(())
}
/// Decode a character string
///
/// Decode a character string encoded by PRTENC.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// STRING I Encoded character string.
/// NUMBER O Decoded number.
/// ```
///
/// # Detailed Input
///
/// ```text
/// STRING is a character string previously encoded by PRTENC.
/// This contains an integer in base 128 notation,
/// where 128 is a function of the size of the
/// available character set. See PRTENC for details
/// about the format of STRING.
/// ```
///
/// # Detailed Output
///
/// ```text
/// NUMBER is the integer encoded in the input string.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the length of the input string is less than MINLEN,
/// the error SPICE(INSUFFLEN) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
/// PRTDEC is the inverse of PRTENC. In the example below,
///
/// CALL PRTENC ( I, STRING )
/// CALL PRTDEC ( STRING, J )
///
/// IF ( I .EQ. J ) THEN
/// .
/// .
/// END IF
///
/// the logical test (I .EQ. J) is always true.
///
/// This routine is identical to DECHAR, except that this routine
/// does not use the machine-dependent encoding base returned by
/// the SPICELIB routine CHBASE. Instead, the base 128 is used.
/// This base is expected to work on all systems supporting ASCII
/// encoding of characters.
/// ```
///
/// # Examples
///
/// ```text
/// See: CARDC, SIZEC.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 12-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.0, 19-DEC-1995 (NJB) (WLT)
/// ```
pub fn prtdec(ctx: &mut SpiceContext, string: &str, number: &mut i32) -> crate::Result<()> {
PRTDEC(string.as_bytes(), number, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure PRTDEC ( Decode a character string )
pub fn PRTDEC(STRING: &[u8], NUMBER: &mut i32, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut BASE: i32 = 0;
if (intrinsics::LEN(STRING) < MINLEN) {
CHKIN(b"PRTDEC", ctx)?;
SIGERR(b"SPICE(INSUFFLEN)", ctx)?;
CHKOUT(b"PRTDEC", ctx)?;
return Ok(());
}
//
// Sum the products of the 'digits' and the corresponding powers
// of NDCHAR, just like any other base conversion.
//
BASE = CHBASE;
*NUMBER = 0;
for I in 1..=MINLEN {
*NUMBER = ((BASE * *NUMBER) + intrinsics::ICHAR(fstr::substr(STRING, I..=I)));
}
Ok(())
}