1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
* C TemplateS : template expander
* Based on the original libctemplate by Stephen C. Losen
*
* Version 0.1
*
* Copyright (c) 2017-2019 Alexander M. Pickering (alex@cogarr.net)
*
* Copyright 2017 Alexander M. Pickering Distributed under the terms
* of the GNU General Public License (GPL)
*/
#include <stddef.h>
#ifndef _CTEMPLATE_H
#define _CTEMPLATE_H
#define MAX_TEMPLATE_LENGTH 2147384647
#define ERRBUF_HINTLEN 50
//Length of error messages
#define ERR_MSG_LEN 500
/* A variable list, holds key-value pairs as (string -> (string | loop)) */
struct TMPL_varlist;
/* A list structure, holds an array of varlists to be looped over*/
struct TMPL_loop;
/* Holdes all the data needed for a template*/
struct TMPL_templates;
/* Adds a variable to a variable list under a certain name */
void TMPL_add_var_to_varlist(struct TMPL_varlist* vl, const char* name, const char* value);
/* Adds a loop to a variable list under a certain name */
void TMPL_add_loop_to_varlist(struct TMPL_varlist* vl, const char* name, struct TMPL_loop* loop);
/* Adds a variable list as one of the varlists in a loop */
void TMPL_add_varlist_to_loop(struct TMPL_loop* l, struct TMPL_varlist* vl);
/* Create a new varlist. If allocation fails, the struct retuned will be NULL */
struct TMPL_varlist* TMPL_alloc_varlist(void);
/* Frees a varlist (and recursively, any loops under it, any varlists under those loops, ect.) */
void TMPL_free_varlist(struct TMPL_varlist* t);
/* Allocate a new template. If allocation fails, the retunred value will be non-zero*/
int TMPL_alloc_template(const char* tmplstr, struct TMPL_templates **t);
/* Free a template. Do not use the string returned by TMPL_render() the template is freed */
void TMPL_free_template(struct TMPL_templates* t);
/* Create a new loop. If allocation fails, the struct returned will be NULL */
struct TMPL_loop* TMPL_alloc_loop(void);
/* Free a loop. If the loop is added to a varlist, just call free on the varlist
and any loops that have been added to it will automatically be freed.*/
void TMPL_free_loop(struct TMPL_loop* tl);
/* Takes a template and varlist, and collapses it into a string.
If size_p is non-null, the size of the resulting string will be placed in it.
If any errors happened the t->error will be non-zero and the error can be
retreived with TMPL_err(t,&size)*/
char* TMPL_render(struct TMPL_templates* t, struct TMPL_varlist* varlist, size_t* size_p);
/*Returns a string error that might give more information*/
const char* TMPL_err(struct TMPL_templates* t,size_t* size);
/* A debug functions */
void print_varlist(struct TMPL_varlist* vl);
void print_ast(struct TMPL_templates* root);
#endif
|