Browse Source

Honor HIPACK_WRITER_COMPACT, test compact encoding

Adrian Perez de Castro 5 years ago
parent
commit
fb76ae7e67
4 changed files with 73 additions and 16 deletions
  1. 3 1
      hipack-writer.c
  2. 33 6
      tools/hipack-cat.c
  3. 33 6
      tools/hipack-roundtrip.c
  4. 4 3
      tools/run-tests

+ 3 - 1
hipack-writer.c

@@ -332,7 +332,9 @@ hipack_write (hipack_writer_t     *writer,
 {
     assert (writer);
     assert (message);
-    writer->indent = 0;
+    if (writer->indent != HIPACK_WRITER_COMPACT) {
+        writer->indent = HIPACK_WRITER_INDENTED;
+    }
     return write_keyval (writer, message);
 }
 

+ 33 - 6
tools/hipack-cat.c

@@ -5,23 +5,49 @@
  * Distributed under terms of the MIT license.
  */
 
+#define _POSIX_C_SOURCE 2
 #include "../hipack.h"
 #include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 
 
+static void
+usage (const char *argv0, int code)
+{
+    FILE *output = (code == EXIT_FAILURE) ? stderr : stdout;
+    fprintf (output, "Usage: %s [-c] PATH\n", argv0);
+    exit (code);
+}
+
+
 int
-main (int argc, const char *argv[])
+main (int argc, char *argv[])
 {
-    if (argc != 2) {
-        fprintf (stderr, "Usage: %s PATH\n", argv[0]);
-        return EXIT_FAILURE;
+    bool compact = false;
+    int opt;
+
+    while ((opt = getopt (argc, argv, "hc")) != -1) {
+        switch (opt) {
+            case 'c':
+                compact = true;
+                break;
+            case 'h':
+                usage (argv[0], EXIT_SUCCESS);
+                break;
+            default:
+                usage (argv[0], EXIT_FAILURE);
+        }
+    }
+
+    if (optind >= argc) {
+        usage (argv[0], EXIT_FAILURE);
     }
 
-    FILE *fp = fopen (argv[1], "rb");
+    FILE *fp = fopen (argv[optind], "rb");
     if (!fp) {
         fprintf (stderr, "%s: Cannot open '%s' (%s)\n",
-                 argv[0], argv[1], strerror (errno));
+                 argv[0], argv[optind], strerror (errno));
         return EXIT_FAILURE;
     }
 
@@ -44,6 +70,7 @@ main (int argc, const char *argv[])
     hipack_writer_t writer = {
         .putchar = hipack_stdio_putchar,
         .putchar_data = stdout,
+        .indent = compact ? HIPACK_WRITER_COMPACT : HIPACK_WRITER_INDENTED,
     };
     hipack_write (&writer, message);
 

+ 33 - 6
tools/hipack-roundtrip.c

@@ -5,23 +5,49 @@
  * Distributed under terms of the MIT license.
  */
 
+#define _POSIX_C_SOURCE 2
 #include "../hipack.h"
 #include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 
 
+static void
+usage (const char *argv0, int code)
+{
+    FILE *output = (code == EXIT_FAILURE) ? stderr : stdout;
+    fprintf (output, "Usage: %s [-c] PATH\n", argv0);
+    exit (code);
+}
+
+
 int
-main (int argc, const char *argv[])
+main (int argc, char *argv[])
 {
-    if (argc != 2) {
-        fprintf (stderr, "Usage: %s PATH\n", argv[0]);
-        return EXIT_FAILURE;
+    bool compact = false;
+    int opt;
+
+    while ((opt = getopt (argc, argv, "hc")) != -1) {
+        switch (opt) {
+            case 'c':
+                compact = true;
+                break;
+            case 'h':
+                usage (argv[0], EXIT_SUCCESS);
+                break;
+            default:
+                usage (argv[0], EXIT_FAILURE);
+        }
+    }
+
+    if (optind >= argc) {
+        usage (argv[0], EXIT_FAILURE);
     }
 
-    FILE *fp = fopen (argv[1], "rb");
+    FILE *fp = fopen (argv[optind], "rb");
     if (!fp) {
         fprintf (stderr, "%s: Cannot open '%s' (%s)\n",
-                 argv[0], argv[1], strerror (errno));
+                 argv[0], argv[optind], strerror (errno));
         return EXIT_FAILURE;
     }
 
@@ -49,6 +75,7 @@ main (int argc, const char *argv[])
     hipack_writer_t writer = {
         .putchar = hipack_stdio_putchar,
         .putchar_data = fp,
+        .indent = compact ? HIPACK_WRITER_COMPACT : HIPACK_WRITER_INDENTED,
     };
 
     if (hipack_write (&writer, message1)) {

+ 4 - 3
tools/run-tests

@@ -51,13 +51,14 @@ for name in "${tests[@]}" ; do
 
 	if [[ ${name} = roundtrip/* ]] ; then
 		if "${srcdir}/tools/hipack-roundtrip" \
-		   "${srcdir}/test/${name#roundtrip/}.hi" \
-		   &> "/tmp/hipack-$$-${name//\//-}.err"
+		   "${srcdir}/test/${name#roundtrip/}.hi" && \
+		   "${srcdir}/tools/hipack-roundtrip" -c \
+		   "${srcdir}/test/${name#roundtrip/}.hi"
 		then
 			passed=true
 		else
 			passed=false
-		fi
+		fi &> "/tmp/hipack-$$-${name//\//-}.err"
 	else
 		if "${srcdir}/tools/hipack-parse" \
 		   "${srcdir}/test/${name}.hi" \