dump: Crazy simplification.
[pspp] / dump.c
diff --git a/dump.c b/dump.c
index ecf278075c06652d0cf526e834e91265d27ebadf..a7bd6c2c14dcbde968f2a05dd4a54bfc9d6aa5a4 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -204,16 +204,26 @@ dump_value_31(void)
     match_byte_assert (0x58);
 }
 
+static void dump_substs(void (*dump)(int level), int level);
+static void dump_value__(int level, bool match1);
+
 static void
-dump_value(int level)
+dump_value__with_preskip(int level)
+{
+  match_byte(0);
+  match_byte(0);
+  match_byte(0);
+  match_byte(0);
+  dump_value__ (level, false);
+  putchar('\n');
+}
+
+static void
+dump_value__(int level, bool match1)
 {
   for (int i = 0; i <= level; i++)
     printf ("    ");
 
-  match_byte (0);
-  match_byte (0);
-  match_byte (0);
-  match_byte (0);
   if (match_byte (3))
     {
       char *s1 = get_string();
@@ -224,9 +234,10 @@ dump_value(int level)
         printf("strings \"%s\", \"%s\" and \"%s\"", s1, s2, s3);
       else
         printf("string \"%s\" and \"%s\"", s1, s2);
-      match_byte (0);
-      match_byte (1);
-      match_byte (1);
+      if (!match_byte (0))
+        match_byte_assert(1);
+      if (match1)
+        match_byte (1);
     }
   else if (match_byte (5))
     {
@@ -276,87 +287,11 @@ dump_value(int level)
       format = get_u32 ();
       value = get_double ();
       printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
-      match_byte (1);
-    }
-  else
-    {
-      dump_value_31();
-      char *base = get_string();
-
-      int x = get_u32();
-      printf ("\"%s\" with %d variables:\n", base, x);
-      if (match_u32(0))
-        {
-          for (int i = 0; i < x; i++)
-            {
-              dump_value (level+1);
-              putchar('\n');
-            }
-        }
-      else
-        {
-          for (int i = 0; i < x; i++)
-            {
-              int y = get_u32();
-              match_u32_assert(0);
-              for (int j = 0; j <= level; j++)
-                printf ("    ");
-              printf("variable %d has %d values:\n", i, y);
-              for (int j = 0; j < y; j++)
-                {
-                  match_byte(0);
-                  if (match_byte(3))
-                    {
-                      char *a = get_string();
-                      match_byte_assert(0x58);
-                      char *b = get_string();
-                      char *c = get_string();
-                      for (int k = 0; k <= level + 1; k++)
-                        printf ("    ");
-                      printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
-                    }
-                  else
-                    dump_value (level+1);
-
-                  match_byte(0);
-                  match_byte(0);
-                  match_byte(0);
-                  match_byte(0);
-                  putchar('\n');
-                }
-            }
-        }
-    }
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
-}
-
-static void
-dump_dim_value(int level)
-{
-  for (int i = 0; i <= level; i++)
-    printf ("    ");
-
-  if (match_byte (3))
-    {
-      get_string();
-      dump_value_31();
-      get_string();
-      printf("string \"%s\"", get_string());
-      match_byte (0);
-      match_byte (1);
-    }
-  else if (match_byte (5))
-    {
-      match_byte_assert (0x58);
-      printf ("variable \"%s\"", get_string());
-      get_string();
-      if (!match_byte (1) && !match_byte (2))
-        match_byte_assert(3);
+      if (match1)
+        match_byte (1);
     }
   else
-    dump_value(level);
+    dump_substs(dump_value__with_preskip, level + 1);
 }
 
 static void
@@ -366,30 +301,24 @@ dump_category(int level)
   match_byte (0);
   match_byte (0);
   match_byte (0);
-  dump_value (level);
+  dump_value__ (level, true);
+  match_byte(0);
+  match_byte(0);
+  match_byte(0);
 
-  if (match_u32 (2))
-    get_u32 ();
-  else if (match_u32 (1))
-    {
-      match_byte (0);
-      match_byte (0);
-      match_byte (0);
-      get_u32 ();
-    }
+  if (match_u32 (1))
+    match_byte (0);
   else if (match_byte (1))
     {
       match_byte (0);
       if (!match_u32 (2))
         match_u32_assert (1);
       match_byte (0);
-      get_u32();
-    }
-  else
-    {
-      match_u32_assert (0);
-      get_u32 ();
     }
+  else if (!match_u32(2))
+    match_u32_assert (0);
+
+  get_u32 ();
 
   int n_categories = get_u32();
   if (n_categories > 0)
@@ -399,13 +328,14 @@ dump_category(int level)
     dump_category (level + 1);
 }
 
