libtemplate

Artifact [34ad5bcb64]
Login

Artifact 34ad5bcb645ab7b9545cf9e20ff3b542ee29b84375b03af05008c0ba9747c74f:


#ifndef TEMPLATE_H
#define TEMPLATE_H

#include <stdbool.h>
#include <json.h>

struct template;

/*
 * create a new template structure
 * - return newly allocated template
 */
struct template *template_new(void);

/*
 * parse a template from a string
 * - return true on success, false otherwise
 */
bool template_parse_string(struct template *tpl, const char *str);

/*
 * parse a template from a file
 * - return true on success, false otherwise
 */
bool template_parse_file(struct template *tpl, const char *filename);

/*
 * render a parsed template to stdout, using 'json' as input
 * and print to result to stdout.
 */
void template_render(struct template *tpl, struct json *json);

/*
 * render a parsed template to string, using 'json' as input
 * - return a newly allocated string
 */
char *template_render_to_string(struct template *tpl, struct json *json);

/*
 * render a parsed template to a file, using 'json' as input
 * - return true on success, false otherwise
 */
bool template_render_to_file(struct template *tpl, struct json *json, const char *filename);

/*
 * free an allocated template
 */
void template_free(struct template *tpl);

/*
 * fetch error string in case of error
 * - return the last error message
 */
const char *template_error_msg(struct template *tpl);

/*
 * convenient function to quickly render a template file to stdout
 * without the need to instantiate then free a template parser:
 * - return true on success, false otherwise.
 */
bool template_render_file(const char *filename, struct json *json);


/*
 * Writer: entity reponsible of taking care of custom formatter output.
 */

struct writer;
void writer_write(struct writer *writer, void *data, size_t len);
void writer_write_string(struct writer *writer, const char *str);

/*
 * Register a custom formatter for a template:
 * - register formatter under 'name' identity
 * - the formatter itself is a function pointer
 *   responsible of rendering a 'json' structure
 *   and write the output to a 'writer'.
 */
void template_set_formatter(struct template *tpl, const char *name,
        void (*)(struct json *, struct writer *));

#endif