Sergey Lyubka 11 rokov pred
rodič
commit
f0be0154d3
4 zmenil súbory, kde vykonal 55 pridanie a 11 odobranie
  1. 3 11
      README.md
  2. 43 0
      frozen.c
  3. 1 0
      frozen.h
  4. 8 0
      unit_test.c

+ 3 - 11
README.md

@@ -157,18 +157,10 @@ function. Values for `null`, `true`, `false`, and characters
     ASSERT(find_json_token(tokens, "ports[3]") == NULL);  // Outside boundaries
     ASSERT(find_json_token(tokens, "foo.bar") == NULL);   // Nonexistent
 
-## Example: generating JSON string `{"foo":[-123,true]}`
+## Example: generating JSON string `{ "foo": [-123, true, false, null] }`
 
-    char buf[1000], *p = buf;
-
-    p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, "{");
-    p += json_emit_quoted_str(p, &buf[sizeof(buf)] - p, "foo");
-    p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, ":[");
-    p += json_emit_int(p, &buf[sizeof(buf)] - p, -123);
-    p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, ",true]}");
-
-    ASSERT(strcmp(buf, "{\"foo\":[-123,true]}") == 0);
-    ASSERT(p < &buf[sizeof(buf)]);
+    char buf[1000];
+    json_emit(buf, sizeof(buf), "{ s: [i, T, F, N] }", "foo", (long) -123);
 
 # License
 

+ 43 - 0
frozen.c

@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include "frozen.h"
 
 #ifdef _WIN32
@@ -394,3 +395,45 @@ int json_emit_quoted_str(char *buf, int buf_len, const char *str) {
 int json_emit_raw_str(char *buf, int buf_len, const char *str) {
   return buf_len <= 0 ? 0 : snprintf(buf, buf_len, "%s", str);
 }
+
+int json_emit(char *s, int s_len, const char *fmt, ...) {
+  const char *end = s + s_len;
+  va_list ap;
+
+  va_start(ap, fmt);
+  while (*fmt != '\0') {
+    switch (*fmt) {
+      case '[': case ']': case '{': case '}': case ',': case ':':
+      case ' ': case '\r': case '\n': case '\t':
+        if (s < end) *s++ = *fmt;
+        break;
+      case 'i':
+        s += json_emit_int(s, end - s, va_arg(ap, long));
+        break;
+      case 'f':
+        s += json_emit_double(s, end - s, va_arg(ap, double));
+        break;
+      case 's':
+        s += json_emit_quoted_str(s, end - s, va_arg(ap, char *));
+        break;
+      case 'S':
+        s += json_emit_raw_str(s, end - s, va_arg(ap, char *));
+        break;
+      case 'T':
+        s += json_emit_raw_str(s, end - s, "true");
+        break;
+      case 'F':
+        s += json_emit_raw_str(s, end - s, "false");
+        break;
+      case 'N':
+        s += json_emit_raw_str(s, end - s, "null");
+        break;
+      default:
+        return 0;
+    }
+    fmt++;
+  }
+  va_end(ap);
+
+  return end - s;
+}

+ 1 - 0
frozen.h

@@ -57,6 +57,7 @@ int json_emit_int(char *buf, int buf_len, long int value);
 int json_emit_double(char *buf, int buf_len, double value);
 int json_emit_quoted_str(char *buf, int buf_len, const char *str);
 int json_emit_raw_str(char *buf, int buf_len, const char *str);
+int json_emit(char *buf, int buf_len, const char *fmt, ...);
 
 #ifdef __cplusplus
 }

+ 8 - 0
unit_test.c

@@ -215,6 +215,14 @@ static const char *test_emit(void) {
   ASSERT(strcmp(buf, s5) == 0);
   ASSERT(p < &buf[sizeof(buf)]);
 
+  ASSERT(json_emit(buf, sizeof(buf), "{s:[i,f,S]}",
+         "foo", (long) -123, 1.23, "true") > 0);
+  ASSERT(json_emit(buf, 4, "{S:i}", "a", 12345) < 0);
+  ASSERT(json_emit(buf, sizeof(buf), "{S:d}", "a", 12345) == 0);
+
+  ASSERT(json_emit(buf, sizeof(buf), "{s:[i,T, F,N]}", "foo", (long) -7) > 0);
+  ASSERT(strcmp(buf, "{\"foo\":[-7,true, false,null]}") == 0);
+
   return NULL;
 }