+static void dump_title_value(int level);
+
 static void
 dump_dim(void)
 {
   int n_categories;
   printf("next dim\n");
-  match_byte(0);
-  dump_dim_value(0);
+  dump_title_value(0);
 
   /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
   pos++;
@@ -416,15 +346,9 @@ dump_dim(void)
     match_u32_assert(2);
   if (!match_byte(0))
     match_byte_assert(1);
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
   get_u32();
   match_byte(0);
   match_byte(0);
-  match_byte(0);
-  match_byte(0);
   n_categories = get_u32();
   printf("%d nested categories\n", n_categories);
   for (int i = 0; i < n_categories; i++)
@@ -445,83 +369,45 @@ dump_dims(void)
 }
 
 static void
-dump_data_value(void)
+dump_substs(void (*dump)(int level), int level)
 {
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
-  match_byte(0);
-  if (match_byte (1))
-    {
-      unsigned int format;
-      double value;
+  dump_value_31();
 
-      dump_value_31();
-      format = get_u32 ();
-      value = get_double ();
-      printf ("    value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
-    }
-  else if (match_byte (3))
+  char *base = get_string();
+  int x = get_u32();
+  printf ("\"%s\" with %d variables:\n", base, x);
+  for (int i = 0; i < x; i++)
     {
-      get_string();
-      dump_value_31();
-      get_string();
-      printf("string \"%s\"", get_string());
-      match_byte (0);
+      int y = get_u32();
+      if (!y)
+        y = 1;
+      else
+        match_u32_assert(0);
+      for (int j = 0; j <= level; j++)
+        printf ("    ");
+      printf("variable %d has %d values:\n", i, y);
+      for (int j = 0; j < y; j++)
+        {
+          dump (level+1);
+          putchar('\n');
+        }
     }
-  else if (match_byte (2))
-    {
-      unsigned int format;
-      char *var, *vallab;
-      double value;
+}
 
-      match_byte_assert (0x58);
-      format = get_u32 ();
-      value = get_double ();
-      var = get_string ();
-      vallab = get_string ();
-      printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
-              value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
-      if (!match_byte (1) && !match_byte(2))
-        match_byte_assert (3);
-    }
-  else if (match_byte (4))
-    {
-      unsigned int format;
-      char *var, *vallab, *value;
+static void
+dump_data_value(int level)
+{
+  for (int i = 0; i <= level; i++)
+    printf ("    ");
 
-      match_byte_assert (0x58);
-      format = get_u32 ();
-      vallab = get_string ();
-      var = get_string ();
-      if (!match_byte(1) && !match_byte(2))
-        match_byte_assert (3);
-      value = get_string ();
-      printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
-              value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
-    }
-  else if (data[pos] == 5)
-    dump_value (0);
+  match_byte(0);
+  match_byte(0);
+  match_byte(0);
+  match_byte(0);
+  if (data[pos] <= 5)
+    dump_value__(0, false);
   else
-    {
-      dump_value_31();
-      char *base = get_string();
-      int x = get_u32();
-      printf ("\"%s\"; %d variables:\n", base, x);
-      for (int i = 0; i < x; i++)
-        {
-          int y = get_u32();
-          if (!y)
-            y = 1;
-          else
-            match_u32_assert(0);
-          for (int j = 0; j <= 0; j++)
-            printf ("    ");
-          printf("variable %d has %d values:\n", i, y);
-          for (int j = 0; j < y; j++)
-            dump_data_value();
-        }
-    }
+    dump_substs (dump_data_value, level + 1);
 }
 
 static void
@@ -547,7 +433,7 @@ dump_data(void)
     {
       printf("%08x, index %d:\n", pos, get_u32());
       match_u32_assert(0);
-      dump_data_value();
+      dump_data_value(0);
       putchar('\n');
     }
 }
@@ -562,151 +448,10 @@ dump_title_value(int level)
   match_byte (0);
   match_byte (0);
   match_byte (0);
-  if (data[pos] == 3 || data[pos] == 2 || data[pos] == 4 || data[pos] == 1)
-    dump_value(level);
-  else if (match_byte (5))
-    {
-      dump_value_31();
-      printf ("variable \"%s\"", get_string());
-      get_string();
-      if (!match_byte(1) && !match_byte(2))
-        match_byte_assert(3);
-    }
+  if (data[pos] <= 5)
+    dump_value__(level, true);
   else
