]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
(format_string): New function.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 10 Feb 2003 16:26:01 +0000 (17:26 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 10 Feb 2003 16:26:01 +0000 (17:26 +0100)
(sexp_vformat): Implemented support for literals in the format
string.

Rev: src/nettle/sexp-format.c:1.6
Rev: src/nettle/sexp.h:1.12

sexp-format.c
sexp.h

index 5c98f7a7f5caeed73a5601e1a8730004e1d7fa15..6fdc89109e1bd1defd7fdc4895a8812542cbde30 100644 (file)
@@ -71,6 +71,20 @@ format_prefix(struct nettle_buffer *buffer,
   return prefix_length + 1;
 }
 
+static unsigned
+format_string(struct nettle_buffer *buffer,
+             unsigned length, const uint8_t *s)
+{
+  unsigned prefix_length = format_prefix(buffer, length);
+  if (!prefix_length)
+    return 0;
+
+  if (buffer && !nettle_buffer_write(buffer, length, s))
+    return 0;
+
+  return prefix_length + length;
+}
+
 unsigned
 sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
 {
@@ -81,8 +95,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
     switch (*format++)
       {
       default:
-       abort();
-
+       {
+         const char *start = format - 1;
+         unsigned length = 1 + strcspn(format, "()%");
+         unsigned output_length = format_string(buffer, length, start);
+         if (!output_length)
+           return 0;
+         
+         done += output_length;
+         format = start + length;
+
+         break;
+       }
       case '\0':
        assert(!nesting);
            
@@ -123,7 +147,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
              {
                const char *s;
                unsigned length;
-               unsigned prefix_length;
+               unsigned output_length;
                
                if (nul_flag)
                  {
@@ -136,25 +160,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
                    s = va_arg(args, const char *);
                  }
                
-               prefix_length = format_prefix(buffer, length);
-             
-               if (!prefix_length)
+               output_length = format_string(buffer, length, s);
+               if (!output_length)
                  return 0;
 
-               done += prefix_length;
-
-               if (buffer && !nettle_buffer_write(buffer, length, s))
-                 return 0;
-
-               done += length;
-
+               done += output_length;
                break;
              }
            case 't':
              {
                const char *s;
                unsigned length;
-               unsigned prefix_length;
+               unsigned output_length;
                
                if (nul_flag)
                  {
@@ -176,18 +193,13 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
                  return 0;
                done++;
                
-               prefix_length = format_prefix(buffer, length);
+               output_length = format_string(buffer, length, s);
              
-               if (!prefix_length)
+               if (!output_length)
                  return 0;
 
-               done += prefix_length;
-
-               if (buffer && !nettle_buffer_write(buffer, length, s))
-                 return 0;
-
-               done += length;
-
+               done += output_length;
+               
                if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
                  return 0;
                done++;
diff --git a/sexp.h b/sexp.h
index aa86db24002cffbbbbcb5688855b35f7752100fd..1849988b963c4a9c41a2efedec1bcccdbea7a636 100644 (file)
--- a/sexp.h
+++ b/sexp.h
@@ -134,7 +134,8 @@ struct nettle_buffer;
 /* Returns the number of output characters, or 0 on out of memory. If
  * buffer == NULL, just compute length.
  *
- * Format strings can contained matched parentheses, and the following
+ * Format strings can contained matched parentheses, tokens ("foo" in
+ * the format string is formatted as "3:foo"), and the following
  * formatting specifiers:
  *
  *   %s   String represented as unsigned length, const uint8_t *data.
@@ -157,8 +158,7 @@ struct nettle_buffer;
  *        instead the string is NUL-terminated, and there's only one
  *        const uint8_t * argument.
  *
- * FIXME: Allow literals, like "(x%b)". Allow "%(" for unbalanced
- * parenthesis. */
+ * FIXME: Allow "%(" for unbalanced parenthesis. */
 
 unsigned
 sexp_format(struct nettle_buffer *buffer,
@@ -179,4 +179,10 @@ unsigned
 sexp_transport_vformat(struct nettle_buffer *buffer,
                       const char *format, va_list args);
 
+/* Classification for advanced syntax. */
+extern const char
+sexp_token_chars[0x80];
+
+#define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
+
 #endif /* NETTLE_SEXP_H_INCLUDED */