new xj_snprintf and xj_decodef. Code from stb_sprintf was pulled in

This commit is contained in:
cozis
2022-04-28 13:04:41 +02:00
parent 12ffc1a667
commit 8fbdd029d4
6 changed files with 1736 additions and 5 deletions
+1 -1
View File
@@ -10,6 +10,6 @@ do
esac esac
done done
$CC tests/test.c src/xjson.c -o test $FLAGS $CC tests/test.c src/xjson.c src/xj_snprintf.c -o test $FLAGS
$CC examples/parse-file.c src/xjson.c -o parse-file $FLAGS $CC examples/parse-file.c src/xjson.c -o parse-file $FLAGS
$CC examples/simple.c src/xjson.c -o simple $FLAGS $CC examples/simple.c src/xjson.c -o simple $FLAGS
+1716
View File
File diff suppressed because it is too large Load Diff
+8
View File
@@ -0,0 +1,8 @@
#ifndef XJ_SNPRINTF_H
#define XJ_SNPRINTF_H
#include <stdarg.h>
#include "xjson.h"
int xj_vsnprintf(char *buf, int count, const char *fmt, va_list va);
xj_value *xj_vdecodef(xj_alloc *alloc, xj_error *error, const char *fmt, va_list va);
xj_value *xj_decodef(xj_alloc *alloc, xj_error *error, const char *fmt, ...);
#endif
+1 -3
View File
@@ -297,7 +297,7 @@ void *xj_bpalloc(xj_alloc *alloc, int size)
return addr; return addr;
} }
static void xj_preport(xj_error *error, const char *src, int off, const char *fmt, ...) void xj_preport(xj_error *error, const char *src, int off, const char *fmt, ...)
{ {
if(error != NULL) if(error != NULL)
{ {
@@ -341,8 +341,6 @@ static void xj_preport(xj_error *error, const char *src, int off, const char *fm
} }
} }
#define xj_report(error, fmt, ...) xj_preport(error, NULL, -1, fmt, ## __VA_ARGS__)
// Create an [xj_value] that represents the [null] JSON value. // Create an [xj_value] that represents the [null] JSON value.
xj_value *xj_value_null(xj_alloc *alloc, xj_error *error) xj_value *xj_value_null(xj_alloc *alloc, xj_error *error)
{ {
+8
View File
@@ -1,3 +1,6 @@
#ifndef XJSON_H
#define XJSON_H
typedef double xj_f64; typedef double xj_f64;
typedef long long xj_i64; typedef long long xj_i64;
typedef _Bool xj_bool; typedef _Bool xj_bool;
@@ -44,6 +47,9 @@ xj_alloc *xj_alloc_new(int size, int ext);
void xj_alloc_del(xj_alloc *alloc); void xj_alloc_del(xj_alloc *alloc);
void *xj_bpalloc(xj_alloc *alloc, int size); void *xj_bpalloc(xj_alloc *alloc, int size);
void xj_preport(xj_error *error, const char *src, int off, const char *fmt, ...);
#define xj_report(error, fmt, ...) xj_preport(error, NULL, -1, fmt, ## __VA_ARGS__)
xj_value *xj_value_null(xj_alloc *alloc, xj_error *error); xj_value *xj_value_null(xj_alloc *alloc, xj_error *error);
xj_value *xj_value_bool(xj_bool val, xj_alloc *alloc, xj_error *error); xj_value *xj_value_bool(xj_bool val, xj_alloc *alloc, xj_error *error);
xj_value *xj_value_int(xj_i64 val, xj_alloc *alloc, xj_error *error); xj_value *xj_value_int(xj_i64 val, xj_alloc *alloc, xj_error *error);
@@ -58,3 +64,5 @@ char *xj_strdup(const char *str, int len, xj_alloc *alloc, xj_error *error);
xj_value *xj_decode(const char *str, int len, xj_alloc *alloc, xj_error *error); xj_value *xj_decode(const char *str, int len, xj_alloc *alloc, xj_error *error);
char *xj_encode(xj_value *value, int *len); char *xj_encode(xj_value *value, int *len);
#endif /* XJSON_H */
+2 -1
View File
@@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include "xjson.h" #include <xjson.h>
#include <xj_snprintf.h>
#define TEST(src_) { .line = __LINE__, .src = src_ } #define TEST(src_) { .line = __LINE__, .src = src_ }