]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
* testsuite/sexp-conv-test: Updated testcases for improved
authorNiels Möller <nisse@lysator.liu.se>
Tue, 10 Feb 2009 18:53:51 +0000 (19:53 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Tue, 10 Feb 2009 18:53:51 +0000 (19:53 +0100)
handling of comments.

* tools/sexp-conv.c (sexp_convert_item): Use sexp_put_soft_newline
to terminate comments, and modify indentation for the case that a
list starts with a comment.

* tools/output.c (sexp_output_init): Initialize soft_newline.
(sexp_put_raw_char): Clear soft_newline.
(sexp_put_newline): Check and reset soft_newline.
(sexp_put_soft_newline): New function.

* tools/output.h (struct sexp_output): Removed union with single
element, and updated all users. New attribute soft_newline.

Rev: nettle/ChangeLog:1.31
Rev: nettle/testsuite/sexp-conv-test:1.3
Rev: nettle/tools/output.c:1.3
Rev: nettle/tools/output.h:1.3
Rev: nettle/tools/sexp-conv.c:1.4

ChangeLog
testsuite/sexp-conv-test
tools/output.c
tools/output.h
tools/sexp-conv.c

index 6051a2239ecf79f876fd68a69c3610220551f1ad..c4ae037cd27f798aaa02c78d06d66ae0ac75553a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2009-02-10  Niels Möller  <nisse@lysator.liu.se>
+
+       * base16-meta.c (base16_encode_update_wrapper): Mark ctx argument
+       as UNUSED.
+
+       * testsuite/sexp-conv-test: Updated testcases for improved
+       handling of comments.
+
+       * tools/sexp-conv.c (sexp_convert_item): Use sexp_put_soft_newline
+       to terminate comments, and modify indentation for the case that a
+       list starts with a comment.
+
+       * tools/output.c (sexp_output_init): Initialize soft_newline.
+       (sexp_put_raw_char): Clear soft_newline.
+       (sexp_put_newline): Check and reset soft_newline.
+       (sexp_put_soft_newline): New function.
+
+       * tools/output.h (struct sexp_output): Removed union with single
+       element, and updated all users. New attribute soft_newline.
+
 2008-12-22  Niels Möller  <nisse@lysator.liu.se>
 
        * Makefile.in ($(des_headers)): Create files in $(srcdir).
index eed19e5a251ceb66613434420cb238989e5fc20b..a3470ab25c9b738f4475431db32b588a0722e378 100755 (executable)
@@ -91,11 +91,9 @@ test_advanced '(foo bar baz)' '(foo bar
      baz)' 
 test_advanced '; comment
 ()' '; comment
-
 ()' 
 test_advanced '(foo ; gazonk
 bar)' '(foo ; gazonk
-     
      bar)'
 
 test_advanced '(foo[bar]foo)' '(foo [bar]foo)'
index 6110b874fe450d224baf5d37125ed293037252e5..d491d9a470b18d5666ee4f66cd7c0634ed2c3afd 100644 (file)
@@ -47,6 +47,7 @@ sexp_output_init(struct sexp_output *output, FILE *f,
   output->ctx = NULL;
   
   output->pos = 0;
+  output->soft_newline = 0;
 }
 
 void
