1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include <data/val-type.h>
26 #include <libpspp/compiler.h>
27 #include <libpspp/float-format.h>
28 #include <libpspp/integer-format.h>
29 #include <libpspp/misc.h>
37 #define _(msgid) gettext (msgid)
41 const char *file_name;
44 int n_variable_records, n_variables;
46 enum integer_format integer_format;
47 enum float_format float_format;
50 static void read_header (struct sfm_reader *);
51 static void read_variable_record (struct sfm_reader *);
52 static void read_value_label_record (struct sfm_reader *);
53 static void read_document_record (struct sfm_reader *);
54 static void read_extension_record (struct sfm_reader *);
55 static void read_machine_integer_info (struct sfm_reader *,
56 size_t size, size_t count);
57 static void read_machine_float_info (struct sfm_reader *,
58 size_t size, size_t count);
59 static void read_display_parameters (struct sfm_reader *,
60 size_t size, size_t count);
61 static void read_long_var_name_map (struct sfm_reader *r,
62 size_t size, size_t count);
63 static void read_long_string_map (struct sfm_reader *r,
64 size_t size, size_t count);
65 static void read_datafile_attributes (struct sfm_reader *r,
66 size_t size, size_t count);
67 static void read_variable_attributes (struct sfm_reader *r,
68 size_t size, size_t count);
70 static struct text_record *open_text_record (
71 struct sfm_reader *, size_t size);
72 static void close_text_record (struct text_record *);
73 static bool read_variable_to_value_pair (struct text_record *,
74 char **key, char **value);
75 static char *text_tokenize (struct text_record *, int delimiter);
76 static bool text_match (struct text_record *text, int c);
78 static void usage (int exit_code);
79 static void sys_warn (struct sfm_reader *, const char *, ...)
81 static void sys_error (struct sfm_reader *, const char *, ...)
85 static void read_bytes (struct sfm_reader *, void *, size_t);
86 static int read_int (struct sfm_reader *);
87 static double read_float (struct sfm_reader *);
88 static void read_string (struct sfm_reader *, char *, size_t);
89 static void skip_bytes (struct sfm_reader *, size_t);
90 static void trim_spaces (char *);
93 main (int argc, char *argv[])
98 set_program_name (argv[0]);
100 usage (EXIT_FAILURE);
102 for (i = 1; i < argc; i++)
106 r.file_name = argv[i];
107 r.file = fopen (r.file_name, "rb");
109 error (EXIT_FAILURE, errno, "error opening \"%s\"", r.file_name);
110 r.n_variable_records = 0;
114 printf ("Reading \"%s\":\n", r.file_name);
117 while ((rec_type = read_int (&r)) != 999)
122 read_variable_record (&r);
126 read_value_label_record (&r);
130 sys_error (&r, _("Misplaced type 4 record."));
133 read_document_record (&r);
137 read_extension_record (&r);
141 sys_error (&r, _("Unrecognized record type %d."), rec_type);
144 printf ("%08lx: end-of-dictionary record "
145 "(first byte of data at %08lx)\n",
146 ftell (r.file), ftell (r.file) + 4);
155 read_header (struct sfm_reader *r)
158 char eye_catcher[61];
159 uint8_t raw_layout_code[4];
161 int32_t nominal_case_size;
163 int32_t weight_index;
167 char creation_date[10];
168 char creation_time[9];
171 read_string (r, rec_type, sizeof rec_type);
172 read_string (r, eye_catcher, sizeof eye_catcher);
174 if (strcmp ("$FL2", rec_type) != 0)
175 sys_error (r, _("This is not an SPSS system file."));
177 /* Identify integer format. */
178 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
179 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
181 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
183 || (r->integer_format != INTEGER_MSB_FIRST
184 && r->integer_format != INTEGER_LSB_FIRST))
185 sys_error (r, _("This is not an SPSS system file."));
186 layout_code = integer_get (r->integer_format,
187 raw_layout_code, sizeof raw_layout_code);
189 nominal_case_size = read_int (r);
190 compressed = read_int (r) != 0;
191 weight_index = read_int (r);
192 ncases = read_int (r);
194 /* Identify floating-point format and obtain compression bias. */
195 read_bytes (r, raw_bias, sizeof raw_bias);
196 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
198 sys_warn (r, _("Compression bias is not the usual "
199 "value of 100, or system file uses unrecognized "
200 "floating-point format."));
201 if (r->integer_format == INTEGER_MSB_FIRST)
202 r->float_format = FLOAT_IEEE_DOUBLE_BE;
204 r->float_format = FLOAT_IEEE_DOUBLE_LE;
206 bias = float_get_double (r->float_format, raw_bias);
208 read_string (r, creation_date, sizeof creation_date);
209 read_string (r, creation_time, sizeof creation_time);
210 read_string (r, file_label, sizeof file_label);
211 trim_spaces (file_label);
214 printf ("File header record:\n");
215 printf ("\t%17s: %s\n", "Product name", eye_catcher);
216 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
217 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
218 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
219 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
220 printf ("\t%17s: %g\n", "Compression bias", bias);
221 printf ("\t%17s: %s\n", "Creation date", creation_date);
222 printf ("\t%17s: %s\n", "Creation time", creation_time);
223 printf ("\t%17s: \"%s\"\n", "File label", file_label);
227 format_name (int format)
229 switch ((format >> 16) & 0xff)
232 case 2: return "AHEX";
233 case 3: return "COMMA";
234 case 4: return "DOLLAR";
237 case 7: return "PIBHEX";
239 case 9: return "PIB";
240 case 10: return "PK";
241 case 11: return "RB";
242 case 12: return "RBHEX";
246 case 20: return "DATE";
247 case 21: return "TIME";
248 case 22: return "DATETIME";
249 case 23: return "ADATE";
250 case 24: return "JDATE";
251 case 25: return "DTIME";
252 case 26: return "WKDAY";
253 case 27: return "MONTH";
254 case 28: return "MOYR";
255 case 29: return "QYR";
256 case 30: return "WKYR";
257 case 31: return "PCT";
258 case 32: return "DOT";
259 case 33: return "CCA";
260 case 34: return "CCB";
261 case 35: return "CCC";
262 case 36: return "CCD";
263 case 37: return "CCE";
264 case 38: return "EDATE";
265 case 39: return "SDATE";
266 default: return "invalid";
270 /* Reads a variable (type 2) record from R and adds the
271 corresponding variable to DICT.
272 Also skips past additional variable records for long string
275 read_variable_record (struct sfm_reader *r)
278 int has_variable_label;
279 int missing_value_code;
284 printf ("%08lx: variable record #%d\n",
285 ftell (r->file), r->n_variable_records++);
287 width = read_int (r);
288 has_variable_label = read_int (r);
289 missing_value_code = read_int (r);
290 print_format = read_int (r);
291 write_format = read_int (r);
292 read_string (r, name, sizeof name);
293 name[strcspn (name, " ")] = '\0';
298 printf ("\tWidth: %d (%s)\n",
301 : width == 0 ? "numeric"
302 : "long string continuation record");
303 printf ("\tVariable label: %d\n", has_variable_label);
304 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
305 (missing_value_code == 0 ? "no missing values"
306 : missing_value_code == 1 ? "one missing value"
307 : missing_value_code == 2 ? "two missing values"
308 : missing_value_code == 3 ? "three missing values"
309 : missing_value_code == -2 ? "one missing value range"
310 : missing_value_code == -3 ? "one missing value, one range"
312 printf ("\tPrint format: %06x (%s%d.%d)\n",
313 print_format, format_name (print_format),
314 (print_format >> 8) & 0xff, print_format & 0xff);
315 printf ("\tWrite format: %06x (%s%d.%d)\n",
316 write_format, format_name (write_format),
317 (write_format >> 8) & 0xff, write_format & 0xff);
318 printf ("\tName: %s\n", name);
320 /* Get variable label, if any. */
321 if (has_variable_label != 0 && has_variable_label != 1)
322 sys_error (r, _("Variable label indicator field is not 0 or 1."));
323 if (has_variable_label == 1)
325 long int offset = ftell (r->file);
330 if (len >= sizeof label)
331 sys_error (r, _("Variable %s has label of invalid length %zu."),
333 read_string (r, label, len + 1);
334 printf("\t%08lx Variable label: \"%s\"\n", offset, label);
336 skip_bytes (r, ROUND_UP (len, 4) - len);
339 /* Set missing values. */
340 if (missing_value_code != 0)
344 printf ("\t%08lx Missing values:", ftell (r->file));
347 if (missing_value_code < -3 || missing_value_code > 3
348 || missing_value_code == -1)
349 sys_error (r, _("Numeric missing value indicator field is not "
350 "-3, -2, 0, 1, 2, or 3."));
351 if (missing_value_code < 0)
353 double low = read_float (r);
354 double high = read_float (r);
355 printf (" %g...%g", low, high);
356 missing_value_code = -missing_value_code - 2;
358 for (i = 0; i < missing_value_code; i++)
359 printf (" %g", read_float (r));
363 if (missing_value_code < 1 || missing_value_code > 3)
364 sys_error (r, _("String missing value indicator field is not "
366 for (i = 0; i < missing_value_code; i++)
369 read_string (r, string, sizeof string);
370 printf (" \"%s\"", string);
377 /* Reads value labels from sysfile R and inserts them into the
378 associated dictionary. */
380 read_value_label_record (struct sfm_reader *r)
382 int label_cnt, var_cnt;
385 printf ("%08lx: value labels record\n", ftell (r->file));
387 /* Read number of labels. */
388 label_cnt = read_int (r);
389 for (i = 0; i < label_cnt; i++)
394 unsigned char label_len;
398 read_bytes (r, raw_value, sizeof raw_value);
399 value = float_get_double (r->float_format, raw_value);
400 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
401 if (!isprint (raw_value[n_printable]))
404 /* Read label length. */
405 read_bytes (r, &label_len, sizeof label_len);
406 padded_len = ROUND_UP (label_len + 1, 8);
408 /* Read label, padding. */
409 read_bytes (r, label, padded_len - 1);
410 label[label_len] = 0;
412 printf ("\t%g/\"%.*s\": \"%s\"\n", value, n_printable, raw_value, label);
415 /* Now, read the type 4 record that has the list of variables
416 to which the value labels are to be applied. */
418 /* Read record type of type 4 record. */
419 if (read_int (r) != 4)
420 sys_error (r, _("Variable index record (type 4) does not immediately "
421 "follow value label record (type 3) as it should."));
423 /* Read number of variables associated with value label from type 4
425 printf ("\t%08lx: apply to variables", ftell (r->file));
426 var_cnt = read_int (r);
427 for (i = 0; i < var_cnt; i++)
428 printf (" #%d", read_int (r));
433 read_document_record (struct sfm_reader *r)
438 printf ("%08lx: document record\n", ftell (r->file));
439 n_lines = read_int (r);
440 printf ("\t%d lines of documents\n", n_lines);
442 for (i = 0; i < n_lines; i++)
445 printf ("\t%08lx: ", ftell (r->file));
446 read_string (r, line, sizeof line);
448 printf ("line %d: \"%s\"\n", i, line);
453 read_extension_record (struct sfm_reader *r)
455 long int offset = ftell (r->file);
456 int subtype = read_int (r);
457 size_t size = read_int (r);
458 size_t count = read_int (r);
459 size_t bytes = size * count;
461 printf ("%08lx: Record 7, subtype %d, size=%zu, count=%zu\n",
462 offset, subtype, size, count);
467 read_machine_integer_info (r, size, count);
471 read_machine_float_info (r, size, count);
475 /* Variable sets information. We don't use these yet.
476 They only apply to GUIs; see VARSETS on the APPLY
477 DICTIONARY command in SPSS documentation. */
481 /* DATE variable information. We don't use it yet, but we
486 /* Unknown purpose. */
490 read_display_parameters (r, size, count);
494 read_long_var_name_map (r, size, count);
498 read_long_string_map (r, size, count);
502 /* New in SPSS v14? Unknown purpose. */
506 read_datafile_attributes (r, size, count);
510 read_variable_attributes (r, size, count);
514 sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
518 skip_bytes (r, bytes);
522 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
524 long int offset = ftell (r->file);
525 int version_major = read_int (r);
526 int version_minor = read_int (r);
527 int version_revision = read_int (r);
528 int machine_code = read_int (r);
529 int float_representation = read_int (r);
530 int compression_code = read_int (r);
531 int integer_representation = read_int (r);
532 int character_code = read_int (r);
534 printf ("%08lx: machine integer info\n", offset);
535 if (size != 4 || count != 8)
536 sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
540 printf ("\tVersion: %d.%d.%d\n",
541 version_major, version_minor, version_revision);
542 printf ("\tMachine code: %d\n", machine_code);
543 printf ("\tFloating point representation: %d (%s)\n",
544 float_representation,
545 float_representation == 1 ? "IEEE 754"
546 : float_representation == 2 ? "IBM 370"
547 : float_representation == 3 ? "DEC VAX"
549 printf ("\tCompression code: %d\n", compression_code);
550 printf ("\tEndianness: %d (%s)\n", integer_representation,
551 integer_representation == 1 ? "big"
552 : integer_representation == 2 ? "little" : "unknown");
553 printf ("\tCharacter code: %d\n", character_code);
556 /* Read record type 7, subtype 4. */
558 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
560 long int offset = ftell (r->file);
561 double sysmis = read_float (r);
562 double highest = read_float (r);
563 double lowest = read_float (r);
565 printf ("%08lx: machine float info\n", offset);
566 if (size != 8 || count != 3)
567 sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
570 printf ("\tsysmis: %g\n", sysmis);
571 if (sysmis != SYSMIS)
572 sys_warn (r, _("File specifies unexpected value %g as SYSMIS."), sysmis);
573 printf ("\thighest: %g\n", highest);
574 if (highest != HIGHEST)
575 sys_warn (r, _("File specifies unexpected value %g as HIGHEST."), highest);
576 printf ("\tlowest: %g\n", lowest);
577 if (lowest != LOWEST)
578 sys_warn (r, _("File specifies unexpected value %g as LOWEST."), lowest);
581 /* Read record type 7, subtype 11. */
583 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
589 printf ("%08lx: variable display parameters\n", ftell (r->file));
592 sys_warn (r, _("Bad size %zu on extension 11."), size);
593 skip_bytes (r, size * count);
597 n_vars = r->n_variables;
598 if (count == 3 * n_vars)
599 includes_width = true;
600 else if (count == 2 * n_vars)
601 includes_width = false;
604 sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
606 skip_bytes (r, size * count);
610 for (i = 0; i < n_vars; ++i)
612 int measure = read_int (r);
613 int width = includes_width ? read_int (r) : 0;
614 int align = read_int (r);
616 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
617 (measure == 1 ? "nominal"
618 : measure == 2 ? "ordinal"
619 : measure == 3 ? "scale"
622 printf (", width=%d", width);
623 printf (", align=%d (%s)\n", align,
625 : align == 1 ? "right"
626 : align == 2 ? "centre"
631 /* Reads record type 7, subtype 13, which gives the long name
632 that corresponds to each short name. */
634 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
636 struct text_record *text;
640 printf ("%08lx: long variable names (short => long)\n", ftell (r->file));
641 text = open_text_record (r, size * count);
642 while (read_variable_to_value_pair (text, &var, &long_name))
643 printf ("\t%s => %s\n", var, long_name);
644 close_text_record (text);
647 /* Reads record type 7, subtype 14, which gives the real length
648 of each very long string. Rearranges DICT accordingly. */
650 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
652 struct text_record *text;
656 printf ("%08lx: very long strings (variable => length)\n", ftell (r->file));
657 text = open_text_record (r, size * count);
658 while (read_variable_to_value_pair (text, &var, &length_s))
659 printf ("\t%s => %d\n", var, atoi (length_s));
660 close_text_record (text);
664 read_attributes (struct sfm_reader *r, struct text_record *text,
665 const char *variable)
672 key = text_tokenize (text, '(');
676 for (index = 1; ; index++)
678 /* Parse the value. */
679 const char *value = text_tokenize (text, '\n');
682 sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
683 variable, key, index);
686 if (strlen (value) < 2
687 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
688 sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
689 variable, key, index, value);
691 printf ("\t%s: %s[%d] = \"%.*s\"\n",
692 variable, key, index, (int) strlen (value) - 2, value + 1);
694 /* Was this the last value for this attribute? */
695 if (text_match (text, ')'))
699 if (text_match (text, '/'))
705 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
707 struct text_record *text;
709 printf ("%08lx: datafile attributes\n", ftell (r->file));
710 text = open_text_record (r, size * count);
711 read_attributes (r, text, "datafile");
712 close_text_record (text);
716 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
718 struct text_record *text;
720 printf ("%08lx: variable attributes\n", ftell (r->file));
721 text = open_text_record (r, size * count);
724 const char *variable = text_tokenize (text, ':');
725 if (variable == NULL || !read_attributes (r, text, variable))
728 close_text_record (text);
731 /* Helpers for reading records that consist of structured text
737 char *buffer; /* Record contents. */
738 size_t size; /* Size of buffer. */
739 size_t pos; /* Current position in buffer. */
742 /* Reads SIZE bytes into a text record for R,
743 and returns the new text record. */
744 static struct text_record *
745 open_text_record (struct sfm_reader *r, size_t size)
747 struct text_record *text = xmalloc (sizeof *text);
748 char *buffer = xmalloc (size + 1);
749 read_bytes (r, buffer, size);
750 text->buffer = buffer;
756 /* Closes TEXT and frees its storage.
757 Not really needed, because the pool will free the text record anyway,
758 but can be used to free it earlier. */
760 close_text_record (struct text_record *text)
767 text_tokenize (struct text_record *text, int delimiter)
769 size_t start = text->pos;
770 while (text->pos < text->size
771 && text->buffer[text->pos] != delimiter
772 && text->buffer[text->pos] != '\0')
774 if (text->pos == text->size)
776 text->buffer[text->pos++] = '\0';
777 return &text->buffer[start];
781 text_match (struct text_record *text, int c)
783 if (text->pos < text->size && text->buffer[text->pos] == c)
792 /* Reads a variable=value pair from TEXT.
793 Looks up the variable in DICT and stores it into *VAR.
794 Stores a null-terminated value into *VALUE. */
796 read_variable_to_value_pair (struct text_record *text,
797 char **key, char **value)
799 *key = text_tokenize (text, '=');
800 *value = text_tokenize (text, '\t');
801 if (!*key || !*value)
804 while (text->pos < text->size
805 && (text->buffer[text->pos] == '\t'
806 || text->buffer[text->pos] == '\0'))
812 usage (int exit_code)
814 printf ("usage: %s SYSFILE...\n"
815 "where each SYSFILE is the name of a system file\n",
820 /* Displays a corruption message. */
822 sys_msg (struct sfm_reader *r, const char *format, va_list args)
824 printf ("\"%s\" near offset 0x%lx: ",
825 r->file_name, (unsigned long) ftell (r->file));
826 vprintf (format, args);
830 /* Displays a warning for the current file position. */
832 sys_warn (struct sfm_reader *r, const char *format, ...)
836 va_start (args, format);
837 sys_msg (r, format, args);
841 /* Displays an error for the current file position,
842 marks it as in an error state,
843 and aborts reading it using longjmp. */
845 sys_error (struct sfm_reader *r, const char *format, ...)
849 va_start (args, format);
850 sys_msg (r, format, args);
856 /* Reads BYTE_CNT bytes into BUF.
857 Returns true if exactly BYTE_CNT bytes are successfully read.
858 Aborts if an I/O error or a partial read occurs.
859 If EOF_IS_OK, then an immediate end-of-file causes false to be
860 returned; otherwise, immediate end-of-file causes an abort
863 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
864 void *buf, size_t byte_cnt)
866 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
867 if (bytes_read == byte_cnt)
869 else if (ferror (r->file))
870 sys_error (r, _("System error: %s."), strerror (errno));
871 else if (!eof_is_ok || bytes_read != 0)
872 sys_error (r, _("Unexpected end of file."));
877 /* Reads BYTE_CNT into BUF.
878 Aborts upon I/O error or if end-of-file is encountered. */
880 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
882 read_bytes_internal (r, false, buf, byte_cnt);
885 /* Reads a 32-bit signed integer from R and returns its value in
888 read_int (struct sfm_reader *r)
891 read_bytes (r, integer, sizeof integer);
892 return integer_get (r->integer_format, integer, sizeof integer);
895 /* Reads a 64-bit floating-point number from R and returns its
896 value in host format. */
898 read_float (struct sfm_reader *r)
901 read_bytes (r, number, sizeof number);
902 return float_get_double (r->float_format, number);
905 /* Reads exactly SIZE - 1 bytes into BUFFER
906 and stores a null byte into BUFFER[SIZE - 1]. */
908 read_string (struct sfm_reader *r, char *buffer, size_t size)
911 read_bytes (r, buffer, size - 1);
912 buffer[size - 1] = '\0';
915 /* Skips BYTES bytes forward in R. */
917 skip_bytes (struct sfm_reader *r, size_t bytes)
922 size_t chunk = MIN (sizeof buffer, bytes);
923 read_bytes (r, buffer, chunk);
929 trim_spaces (char *s)
931 char *end = strchr (s, '\0');
932 while (end > s && end[-1] == ' ')