Factor out nested bytes.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 7 Nov 2014 17:41:30 +0000 (09:41 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 7 Nov 2014 17:41:30 +0000 (09:41 -0800)
dump.c

diff --git a/dump.c b/dump.c
index f478131555e2ef126837096f65f4a292b2d02741..f1f212bd3d43ee3a76b615ff17ebc37ad31f7632 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -124,6 +124,66 @@ match_byte_assert(uint8_t b, const char *where)
 }
 #define match_byte_assert(b) match_byte_assert(b, WHERE)
 
+static void
+dump_raw(FILE *stream, int start, int end, const char *separator)
+{
+  for (size_t i = start; i < end; )
+    {
+      if (i + 5 <= n
+          && data[i]
+          //&& !data[i + 1]
+          && !data[i + 2]
+          && !data[i + 3]
+          && i + 4 + data[i] + data[i + 1] * 256 <= end
+          && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
+        {
+          fprintf(stream, "%s\"", separator);
+          fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
+          fputs("\" ", stream);
+
+          i += 4 + data[i] + data[i + 1] * 256;
+        }
+      else if (i + 12 <= end
+               && data[i + 1] == 40
+               && data[i + 2] == 5
+               && data[i + 3] == 0)
+        {
+          double d;
+
+          memcpy (&d, &data[i + 4], 8);
+          fprintf (stream, "F40.%d(%.*f)%s", data[i], data[i], d, separator);
+          i += 12;
+        }
+      else if (i + 12 <= end
+               && data[i + 1] == 40
+               && data[i + 2] == 31
+               && data[i + 3] == 0)
+        {
+          double d;
+
+          memcpy (&d, &data[i + 4], 8);
+          fprintf (stream, "PCT40.%d(%.*f)%s", data[i], data[i], d, separator);
+          i += 12;
+        }
+      else if (i + 4 <= end
+               && (data[i] && data[i] != 88 && data[i] != 0x41)
+               && !data[i + 1]
+               && !data[i + 2]
+               && !data[i + 3])
+        {
+          fprintf (stream, "i%d ", data[i]);
+          i += 4;
+        }
+      else
+        {
+          fprintf(stream, "%02x ", data[i]);
+          i++;
+        }
+    }
+
+
+}
+
 static char *
 get_string(const char *where)
 {
@@ -147,6 +207,16 @@ get_string(const char *where)
 }
 #define get_string() get_string(WHERE)
 
+static void
+dump_nested(void)
+{
+  int subn = get_u32 ();
+  fprintf (stderr, "nested %d bytes: ", subn);
+  dump_raw(stderr, pos, pos + subn, "");
+  putc('\n', stderr);
+  pos += subn;
+}
+
 static void
 dump_value_31(void)
 {
@@ -158,18 +228,14 @@ dump_value_31(void)
             get_string();
           else
             match_u32_assert (0);
-          int subn = get_u32 ();
-          printf ("nested %d bytes", subn);
-          pos += subn;
+          dump_nested();
         }
       else if (match_u32 (1))
         {
           printf("(footnote %d) ", get_u32());
           match_byte_assert (0);
           match_byte_assert (0);
-          int subn = get_u32 ();
-          printf ("nested %d bytes", subn);
-          pos += subn;
+          dump_nested();
         }
       else if (match_u32 (2))
         {
@@ -180,9 +246,7 @@ dump_value_31(void)
             match_u32_assert(1);
           match_byte_assert(0);
           match_byte_assert(0);
-          int subn = get_u32 ();
-          printf ("nested %d bytes", subn);
-          pos += subn;
+          dump_nested();
         }
       else
         {
@@ -192,12 +256,8 @@ dump_value_31(void)
           match_byte_assert(0);
           match_byte_assert(1);
           match_byte_assert(0);
-          int subn = get_u32 ();
-          printf ("nested %d bytes, ", subn);
-          pos += subn;
-          subn = get_u32 ();
-          printf ("nested %d bytes, ", subn);
-          pos += subn;
+          dump_nested();
+          dump_nested();
         }
     }
   else
@@ -686,59 +746,7 @@ main(int argc, char *argv[])
   else
     start = 0x27;
 
-  for (size_t i = start; i < n; )
-    {
-      if (i + 5 <= n
-          && data[i]
-          //&& !data[i + 1]
-          && !data[i + 2]
-          && !data[i + 3]
-          && i + 4 + data[i] + data[i + 1] * 256 <= n
-          && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
-        {
-          fputs("\n\"", stdout);
-          fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stdout);
-          fputs("\" ", stdout);
-
-          i += 4 + data[i] + data[i + 1] * 256;
-        }
-      else if (i + 12 <= n
-               && data[i + 1] == 40
-               && data[i + 2] == 5
-               && data[i + 3] == 0)
-        {
-          double d;
-
-          memcpy (&d, &data[i + 4], 8);
-          printf ("F40.%d(%.*f)\n", data[i], data[i], d);
-          i += 12;
-        }
-      else if (i + 12 <= n
-               && data[i + 1] == 40
-               && data[i + 2] == 31
-               && data[i + 3] == 0)
-        {
-          double d;
-
-          memcpy (&d, &data[i + 4], 8);
-          printf ("PCT40.%d(%.*f)\n", data[i], data[i], d);
-          i += 12;
-        }
-      else if (i + 4 <= n
-               && (data[i] && data[i] != 88 && data[i] != 0x41)
-               && !data[i + 1]
-               && !data[i + 2]
-               && !data[i + 3])
-        {
-          printf ("i%d ", data[i]);
-          i += 4;
-        }
-      else
-        {
-          printf("%02x ", data[i]);
-          i++;
-        }
-    }
+  dump_raw(stdout, start, n, "\n");
 
   return 0;
 }