start moving beyond PMModelItemInfo
[pspp] / dump.c
diff --git a/dump.c b/dump.c
index f6a8b49f4857ba9903dd4a835109a1637a1fff73..7f3c2b8c75174a045f9666ff379af99efaef5aa3 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -12,6 +12,7 @@
 #include <unistd.h>
 #include "u8-mbtouc.h"
 
+static const char *filename;
 static uint8_t *data;
 static size_t n;
 
@@ -248,24 +249,25 @@ get_end(void)
 }
 
 static void __attribute__((unused))
-hex_dump(int ofs, int n)
+hex_dump(FILE *stream, int ofs, int n)
 {
   for (int i = 0; i < n; i++)
     {
       int c = data[ofs + i];
 #if 1
       if (i && !(i % 16))
-        printf("-");
+        putc('-', stream);
       else
-        printf(" ");
+        putc(' ', stream);
 #endif
-      printf("%02x", c);
+      fprintf(stream, "%02x", c);
     }
   for (int i = 0; i < n; i++)
     {
       int c = data[ofs + i];
-      printf("%c", c >= 32 && c < 127 ? c : '.');
+      putc(c >= 32 && c < 127 ? c : '.', stream);
     }
+  putc('\n', stream);
 }
 
 static char *
@@ -506,6 +508,7 @@ dump_value(FILE *stream, int level)
   for (int i = 0; i <= level; i++)
     fprintf (stream, "    ");
 
+  printf ("%02x: value (%d)\n", pos, data[pos]);
   if (match_byte (1))
     {
       unsigned int format;
@@ -587,6 +590,7 @@ dump_value(FILE *stream, int level)
     }
   else
     {
+      printf ("else %#x\n", pos);
       dump_value_modifier(stream);
 
       char *base = get_string();
@@ -648,15 +652,9 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
   printf ("<category>\n");
   dump_value (stream, level + 1);
 
-  int merge = data[pos];
-  if (!match_byte(0))
-    match_byte_assert (1);
-
+  bool merge = get_bool();
   match_byte_assert (0);
-
-  int unindexed = data[pos];
-  if (!match_byte(0))
-    match_byte_assert (1);
+  int unindexed = get_bool();
 
   int x = get_u32 ();
   pos -= 4;
@@ -673,24 +671,14 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
             fprintf (stream, "    ");
           fprintf (stream, "<merge/>\n");
         }
+      assert (unindexed);
     }
   else
     {
-      if (merge)
-        {
-          fprintf(stderr, "index not -1 but merged\n");
-          exit(1);
-        }
-      if (x != 2)
-        {
-          fprintf(stderr, "index not -1 but x != 2\n");
-          exit(1);
-        }
-      if (n_categories != 0)
-        {
-          fprintf(stderr, "index not -1 but subcategories\n");
-          exit(1);
-        }
+      assert (!merge);
+      assert (!unindexed);
+      assert (x == 2);
+      assert (n_categories == 0);
       if (*n_indexes >= *allocated_indexes)
         {
           *allocated_indexes = *allocated_indexes ? 2 * *allocated_indexes : 16;
@@ -699,14 +687,6 @@ dump_category(FILE *stream, int level, int **indexes, int *allocated_indexes,
       (*indexes)[(*n_indexes)++] = indx;
     }
 
-  int expected_unindexed = indx == -1;
-  if (unindexed != expected_unindexed)
-    {
-      fprintf(stderr, "unindexed (%d) mismatch with indx (%d)\n",
-              unindexed, indx);
-      exit(1);
-    }
-
   if (n_categories == 0)
     {
       for (int i = 0; i <= level + 1; i++)
@@ -728,20 +708,29 @@ dump_dim(int indx)
   printf ("<dimension index=\"%d\">\n", indx);
   dump_value (stdout, 0);
 
-  /* This byte is usually 0 but many other values have been spotted. */
+  /* This byte is usually 0 but many other values have been spotted.
+     No visible effect. */
   pos++;
 
+  /* This byte can cause data to be oddly replicated. */
   if (!match_byte(0) && !match_byte(1))
     match_byte_assert(2);
+
   if (!match_u32(0))
     match_u32_assert(2);
-  if (!match_byte(0))
-    match_byte_assert(1);
-  if (!match_byte(0))
-    match_byte_assert(1);
+
+  bool show_dim_label = get_bool();
+  if (show_dim_label)
+    printf("  <show-dim-label/>\n");
+
+  bool hide_all_labels = get_bool();
+  if (hide_all_labels)
+    printf("  <hide-all-labels/>\n");
+
   match_byte_assert(1);
   if (!match_u32(UINT32_MAX))
     match_u32_assert(indx);
+
   n_categories = get_u32();
 
   int *indexes = NULL;
@@ -862,7 +851,19 @@ dump_title(void)
         dump_value(stdout, 0);
       else
         match_byte_assert (0x58);
-      get_u32 ();
+      int n = get_u32();
+      if (n >= 0)
+        {
+          /* Appears to be the number of references to a footnote. */
+          printf ("  <references n=\"%d\"/>\n", n);
+        }
+      else if (n == -2)
+        {
+          /* The user deleted the footnote references. */
+          printf ("  <deleted/>\n");
+        }
+      else
+        assert(0);
       printf ("</footnote>\n");
     }
 }
@@ -962,18 +963,21 @@ dump_fonts(void)
       match_be32_assert(1);
       get_be32();
       printf("<settings layer=\"%d\"", get_be32());
-      if (!get_byte())
+      if (!get_bool())
         printf(" skipempty=\"false\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" showdimensionincorner=\"false\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" markers=\"numeric\"");
-      if (!get_byte())
+      if (!get_bool())
         printf(" footnoteposition=\"subscript\"");
       get_byte();
       int nbytes = get_be32();
+      int end = pos + nbytes;
       printf("\n");
-      hex_dump(pos, nbytes);
+      while (pos + 4 <= end)
+        printf(" %d", get_be32());
+      pos = end;
       printf("\n");
       pos += nbytes;
       char *notes = get_string_be();
@@ -1003,10 +1007,11 @@ dump_fonts(void)
   const char *locale = get_string();
   printf ("<locale>%s</locale>\n", locale);
 
-  get_u32();            /* Seen: 0, UINT32_MAX, 2, 3, 4, 5, 6, 8, 9, 21, 24. */
+  printf ("<layer>%d</layer>\n", get_u32());
+  if (!match_byte(0))
+    match_byte_assert(1);
   if (!match_byte(0))
     match_byte_assert(1);
-  match_byte_assert(0);
   if (!match_byte(0))
     match_byte_assert(1);
   printf("<epoch>%d</epoch>\n", get_u32());
@@ -1024,9 +1029,9 @@ dump_fonts(void)
       if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
         match_byte_assert(0);
     }
