Implement MRSETS command.
[pspp-builds.git] / tests / dissect-sysfile.c
index 8bab745f3ab23a52ca566b4e75b2c8001d194e30..ecb8d59b77ac11d9d5f415a2886adf23234e0fba 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -64,6 +64,7 @@ static void read_machine_integer_info (struct sfm_reader *,
                                        size_t size, size_t count);
 static void read_machine_float_info (struct sfm_reader *,
                                      size_t size, size_t count);
+static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
 static void read_display_parameters (struct sfm_reader *,
                                      size_t size, size_t count);
 static void read_long_var_name_map (struct sfm_reader *r,
@@ -74,10 +75,13 @@ static void read_datafile_attributes (struct sfm_reader *r,
                                       size_t size, size_t count);
 static void read_variable_attributes (struct sfm_reader *r,
                                       size_t size, size_t count);
+static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
 static void read_character_encoding (struct sfm_reader *r,
                                       size_t size, size_t count);
 static void read_long_string_value_labels (struct sfm_reader *r,
                                            size_t size, size_t count);
+static void read_unknown_extension (struct sfm_reader *,
+                                    size_t size, size_t count);
 static void read_compressed_data (struct sfm_reader *);
 
 static struct text_record *open_text_record (
@@ -87,6 +91,8 @@ static bool read_variable_to_value_pair (struct text_record *,
                                          char **key, char **value);
 static char *text_tokenize (struct text_record *, int delimiter);
 static bool text_match (struct text_record *text, int c);
+static const char *text_parse_counted_string (struct text_record *);
+static size_t text_pos (const struct text_record *);
 
 static void usage (int exit_code);
 static void sys_warn (struct sfm_reader *, const char *, ...)
@@ -97,6 +103,7 @@ static void sys_error (struct sfm_reader *, const char *, ...)
 
 static void read_bytes (struct sfm_reader *, void *, size_t);
 static int read_int (struct sfm_reader *);
+static int64_t read_int64 (struct sfm_reader *);
 static double read_float (struct sfm_reader *);
 static void read_string (struct sfm_reader *, char *, size_t);
 static void skip_bytes (struct sfm_reader *, size_t);
@@ -519,8 +526,9 @@ read_extension_record (struct sfm_reader *r)
       break;
 
     case 7:
-      /* Unknown purpose. */
-      break;
+    case 19:
+      read_mrsets (r, size, count);
+      return;
 
     case 11:
       read_display_parameters (r, size, count);
@@ -535,8 +543,8 @@ read_extension_record (struct sfm_reader *r)
       return;
 
     case 16:
-      /* New in SPSS v14?  Unknown purpose.  */
-      break;
+      read_ncases64 (r, size, count);
+      return;
 
     case 17:
       read_datafile_attributes (r, size, count);
@@ -556,7 +564,8 @@ read_extension_record (struct sfm_reader *r)
 
     default:
       sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
-      break;
+      read_unknown_extension (r, size, count);
+      return;
     }
 
   skip_bytes (r, bytes);
@@ -627,6 +636,107 @@ read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
               lowest, "LOWEST");
 }
 
