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