-  printf("<format decimal=\"%c\" grouping=\"", decimal);
+  printf("<format decimal=\"%c\"", decimal);
   if (grouping)
-    putchar(grouping);
+    printf(" grouping=\"%c\"", grouping);
   printf("\"/>\n");
   if (match_u32(5))
     {
@@ -1039,22 +1044,22 @@ dump_fonts(void)
   /* The last chunk is an outer envelope that contains two inner envelopes.
      The second inner envelope has some interesting data like the encoding and
      the locale. */
+  int outer_end = get_end();
   if (version == 3)
     {
-      int outer_end = get_end();
-
       /* First inner envelope: byte*33 int[n] int*[n]. */
       int inner_len = get_u32();
       int inner_end = pos + inner_len;
       int array_start = pos + 33;
       match_byte_assert(0);
       pos++;                    /* 0, 1, 10 seen. */
-      match_byte_assert(0);
-      pos++;                    /* 0...11 seen. */
-      if (!match_byte(0) && !match_byte(1) && !match_byte(2))
-        match_byte_assert(3);
-      if (!match_byte(0) && !match_byte(2))
-        match_byte_assert(3);
+      get_bool();
+
+      /* 0=en 1=de 2=es 3=it 5=ko 6=pl 8=zh-tw 10=pt_BR 11=fr */
+      printf("lang=%d ", get_byte());
+
+      printf ("variable_mode=%d\n", get_byte());
+      printf ("value_mode=%d\n", get_byte());
       if (!match_u64(0))
         match_u64_assert(UINT64_MAX);
       match_u32_assert(0);
@@ -1062,17 +1067,40 @@ dump_fonts(void)
       match_u32_assert(0);
       match_u32_assert(0);
       match_byte_assert(0);
-      if (!match_byte(0))
-        match_byte_assert(1);
+      get_bool();
       match_byte_assert(1);
       pos = array_start;
-#if 1
-      printf("widths:");
-      while (pos < inner_end)
-        printf(" %d", get_u32());
-      printf("\n");
-#endif
-      pos = inner_end;;
+
+      assert(get_end() == inner_end);
+      printf("<heights>");
+      int n_heights = get_u32();
+      for (int i = 0; i < n_heights; i++)
+        {
+          if (i)
+            putchar(' ');
+          printf("%d", get_u32());
+        }
+      printf("</heights>\n");
+
+      int n_style_map = get_u32();
+      for (int i = 0; i < n_style_map; i++)
+        {
+          uint64_t cell = get_u64();
+          int style = get_u16();
+          printf("<style-map cell=\"%llu\" style=\"%d\"/>\n", cell, style);
+        }
+
+      int n_styles = get_u32();
+      for (int i = 0; i < n_styles; i++)
+        {
+          printf("<cell-style index=\"%d\"", i);
+          dump_style(stdout);
+          dump_style2(stdout);
+          printf("/>\n");
+        }
+
+      pos = get_end();
+      assert(pos == inner_end);
 
       /* Second inner envelope. */
       assert(get_end() == outer_end);
@@ -1086,18 +1114,15 @@ dump_fonts(void)
       match_byte_assert(0);
 
       printf("<command>%s</command>\n", get_string());
-      printf("<subcommand>%s</subcommand>\n", get_string());
+      printf("<command-local>%s</command-local>\n", get_string());
       printf("<language>%s</language>\n", get_string());
       printf("<charset>%s</charset>\n", get_string());
       printf("<locale>%s</locale>\n", get_string());
 
-      if (!match_byte(0))
-        match_byte_assert(1);
-      match_byte_assert(0);
-      if (!match_byte(0))
-        match_byte_assert(1);
-      if (!match_byte(0))
-        match_byte_assert(1);
+      get_bool();
+      get_bool();
+      get_bool();
+      get_bool();
 
       printf("<epoch2>%d</epoch2>\n", get_u32());
 
@@ -1155,21 +1180,61 @@ dump_fonts(void)
         match_u32_assert(0);
 
       match_byte_assert('.');
-      if (!match_byte(0))
-        match_byte_assert(1);
+      get_bool();
 
       if (pos < outer_end)
         {
-          printf("<seed>%d</seed>\n", get_u32());
+          get_u32();
           match_u32_assert(0);
         }
       assert(pos == outer_end);
 
       pos = outer_end;
     }
-  else
+  else if (outer_end != pos)
     {
-      pos = get_end();
+      pos += 14;
+      printf("<command>%s</command>\n", get_string());
+      printf("<command-local>%s</command-local>\n", get_string());
+      printf("<language>%s</command>\n", get_string());
+      printf("<charset>%s</charset>\n", get_string());
+      printf("<locale>%s</locale>\n", get_string());
+      get_bool();
+      match_byte_assert(0);
+      get_bool();
+      get_bool();
+
+      printf("<epoch2>%d</epoch2>\n", get_u32());
+      int decimal = data[pos];
+      int grouping = data[pos + 1];
+      if (match_byte('.'))
+        {
+          if (!match_byte(',') && !match_byte('\''))
+            match_byte_assert(' ');
+        }
+      else
+        {
+          match_byte_assert(',');
+          if (!match_byte('.') && !match_byte(' ') && !match_byte(','))
+            match_byte_assert(0);
+        }
+      printf("<format decimal=\"%c\"", decimal);
+      if (grouping)
+        printf(" grouping=\"%c\"", grouping);
+      printf("\"/>\n");
+      if (match_u32(5))
+        {
+          for (int i = 0; i < 5; i++)
+            printf("<CC%c>%s</CC%c>\n", 'A' + i, get_string(), 'A' + i);
+        }
+      else
+        match_u32_assert(0);
+
+      match_byte_assert('.');
+      get_bool();
+
+      assert(pos == outer_end);
+      pos = outer_end;
     }
 }
 
@@ -1182,10 +1247,11 @@ main(int argc, char *argv[])
       exit (1);
     }
 
-  int fd = open(argv[1], O_RDONLY);
+  filename = argv[1];
+  int fd = open(filename, O_RDONLY);
   if (fd < 0)
     {
-      fprintf (stderr, "%s: open failed (%s)", argv[1], strerror (errno));
+      fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
       exit (1);
     }
 
@@ -1217,8 +1283,16 @@ main(int argc, char *argv[])
   assert(version == 1 || version == 3);
 
   match_byte_assert(1);
-  for (int i = 0; i < 4; i++)
-    get_bool();
+  bool number_footnotes = get_bool();
+  printf("<footnote markers=\"%s\"/>\n",
+         number_footnotes ? "number" : "letter");
+  bool rotate_inner_column_labels = get_bool();
+  bool rotate_outer_row_labels = get_bool();
+  printf("x=%d\n", get_bool());
+  printf("<rotate-labels inner-column=\"%s\" outer-row=\"%s\"/>",
+         rotate_inner_column_labels ? "yes" : "no",
+         rotate_outer_row_labels ? "yes" : "no");
+  //fprintf(stderr, "option-number=%d\n", get_u32());
   get_u32();
 
   int min_col_width = get_u32();