-    {
-      dump_value_31();
-
-      char *base = get_string();
-      int x = get_u32();
-      printf ("\"%s\" with %d variables:\n", base, x);
-      for (int i = 0; i < x; i++)
-        {
-          int y = get_u32();
-          if (!y)
-            y = 1;
-          else
-            match_u32_assert(0);
-          for (int j = 0; j <= level; j++)
-            printf ("    ");
-          printf("variable %d has %d values:\n", i, y);
-          for (int j = 0; j < y; j++)
-            {
-              match_byte(0);
-              if (match_byte(3))
-                {
-                  char *a = get_string();
-                  match_byte_assert(0x58);
-                  char *b = get_string();
-                  char *c = get_string();
-                  for (int k = 0; k <= level + 1; k++)
-                    printf ("    ");
-                  printf ("\"%s\", \"%s\", \"%s\"", a, b, c);
-                }
-              else
-                dump_title_value (level+1);
-              putchar('\n');
-            }
-        }
-    }
-}
-
-static void
-dump_footnote_value(int level)
-{
-  for (int i = 0; i <= level; i++)
-    printf ("    ");
-
-  match_byte (0);
-  match_byte (0);
-  match_byte (0);
-  match_byte (0);
-  if (match_byte (3))
-    {
-      get_string();
-      dump_value_31();
-      get_string();
-      printf("string \"%s\"", get_string());
-      if (!match_byte (0))
-        match_byte_assert (1);
-    }
-  else if (match_byte (5))
-    {
-      match_byte_assert (0x58);
-      printf ("variable \"%s\"", get_string());
-      get_string();
-      if (!match_byte(1) && !match_byte(2))
-        match_byte_assert(3);
-    }
-  else if (match_byte (2))
-    {
-      unsigned int format;
-      char *var, *vallab;
-      double value;
-
-      match_byte_assert (0x58);
-      format = get_u32 ();
-      value = get_double ();
-      var = get_string ();
-      vallab = get_string ();
-      printf ("value %g format %d(%d.%d) var \"%s\" vallab \"%s\"",
-              value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
-      if (!match_byte (1) && !match_byte(2))
-        match_byte_assert (3);
-      match_byte (0);
-      match_byte (0);
-      match_byte (0);
-    }
-  else if (match_byte (4))
-    {
-      unsigned int format;
-      char *var, *vallab, *value;
-
-      match_byte_assert (0x58);
-      format = get_u32 ();
-      vallab = get_string ();
-      var = get_string ();
-      if (!match_byte(1) && !match_byte(2))
-        match_byte_assert (3);
-      value = get_string ();
-      printf ("value \"%s\" format %d(%d.%d) var \"%s\" vallab \"%s\"",
-              value, format >> 16, (format >> 8) & 0xff, format & 0xff, var, vallab);
-      match_byte (0);
-      match_byte (0);
-      match_byte (0);
-    }
-  else if (match_byte (1))
-    {
-      unsigned int format;
-      double value;
-
-      dump_value_31();
-      format = get_u32 ();
-      value = get_double ();
-      printf ("value %g format %d(%d.%d)", value, format >> 16, (format >> 8) & 0xff, format & 0xff);
-    }
-  else
-    {
-      dump_value_31();
-      char *base = get_string();
-      int x = get_u32();
-      printf ("\"%s\"; %d variables:\n", base, x);
-      for (int i = 0; i < x; i++)
-        {
-          int y = get_u32();
-          if (!y)
-            y = 1;
-          else
-            match_u32_assert(0);
-          for (int j = 0; j <= level; j++)
-            printf ("    ");
-          printf("variable %d has %d values:\n", i, y);
-          for (int j = 0; j < y; j++)
-            {
-              dump_footnote_value (level+1);
-              putchar('\n');
-            }
-        }
-    }
+    dump_substs(dump_title_value, level + 1);
 }
 
 static void
@@ -721,7 +466,7 @@ dump_title(void)
   match_byte_assert(0x58);
   if (match_byte(0x31))
     {
-      dump_footnote_value(0); putchar('\n');
+      dump_data_value(0); putchar('\n');
     }
   else
     match_byte_assert(0x58);
@@ -740,11 +485,7 @@ dump_title(void)
       for (int i = 0; i < n_footnotes; i++)
         {
           printf("footnote %d:\n", i);
-          dump_footnote_value(0);
-          match_byte(0);
-          match_byte(0);
-          match_byte(0);
-          match_byte(0);
+          dump_data_value(0);
           if (match_byte (0x31))
             {
               /* Custom footnote marker string. */
@@ -941,8 +682,7 @@ main(int argc, char *argv[])
           dump_dims ();
           printf("\n\ndata:\n");
           dump_data ();
-          if (pos == n - 1)
-            match_byte_assert (1);
+          match_byte (1);
           if (pos != n)
             {
               fprintf (stderr, "%x / %x\n", pos, n);