docs
[pspp] / doc / dev / portable-file-format.texi
1 @c PSPP - a program for statistical analysis.
2 @c Copyright (C) 2019 Free Software Foundation, Inc.
3 @c Permission is granted to copy, distribute and/or modify this document
4 @c under the terms of the GNU Free Documentation License, Version 1.3
5 @c or any later version published by the Free Software Foundation;
6 @c with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
7 @c A copy of the license is included in the section entitled "GNU
8 @c Free Documentation License".
9 @c
10
11 @node Portable File Format
12 @appendix Portable File Format
13
14 These days, most computers use the same internal data formats for
15 integer and floating-point data, if one ignores little differences like
16 big- versus little-endian byte ordering.  However, occasionally it is
17 necessary to exchange data between systems with incompatible data
18 formats.  This is what portable files are designed to do.
19
20 @strong{Please note:} This information is gleaned from examination of
21 ASCII-formatted portable files only, so some of it may be incorrect
22 for portable files formatted in EBCDIC or other character sets.
23
24 @menu
25 * Portable File Characters::
26 * Portable File Structure::
27 * Portable File Header::
28 * Version and Date Info Record::
29 * Identification Records::
30 * Variable Count Record::
31 * Precision Record::
32 * Case Weight Variable Record::
33 * Variable Records::
34 * Value Label Records::
35 * Portable File Document Record::
36 * Portable File Data::
37 @end menu
38
39 @node Portable File Characters
40 @section Portable File Characters
41
42 Portable files are arranged as a series of lines of 80
43 characters each.  Each line is terminated by a carriage-return,
44 line-feed sequence (``new-lines'').  New-lines are only used to avoid
45 line length limits imposed by some OSes; they are not meaningful.
46
47 Most lines in portable files are exactly 80 characters long.  The only
48 exception is a line that ends in one or more spaces, in which the
49 spaces may optionally be omitted.  Thus, a portable file reader must
50 act as though a line shorter than 80 characters is padded to that
51 length with spaces.
52
53 The file must be terminated with a @samp{Z} character.  In addition, if
54 the final line in the file does not have exactly 80 characters, then it
55 is padded on the right with @samp{Z} characters.  (The file contents may
56 be in any character set; the file contains a description of its own
57 character set, as explained in the next section.  Therefore, the
58 @samp{Z} character is not necessarily an ASCII @samp{Z}.)
59
60 For the rest of the description of the portable file format, new-lines
61 and the trailing @samp{Z}s will be ignored, as if they did not exist,
62 because they are not an important part of understanding the file
63 contents.
64
65 @node Portable File Structure
66 @section Portable File Structure
67
68 Every portable file consists of the following records, in sequence:
69
70 @itemize @bullet
71
72 @item
73 File header.
74
75 @item
76 Version and date info.
77
78 @item
79 Product identification.
80
81 @item
82 Author identification (optional).
83
84 @item
85 Subproduct identification (optional).
86
87 @item
88 Variable count.
89
90 @item
91 Case weight variable (optional).
92
93 @item
94 Variables.  Each variable record may optionally be followed by a
95 missing value record and a variable label record.
96
97 @item
98 Value labels (optional).
99
100 @item
101 Documents (optional).
102
103 @item
104 Data.
105 @end itemize
106
107 Most records are identified by a single-character tag code.  The file
108 header and version info record do not have a tag.
109
110 Other than these single-character codes, there are three types of fields
111 in a portable file: floating-point, integer, and string.  Floating-point
112 fields have the following format:
113
114 @itemize @bullet
115
116 @item
117 Zero or more leading spaces.
118
119 @item
120 Optional asterisk (@samp{*}), which indicates a missing value.  The
121 asterisk must be followed by a single character, generally a period
122 (@samp{.}), but it appears that other characters may also be possible.
123 This completes the specification of a missing value.
124
125 @item
126 Optional minus sign (@samp{-}) to indicate a negative number.
127
128 @item
129 A whole number, consisting of one or more base-30 digits: @samp{0}
130 through @samp{9} plus capital letters @samp{A} through @samp{T}.
131
132 @item
133 Optional fraction, consisting of a radix point (@samp{.}) followed by
134 one or more base-30 digits.
135
136 @item
137 Optional exponent, consisting of a plus or minus sign (@samp{+} or
138 @samp{-}) followed by one or more base-30 digits.
139
140 @item
141 A forward slash (@samp{/}).
142 @end itemize
143
144 Integer fields take a form identical to floating-point fields, but they
145 may not contain a fraction.
146
147 String fields take the form of a integer field having value @var{n},
148 followed by exactly @var{n} characters, which are the string content.
149
150 @node Portable File Header
151 @section Portable File Header
152
153 Every portable file begins with a 464-byte header, consisting of a
154 200-byte collection of vanity splash strings, followed by a 256-byte
155 character set translation table, followed by an 8-byte tag string.
156
157 The 200-byte segment is divided into five 40-byte sections, each of
158 which represents the string @code{@var{charset} SPSS PORT FILE} in a
159 different character set encoding, where @var{charset} is the name of
160 the character set used in the file, e.g.@: @code{ASCII} or
161 @code{EBCDIC}.  Each string is padded on the right with spaces in its
162 respective character set.
163
164 It appears that these strings exist only to inform those who might view
165 the file on a screen, and that they are not parsed by SPSS products.
166 Thus, they can be safely ignored.  For those interested, the strings are
167 supposed to be in the following character sets, in the specified order:
168 EBCDIC, 7-bit ASCII, CDC 6-bit ASCII, 6-bit ASCII, Honeywell 6-bit
169 ASCII.
170
171 The 256-byte segment describes a mapping from the character set used in
172 the portable file to an arbitrary character set having characters at the
173 following positions:
174
175 @table @asis
176 @item 0--60
177
178 Control characters.  Not important enough to describe in full here.
179
180 @item 61--63
181
182 Reserved.
183
184 @item 64--73
185
186 Digits @samp{0} through @samp{9}.
187
188 @item 74--99
189
190 Capital letters @samp{A} through @samp{Z}.
191
192 @item 100--125
193
194 Lowercase letters @samp{a} through @samp{z}.
195
196 @item 126
197
198 Space.
199
200 @item 127--130
201
202 Symbols @code{.<(+}
203
204 @item 131
205
206 Solid vertical pipe.
207
208 @item 132--142
209
210 Symbols @code{&[]!$*);^-/}
211
212 @item 143
213
214 Broken vertical pipe.
215
216 @item 144--150
217
218 Symbols @code{,%_>}?@code{`:}   @c @code{?} is an inverted question mark
219
220 @item 151
221
222 British pound symbol.
223
224 @item 152--155
225
226 Symbols @code{@@'="}.
227
228 @item 156
229
230 Less than or equal symbol.
231
232 @item 157
233
234 Empty box.
235
236 @item 158
237
238 Plus or minus.
239
240 @item 159
241
242 Filled box.
243
244 @item 160
245
246 Degree symbol.
247
248 @item 161
249
250 Dagger.
251
252 @item 162
253
254 Symbol @samp{~}.
255
256 @item 163
257
258 En dash.
259
260 @item 164
261
262 Lower left corner box draw.
263
264 @item 165
265
266 Upper left corner box draw.
267
268 @item 166
269
270 Greater than or equal symbol.
271
272 @item 167--176
273
274 Superscript @samp{0} through @samp{9}.
275
276 @item 177
277
278 Lower right corner box draw.
279
280 @item 178
281
282 Upper right corner box draw.
283
284 @item 179
285
286 Not equal symbol.
287
288 @item 180
289
290 Em dash.
291
292 @item 181
293
294 Superscript @samp{(}.
295
296 @item 182
297
298 Superscript @samp{)}.
299
300 @item 183
301
302 Horizontal dagger (?).
303
304 @item 184--186
305
306 Symbols @samp{@{@}\}.
307 @item 187
308
309 Cents symbol.
310
311 @item 188
312
313 Centered dot, or bullet.
314
315 @item 189--255
316
317 Reserved.
318 @end table
319
320 Symbols that are not defined in a particular character set are set to
321 the same value as symbol 64; i.e., to @samp{0}.
322
323 The 8-byte tag string consists of the exact characters @code{SPSSPORT}
324 in the portable file's character set, which can be used to verify that
325 the file is indeed a portable file.
326
327 @node Version and Date Info Record
328 @section Version and Date Info Record
329
330 This record does not have a tag code.  It has the following structure:
331
332 @itemize @bullet
333 @item
334 A single character identifying the file format version.  The letter A
335 represents version 0, and so on.
336
337 @item
338 An 8-character string field giving the file creation date in the format
339 YYYYMMDD.
340
341 @item
342 A 6-character string field giving the file creation time in the format
343 HHMMSS.
344 @end itemize
345
346 @node Identification Records
347 @section Identification Records
348
349 The product identification record has tag code @samp{1}.  It consists of
350 a single string field giving the name of the product that wrote the
351 portable file.
352
353 The author identification record has tag code @samp{2}.  It is
354 optional.  If present, it consists of a single string field giving the
355 name of the person who caused the portable file to be written.
356
357 The subproduct identification record has tag code @samp{3}.  It is
358 optional.  If present, it consists of a single string field giving
359 additional information on the product that wrote the portable file.
360
361 @node Variable Count Record
362 @section Variable Count Record
363
364 The variable count record has tag code @samp{4}.  It consists of a
365 single integer field giving the number of variables in the file
366 dictionary.
367
368 @node Precision Record
369 @section Precision Record
370
371 The precision record has tag code @samp{5}.  It consists of a single
372 integer field specifying the maximum number of base-30 digits used in
373 data in the file.
374
375 @node Case Weight Variable Record
376 @section Case Weight Variable Record
377
378 The case weight variable record is optional.  If it is present, it
379 indicates the variable used for weighting cases; if it is absent,
380 cases are unweighted.  It has tag code @samp{6}.  It consists of a
381 single string field that names the weighting variable.
382
383 @node Variable Records
384 @section Variable Records
385
386 Each variable record represents a single variable.  Variable records
387 have tag code @samp{7}.  They have the following structure:
388
389 @itemize @bullet
390
391 @item
392 Width (integer).  This is 0 for a numeric variable, and a number between 1
393 and 255 for a string variable.
394
395 @item
396 Name (string).  1--8 characters long.  Must be in all capitals.
397
398 A few portable files that contain duplicate variable names have been
399 spotted in the wild.  PSPP handles these by renaming the duplicates
400 with numeric extensions: @code{@var{var}_1}, @code{@var{var}_2}, and
401 so on.
402
403 @item
404 Print format.  This is a set of three integer fields:
405
406 @itemize @minus
407
408 @item
409 Format type (@pxref{Variable Record}).
410
411 @item
412 Format width.  1--40.
413
414 @item
415 Number of decimal places.  1--40.
416 @end itemize
417
418 A few portable files with invalid format types or formats that are not
419 of the appropriate width for their variables have been spotted in the
420 wild.  PSPP assigns a default F or A format to a variable with an
421 invalid format.
422
423 @item
424 Write format.  Same structure as the print format described above.
425 @end itemize
426
427 Each variable record can optionally be followed by a missing value
428 record, which has tag code @samp{8}.  A missing value record has one
429 field, the missing value itself (a floating-point or string, as
430 appropriate).  Up to three of these missing value records can be used.
431
432 There is also a record for missing value ranges, which has tag code
433 @samp{B}.  It is followed by two fields representing the range, which
434 are floating-point or string as appropriate.  If a missing value range
435 is present, it may be followed by a single missing value record.
436
437 Tag codes @samp{9} and @samp{A} represent @code{LO THRU @var{x}} and
438 @code{@var{x} THRU HI} ranges, respectively.  Each is followed by a
439 single field representing @var{x}.  If one of the ranges is present, it
440 may be followed by a single missing value record.
441
442 In addition, each variable record can optionally be followed by a
443 variable label record, which has tag code @samp{C}.  A variable label
444 record has one field, the variable label itself (string).
445
446 @node Value Label Records
447 @section Value Label Records
448
449 Value label records have tag code @samp{D}.  They have the following
450 format:
451
452 @itemize @bullet
453 @item
454 Variable count (integer).
455
456 @item
457 List of variables (strings).  The variable count specifies the number in
458 the list.  Variables are specified by their names.  All variables must
459 be of the same type (numeric or string), but string variables do not
460 necessarily have the same width.
461
462 @item
463 Label count (integer).
464
465 @item
466 List of (value, label) tuples.  The label count specifies the number of
467 tuples.  Each tuple consists of a value, which is numeric or string as
468 appropriate to the variables, followed by a label (string).
469 @end itemize
470
471 A few portable files that specify duplicate value labels, that is, two
472 different labels for a single value of a single variable, have been
473 spotted in the wild.  PSPP uses the last value label specified in
474 these cases.
475
476 @node Portable File Document Record
477 @section Document Record
478
479 One document record may optionally follow the value label record.  The
480 document record consists of tag code @samp{E}, following by the number
481 of document lines as an integer, followed by that number of strings,
482 each of which represents one document line.  Document lines must be 80
483 bytes long or shorter.
484
485 @node Portable File Data
486 @section Portable File Data
487
488 The data record has tag code @samp{F}.  There is only one tag for all
489 the data; thus, all the data must follow the dictionary.  The data is
490 terminated by the end-of-file marker @samp{Z}, which is not valid as the
491 beginning of a data element.
492
493 Data elements are output in the same order as the variable records
494 describing them.  String variables are output as string fields, and
495 numeric variables are output as floating-point fields.
496 @setfilename ignored