+/* Read record type 7, subtype 7. */
+static void
+read_mrsets (struct sfm_reader *r, size_t size, size_t count)
+{
+  struct text_record *text;
+
+  printf ("%08lx: multiple response sets\n", ftell (r->file));
+  text = open_text_record (r, size * count);
+  for (;;)
+    {
+      const char *name;
+      enum { MRSET_MC, MRSET_MD } type;
+      bool cat_label_from_counted_values = false;
+      bool label_from_var_label = false;
+      const char *counted;
+      const char *label;
+      const char *variables;
+
+      name = text_tokenize (text, '=');
+      if (name == NULL)
+        break;
+
+      if (text_match (text, 'C'))
+        {
+          type = MRSET_MC;
+          counted = NULL;
+          if (!text_match (text, ' '))
+            {
+              sys_warn (r, "missing space following 'C' at offset %zu "
+                        "in mrsets record", text_pos (text));
+              break;
+            }
+        }
+      else if (text_match (text, 'D'))
+        {
+          type = MRSET_MD;
+        }
+      else if (text_match (text, 'E'))
+        {
+          char *number;
+
+          type = MRSET_MD;
+          cat_label_from_counted_values = true;
+
+          if (!text_match (text, ' '))
+            {
+              sys_warn (r, _("Missing space following 'E' at offset %zu "
+                             "in MRSETS record"), text_pos (text));
+              break;
+            }
+
+          number = text_tokenize (text, ' ');
+          if (!strcmp (number, "11"))
+            label_from_var_label = true;
+          else if (strcmp (number, "1"))
+            sys_warn (r, _("Unexpected label source value \"%s\" "
+                           "following 'E' at offset %zu in MRSETS record"),
+                      number, text_pos (text));
+
+        }
+      else
+        {
+          sys_warn (r, "missing 'C', 'D', or 'E' at offset %zu "
+                    "in mrsets record", text_pos (text));
+          break;
+        }
+
+      if (type == MRSET_MD)
+        {
+          counted = text_parse_counted_string (text);
+          if (counted == NULL)
+            break;
+        }
+
+      label = text_parse_counted_string (text);
+      if (label == NULL)
+        break;
+
+      variables = text_tokenize (text, '\n');
+      if (variables == NULL)
+        {
+          sys_warn (r, "missing variable names following label "
+                    "at offset %zu in mrsets record", text_pos (text));
+          break;
+        }
+
+      printf ("\t\"%s\": multiple %s set",
+              name, type == MRSET_MC ? "category" : "dichotomy");
+      if (counted != NULL)
+        printf (", counted value \"%s\"", counted);
+      if (cat_label_from_counted_values)
+        printf (", category labels from counted values");
+      if (label[0] != '\0')
+        printf (", label \"%s\"", label);
+      if (label_from_var_label)
+        printf (", label from variable label");
+      printf(", variables \"%s\"\n", variables);
+    }
+  close_text_record (text);
+}
+
 /* Read record type 7, subtype 11. */
 static void
 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
@@ -750,6 +860,31 @@ read_attributes (struct sfm_reader *r, struct text_record *text,
     }
 }
 
+/* Read extended number of cases record. */
+static void
+read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
+{
+  int64_t unknown, ncases64;
+
+  if (size != 8)
+    {
+      sys_warn (r, _("Bad size %zu for extended number of cases."), size);
+      skip_bytes (r, size * count);
+      return;
+    }
+  if (count != 2)
+    {
+      sys_warn (r, _("Bad count %zu for extended number of cases."), size);
+      skip_bytes (r, size * count);
+      return;
+    }
+  unknown = read_int64 (r);
+  ncases64 = read_int64 (r);
+  printf ("%08lx: extended number of cases: "
+          "unknown=%"PRId64", ncases64=%"PRId64"\n",
+          ftell (r->file), unknown, ncases64);
+}
+
 static void
 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count) 
 {
@@ -831,6 +966,80 @@ read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
     }
 }
 
+static void
+hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
+{
+  const uint8_t *buffer = buffer_;
+
+  while (buffer_size > 0)
+    {
+      size_t n = MIN (buffer_size, 16);
+      size_t i;
+
+      printf ("%04zx", offset);
+      for (i = 0; i < 16; i++)
+        {
+          if (i < n)
+            printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
+          else
+            printf ("   ");
+        }
+
+      printf (" |");
+      for (i = 0; i < 16; i++)
+        {
+          unsigned char c = i < n ? buffer[i] : ' ';
+          putchar (isprint (c) ? c : '.');
+        }
+      printf ("|\n");
+
+      offset += n;
+      buffer += n;
+      buffer_size -= n;
+    }
+}
+
+/* Reads and prints any type 7 record that we don't understand. */
+static void
+read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
+{
+  unsigned char *buffer;
+  size_t i;
+
+  if (size == 0 || count > 65536 / size)
+    skip_bytes (r, size * count);
+  else if (size != 1)
+    {
+      buffer = xmalloc (size);
+      for (i = 0; i < count; i++)
+        {
+          read_bytes (r, buffer, size);
+          hex_dump (i * size, buffer, size);
+        }
+      free (buffer);
+    }
+  else
+    {
+      buffer = xmalloc (count);
+      read_bytes (r, buffer, count);
+      if (memchr (buffer, 0, count) == 0)
+        for (i = 0; i < count; i++)
+          {
+            unsigned char c = buffer[i];
+
+            if (c == '\\')
+              printf ("\\\\");
+            else if (c == '\n' || isprint (c))
+              putchar (c);
+            else
+              printf ("\\%02x", c);
+          }
+      else
+        hex_dump (0, buffer, count);
+      free (buffer);
+    }
+}
+
 static void
 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count) 
 {
@@ -929,6 +1138,7 @@ read_compressed_data (struct sfm_reader *r)
 /* State. */
 struct text_record
   {
+    struct sfm_reader *reader;  /* Reader. */
     char *buffer;               /* Record contents. */
     size_t size;                /* Size of buffer. */
     size_t pos;                 /* Current position in buffer. */
@@ -942,6 +1152,8 @@ open_text_record (struct sfm_reader *r, size_t size)
   struct text_record *text = xmalloc (sizeof *text);
   char *buffer = xmalloc (size + 1);
   read_bytes (r, buffer, size);
+  buffer[size] = '\0';
+  text->reader = r;
   text->buffer = buffer;
   text->size = size;
   text->pos = 0;
@@ -984,6 +1196,54 @@ text_match (struct text_record *text, int c)
     return false;
 }
 
+/* Reads a integer value expressed in decimal, then a space, then a string that
+   consists of exactly as many bytes as specified by the integer, then a space,
+   from TEXT.  Returns the string, null-terminated, as a subset of TEXT's
+   buffer (so the caller should not free the string). */
+static const char *
+text_parse_counted_string (struct text_record *text)
+{
+  size_t start;
+  size_t n;
+  char *s;
+
+  start = text->pos;
+  n = 0;
+  while (isdigit ((unsigned char) text->buffer[text->pos]))
+    n = (n * 10) + (text->buffer[text->pos++] - '0');
+  if (start == text->pos)
+    {
+      sys_error (text->reader, "expecting digit at offset %zu in record",
+                 text->pos);
+      return NULL;
+    }
+
+  if (!text_match (text, ' '))
+    {
+      sys_error (text->reader, "expecting space at offset %zu in record",
+                 text->pos);
+      return NULL;
+    }
+
+  if (text->pos + n > text->size)
+    {
+      sys_error (text->reader, "%zu-byte string starting at offset %zu "
+                 "exceeds record length %zu", n, text->pos, text->size);
+      return NULL;
+    }
+
+  s = &text->buffer[text->pos];
+  if (s[n] != ' ')
+    {
+      sys_error (text->reader, "expecting space at offset %zu following "
+                 "%zu-byte string", text->pos + n, n);
+      return NULL;
+    }
+  s[n] = '\0';
+  text->pos += n + 1;
+  return s;
+}
+
 /* Reads a variable=value pair from TEXT.
    Looks up the variable in DICT and stores it into *VAR.
    Stores a null-terminated value into *VALUE. */
@@ -1002,6 +1262,13 @@ read_variable_to_value_pair (struct text_record *text,
     text->pos++;
   return true;
 }
+
+/* Returns the current byte offset inside the TEXT's string. */
+static size_t
+text_pos (const struct text_record *text)
+{
+  return text->pos;
+}
 \f
 static void
 usage (int exit_code)
@@ -1087,6 +1354,16 @@ read_int (struct sfm_reader *r)
   return integer_get (r->integer_format, integer, sizeof integer);
 }
 
+/* Reads a 64-bit signed integer from R and returns its value in
+   host format. */
+static int64_t
+read_int64 (struct sfm_reader *r)
+{
+  uint8_t integer[8];
+  read_bytes (r, integer, sizeof integer);
+  return integer_get (r->integer_format, integer, sizeof integer);
+}
+
 /* Reads a 64-bit floating-point number from R and returns its
    value in host format. */
 static double