@@ -61,24 +62,41 @@ sexp_output_hash_init(struct sexp_output *output,
 static void
 sexp_put_raw_char(struct sexp_output *output, uint8_t c)
 {
-  output->pos++;
   if (putc(c, output->f) < 0)
     die("Write failed: %s\n", strerror(errno));
+
+  output->pos++;
+  output->soft_newline = 0;
 }
 
-void 
+void
 sexp_put_newline(struct sexp_output *output,
                 unsigned indent)
 {
-  unsigned i;
+  if (output->soft_newline)
+    output->soft_newline = 0;
+  else
+    {
+      unsigned i;
 
-  sexp_put_raw_char(output, '\n');
-  output->pos = 0;
+      sexp_put_raw_char(output, '\n');
+      output->pos = 0;
   
-  for(i = 0; i < indent; i++)
-    sexp_put_raw_char(output, ' ');
+      for(i = 0; i < indent; i++)
+       sexp_put_raw_char(output, ' ');
   
-  output->pos = indent;
+      output->pos = indent;
+    }
+}
+
+/* Put a newline, but only if it is followed by another newline,
+   collaps to one newline only. */
+void
+sexp_put_soft_newline(struct sexp_output *output,
+                     unsigned indent)
+{
+  sexp_put_newline(output, indent);
+  output->soft_newline = 1;
 }
 
 void
@@ -86,13 +104,13 @@ sexp_put_char(struct sexp_output *output, uint8_t c)
 {
   if (output->coding)
     {
-      /* Two is enough for both hex and base64. */
+      /* Two is enough for both base16 and base64. */
       uint8_t encoded[2];
       unsigned done;
 
       unsigned i;
-      
-      done = output->coding->encode_update(&output->state, encoded,
+
+      done = output->coding->encode_update(&output->base64, encoded,
                                           1, &c);
       assert(done <= sizeof(encoded));
       
@@ -149,7 +167,7 @@ sexp_put_code_start(struct sexp_output *output,
   output->coding_indent = output->pos;
   
   output->coding = coding;
-  output->coding->encode_init(&output->state);
+  output->coding->encode_init(&output->base64);
 }
 
 void
@@ -161,7 +179,7 @@ sexp_put_code_end(struct sexp_output *output)
 
   assert(output->coding);
 
-  done = output->coding->encode_final(&output->state, encoded);
+  done = output->coding->encode_final(&output->base64, encoded);
 
   assert(done <= sizeof(encoded));
   
index 79f147a06f1b14fab1dadc31246f7ac1a8722362..8ac52562a5c23045a1f760a14c9d929233fb1d37 100644 (file)
@@ -45,12 +45,12 @@ struct sexp_output
   const struct nettle_hash *hash;
   void *ctx;
   
-  union {
-    struct base64_decode_ctx base64;
-    /* NOTE: There's no context for hex encoding */
-  } state;
+  /* NOTE: There's no context for hex encoding, the state argument to
+     encode_update is ignored */
+  struct base64_decode_ctx base64;
   
   unsigned pos;
+  int soft_newline;
 };
 
 void
@@ -65,6 +65,10 @@ void
 sexp_put_newline(struct sexp_output *output,
                 unsigned indent);
 
+void
+sexp_put_soft_newline(struct sexp_output *output,
+                     unsigned indent);
+
 void
 sexp_put_char(struct sexp_output *output, uint8_t c);
 
index d6ebef0368487756fe0fd632d3a7424f4f6674cc..4b08a9cd7562b86a1d1ca11d254778aaf642c523 100644 (file)
@@ -110,13 +110,26 @@ sexp_convert_item(struct sexp_parser *parser,
              {
                /* FIXME: Adapt pretty printing to handle a big first
                 * element. */
-               if (item == 1)
+               switch (item)
                  {
+                 case 0:
+                   if (token->type == SEXP_COMMENT)
+                     {
+                       indent = output->pos;
+                       /* Disable the indentation setup for next item */
+                       item++;
+                     }
+                   break;
+                   
+                 case  1:
                    sexp_put_char(output, ' ');
                    indent = output->pos;
+                   break;
+
+                 default:
+                   sexp_put_newline(output, indent);
+                   break;
                  }
-               else if (item > 1)
-                 sexp_put_newline(output, indent);
              }
 
            sexp_convert_item(parser, token, output, mode_out, indent);
@@ -141,9 +154,7 @@ sexp_convert_item(struct sexp_parser *parser,
       if (mode_out == SEXP_ADVANCED)
        {
          sexp_put_data(output, token->string.size, token->string.contents);
-         /* This newline is necessary only if the comment comes first
-            in a list. It would be nice to supress extra newlines. */
-         sexp_put_newline(output, indent);
+         sexp_put_soft_newline(output, indent);
        }
       break;
     default: