clean up of files and fixed docs
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "xjson.h"
|
||||
#include "tinytemplate.h"
|
||||
|
||||
typedef struct wrap_t wrap_t;
|
||||
typedef struct eval_context_t eval_context_t;
|
||||
|
||||
struct wrap_t {
|
||||
eval_context_t *context;
|
||||
union {
|
||||
xj_value *value;
|
||||
wrap_t *next;
|
||||
};
|
||||
};
|
||||
|
||||
struct eval_context_t {
|
||||
FILE *stream;
|
||||
wrap_t params;
|
||||
wrap_t *free;
|
||||
wrap_t pool[TINYTEMPLATE_MAX_ITER_DEPTH];
|
||||
};
|
||||
|
||||
static bool query_json_array(void *data,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value);
|
||||
|
||||
static bool query_json_object(void *data, const char *key, size_t len,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value);
|
||||
|
||||
static void convert_json_object(eval_context_t *context,
|
||||
xj_value *child,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value)
|
||||
{
|
||||
switch (child->type) {
|
||||
|
||||
case XJ_NULL:
|
||||
*type = TINYTEMPLATE_TYPE_INT;
|
||||
value->as_int = 0;
|
||||
break;
|
||||
|
||||
case XJ_INT:
|
||||
*type = TINYTEMPLATE_TYPE_INT;
|
||||
value->as_int = child->as_int;
|
||||
break;
|
||||
|
||||
case XJ_FLOAT:
|
||||
*type = TINYTEMPLATE_TYPE_INT;
|
||||
value->as_float = child->as_float;
|
||||
break;
|
||||
|
||||
case XJ_BOOL:
|
||||
*type = TINYTEMPLATE_TYPE_INT;
|
||||
value->as_int = child->as_bool;
|
||||
break;
|
||||
|
||||
case XJ_ARRAY:
|
||||
{
|
||||
wrap_t *wrap = context->free;
|
||||
*type = TINYTEMPLATE_TYPE_ARRAY;
|
||||
value->as_array.data = wrap;
|
||||
value->as_array.next = query_json_array;
|
||||
if (wrap) {
|
||||
context->free = wrap->next;
|
||||
wrap->value = child->as_array;
|
||||
wrap->context = context;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case XJ_OBJECT:
|
||||
*type = TINYTEMPLATE_TYPE_DICT;
|
||||
value->as_dict.data = child;
|
||||
value->as_dict.get = query_json_object;
|
||||
break;
|
||||
|
||||
case XJ_STRING:
|
||||
*type = TINYTEMPLATE_TYPE_STRING;
|
||||
value->as_string.str = child->as_string;
|
||||
value->as_string.len = child->size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static bool query_json_array(void *data,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value)
|
||||
{
|
||||
wrap_t *wrap = data;
|
||||
|
||||
if (wrap->value) {
|
||||
convert_json_object(wrap->context, wrap->value, type, value);
|
||||
wrap->value = wrap->value->next;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool query_json_object(void *data, const char *key, size_t len,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value)
|
||||
{
|
||||
wrap_t *wrap = data;
|
||||
|
||||
assert(wrap->value->type == XJ_OBJECT);
|
||||
|
||||
xj_value *child = wrap->value->as_object;
|
||||
while (child) {
|
||||
size_t keylen = strlen(child->key);
|
||||
if (keylen == len && !strncmp(key, child->key, len))
|
||||
break;
|
||||
child = child->next;
|
||||
}
|
||||
if (!child)
|
||||
return false;
|
||||
|
||||
convert_json_object(wrap->context, child, type, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void callback(void *userp, const char *lbl, size_t lbllen,
|
||||
const char *str, size_t len)
|
||||
{
|
||||
(void) lbl;
|
||||
(void) lbllen;
|
||||
|
||||
eval_context_t *context = userp;
|
||||
fwrite(str, 1, len, context->stream);
|
||||
}
|
||||
|
||||
static bool query_root_json_object(void *data, const char *key, size_t len,
|
||||
tinytemplate_type_t *type,
|
||||
tinytemplate_value_t *value)
|
||||
{
|
||||
eval_context_t *context = data;
|
||||
if (context->params.value)
|
||||
return query_json_object(&context->params, key, len, type, value);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static void init_context(eval_context_t *context,
|
||||
FILE *stream, xj_value *root)
|
||||
{
|
||||
context->stream = stream;
|
||||
context->params.context = context;
|
||||
context->params.value = root;
|
||||
context->free = context->pool;
|
||||
|
||||
for (int i = 0; i < TINYTEMPLATE_MAX_ITER_DEPTH-1; i++)
|
||||
context->pool[i].next = &context->pool[i+1];
|
||||
context->pool[TINYTEMPLATE_MAX_ITER_DEPTH-1].next = NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *param_file = NULL;
|
||||
if (argc > 1)
|
||||
param_file = argv[1];
|
||||
|
||||
char buffer[1 << 16];
|
||||
char json[1 << 16];
|
||||
xj_value *root = NULL;
|
||||
xj_alloc *alloc = NULL;
|
||||
|
||||
if (param_file) {
|
||||
|
||||
FILE *stream = fopen(param_file, "rb");
|
||||
if (stream == NULL) {
|
||||
fprintf(stderr, "Couldn't open parameter file \"%s\"\n", param_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t num = fread(buffer, 1, sizeof(buffer), stream);
|
||||
if (ferror(stream)) {
|
||||
fprintf(stderr, "Couldn't read from \"%s\"\n", param_file);
|
||||
fclose(stream);
|
||||
return -1;
|
||||
}
|
||||
|
||||
alloc = xj_alloc_using(json, sizeof(json), 0, NULL);
|
||||
|
||||
xj_error error;
|
||||
root = xj_decode(buffer, (int) num, alloc, &error);
|
||||
if (root == NULL) {
|
||||
fprintf(stderr, "Couldn't decode JSON (%s)\n", error.message);
|
||||
fclose(stream);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
size_t num = fread(buffer, 1, sizeof(buffer), stdin);
|
||||
if (ferror(stdin)) {
|
||||
fprintf(stderr, "Failed to read from stdin\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char message[256];
|
||||
|
||||
tinytemplate_instr_t *program = NULL;
|
||||
size_t max_instr = 1 << 10;
|
||||
|
||||
tinytemplate_status_t status;
|
||||
do {
|
||||
max_instr *= 2;
|
||||
program = realloc(program, max_instr * sizeof(tinytemplate_instr_t));
|
||||
if (program == NULL) {
|
||||
fprintf(stderr, "Out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = tinytemplate_compile(buffer, num, program, max_instr, NULL, message, sizeof(message));
|
||||
if (status != TINYTEMPLATE_STATUS_DONE &&
|
||||
status != TINYTEMPLATE_STATUS_EMEMORY) {
|
||||
fprintf(stderr, "Failed to compile template (%s)\n", message);
|
||||
return -1;
|
||||
}
|
||||
} while (status == TINYTEMPLATE_STATUS_EMEMORY);
|
||||
|
||||
eval_context_t context;
|
||||
init_context(&context, stdout, root);
|
||||
|
||||
if (tinytemplate_eval(buffer, program, &context, query_root_json_object, callback, message, sizeof(message)) != TINYTEMPLATE_STATUS_DONE) {
|
||||
fprintf(stderr, "Failed to evaluate template (%s)\n", message);
|
||||
free(program);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(program);
|
||||
return 0;
|
||||
}
|
||||
+1674
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,89 @@
|
||||
#ifndef TINYTEMPLATE_H
|
||||
#define TINYTEMPLATE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef TINYTEMPLATE_MAX_SCOPE_DEPTH
|
||||
#define TINYTEMPLATE_MAX_SCOPE_DEPTH 8
|
||||
#endif
|
||||
|
||||
#ifndef TINYTEMPLATE_MAX_EXPR_DEPTH
|
||||
#define TINYTEMPLATE_MAX_EXPR_DEPTH 8
|
||||
#endif
|
||||
|
||||
#ifndef TINYTEMPLATE_MAX_ITER_DEPTH
|
||||
#define TINYTEMPLATE_MAX_ITER_DEPTH 8
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
TINYTEMPLATE_TYPE_INT,
|
||||
TINYTEMPLATE_TYPE_FLOAT,
|
||||
TINYTEMPLATE_TYPE_DICT,
|
||||
TINYTEMPLATE_TYPE_ARRAY,
|
||||
TINYTEMPLATE_TYPE_STRING,
|
||||
} tinytemplate_type_t;
|
||||
|
||||
typedef union tinytemplate_value_t tinytemplate_value_t;
|
||||
|
||||
typedef bool (*tinytemplate_nextcallback_t)(void*, tinytemplate_type_t*, tinytemplate_value_t*);
|
||||
typedef bool (*tinytemplate_getter_t)(void*, const char*, size_t, tinytemplate_type_t*, tinytemplate_value_t*);
|
||||
typedef void (*tinytemplate_callback_t)(void *userp, const char *lbl, size_t lblen, const char *str, size_t len);
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
tinytemplate_getter_t get;
|
||||
} tinytemplate_dict_t;
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
tinytemplate_nextcallback_t next;
|
||||
} tinytemplate_array_t;
|
||||
|
||||
typedef struct {
|
||||
const char *str;
|
||||
size_t len;
|
||||
} tinytemplate_string_t;
|
||||
|
||||
union tinytemplate_value_t {
|
||||
int64_t as_int;
|
||||
double as_float;
|
||||
tinytemplate_dict_t as_dict;
|
||||
tinytemplate_array_t as_array;
|
||||
tinytemplate_string_t as_string;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int opcode;
|
||||
struct {
|
||||
int64_t as_int;
|
||||
size_t as_size;
|
||||
double as_float;
|
||||
} operands[2];
|
||||
} tinytemplate_instr_t;
|
||||
|
||||
typedef enum {
|
||||
TINYTEMPLATE_STATUS_DONE,
|
||||
TINYTEMPLATE_STATUS_ESYMBOL,
|
||||
TINYTEMPLATE_STATUS_ESCOPE,
|
||||
TINYTEMPLATE_STATUS_EDEPTH,
|
||||
TINYTEMPLATE_STATUS_ETYPE,
|
||||
TINYTEMPLATE_STATUS_EITER,
|
||||
TINYTEMPLATE_STATUS_EMEMORY,
|
||||
TINYTEMPLATE_STATUS_ESYNTAX,
|
||||
TINYTEMPLATE_STATUS_ESEMANT,
|
||||
} tinytemplate_status_t;
|
||||
|
||||
tinytemplate_status_t
|
||||
tinytemplate_eval(const char *src, const tinytemplate_instr_t *program,
|
||||
void *userp, tinytemplate_getter_t params,
|
||||
tinytemplate_callback_t callback,
|
||||
char *errmsg, size_t errmax);
|
||||
|
||||
tinytemplate_status_t
|
||||
tinytemplate_compile(const char *src, size_t len,
|
||||
tinytemplate_instr_t *program,
|
||||
size_t max_instr, size_t *num_instr,
|
||||
char *errmsg, size_t errmax);
|
||||
#endif
|
||||
Reference in New Issue
Block a user