Commit 80e6bee3 authored by josé bollo's avatar josé bollo

Replace painful 'partial' with automatic stuff

Change-Id: I263b7de02cd40a8466f11f4f763f63f6af2d3e19
Signed-off-by: josé bollo's avatarJosé Bollo <jobol@nonadev.net>
parent fd8a7235
......@@ -183,21 +183,12 @@ static int leave(void *closure)
return 0;
}
static int partial(void *closure, const char *name, char **result)
{
struct expl *e = closure;
struct json_object *o = find(e, name);
*result = strdup(json_object_get_string(o));
return 0;
}
static struct mustach_itf itf = {
.start = start,
.put = put,
.enter = enter,
.next = next,
.leave = leave,
.partial = partial
.leave = leave
};
int fmustach_json_c(const char *template, struct json_object *root, FILE *file)
......
......@@ -30,6 +30,30 @@
#define NAME_LENGTH_MAX 1024
#define DEPTH_MAX 256
static int getpartial(struct mustach_itf *itf, void *closure, const char *name, char **result)
{
int rc;
FILE *file;
size_t size;
*result = NULL;
file = open_memstream(result, &size);
if (file == NULL)
rc = MUSTACH_ERROR_SYSTEM;
else {
rc = itf->put(closure, name, 0, file);
if (rc == 0)
/* adds terminating null */
rc = fputc(0, file) ? MUSTACH_ERROR_SYSTEM : 0;
fclose(file);
if (rc < 0) {
free(result);
*result = NULL;
}
}
return rc;
}
static int process(const char *template, struct mustach_itf *itf, void *closure, FILE *file, const char *opstr, const char *clstr)
{
char name[NAME_LENGTH_MAX + 1], *partial, c;
......@@ -151,9 +175,9 @@ static int process(const char *template, struct mustach_itf *itf, void *closure,
break;
case '>':
/* partials */
if (emit && itf->partial) {
rc = itf->partial(closure, name, &partial);
if (rc == 0 && partial) {
if (emit) {
rc = getpartial(itf, closure, name, &partial);
if (rc == 0) {
rc = process(partial, itf, closure, file, opstr, clstr);
free(partial);
}
......
......@@ -32,6 +32,7 @@
* All other functions should normally return 0.
*
* @start: Starts the mustach processing of the closure
* 'start' is optional (can be NULL)
*
* @put: Writes the value of 'name' to 'file' with 'escape' or not
*
......@@ -48,10 +49,6 @@
* Musts return 0 when there is no item to activate.
*
* @leave: Leaves the last entered section
*
* @partial: Returns an allocated string for the partial content for 'name'.
* The returned string (in 'result') will be freed by mustach
* 'partial' is optional, it cans be NULL.
*/
struct mustach_itf {
int (*start)(void *closure);
......@@ -59,7 +56,6 @@ struct mustach_itf {
int (*enter)(void *closure, const char *name);
int (*next)(void *closure);
int (*leave)(void *closure);
int (*partial)(void *closure, const char *name, char **result);
};
#define MUSTACH_OK 0
......
......@@ -6,7 +6,7 @@ Command: ../mustach json must
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 157 allocs, 157 frees, 19,159 bytes allocated
total heap usage: 159 allocs, 159 frees, 27,936 bytes allocated
All heap blocks were freed -- no leaks are possible
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment