readme update after changind the code
This commit is contained in:
@@ -70,17 +70,18 @@ The compilation function expects a source string of the template through `src` a
|
|||||||
|
|
||||||
Here's an example of how you would use this function:
|
Here's an example of how you would use this function:
|
||||||
```c
|
```c
|
||||||
|
#define COUNT(X) ((int) (sizeof(X) / sizeof((X)[0])))
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char message[128];
|
char message[128];
|
||||||
tinytemplate_instr_t program[32];
|
tinytemplate_instr_t prog[32];
|
||||||
tinytemplate_status_t status;
|
tinytemplate_status_t status;
|
||||||
|
|
||||||
static const char text[] = "Hello, my name is {{name}}!";
|
static const char text[] = "Hello, my name is {{name}}!";
|
||||||
|
|
||||||
size_t num_instr;
|
size_t num_instr;
|
||||||
size_t max_instr = sizeof(program) / sizeof(program[0]);
|
status = tinytemplate_compile(text, strlen(text), program, COUNT(prog),
|
||||||
status = tinytemplate_compile(text, sizeof(text)-1, program, max_instr,
|
|
||||||
NULL, message, sizeof(message));
|
NULL, message, sizeof(message));
|
||||||
if (status != TINYTEMPLATE_STATUS_DONE) {
|
if (status != TINYTEMPLATE_STATUS_DONE) {
|
||||||
fprintf(stderr, "Error: %s", message);
|
fprintf(stderr, "Error: %s", message);
|
||||||
@@ -96,18 +97,15 @@ int main(void)
|
|||||||
To evaluate a program you need to provide both the program and the source string of the template. Also the caller must specify two callbacks: `params` and `callback`. The `params` callback, which has the following interface:
|
To evaluate a program you need to provide both the program and the source string of the template. Also the caller must specify two callbacks: `params` and `callback`. The `params` callback, which has the following interface:
|
||||||
```c
|
```c
|
||||||
bool params(void *data, const char *key, size_t len,
|
bool params(void *data, const char *key, size_t len,
|
||||||
tinytemplate_type_t *type,
|
|
||||||
tinytemplate_value_t *value);
|
tinytemplate_value_t *value);
|
||||||
```
|
```
|
||||||
provides the values of the parameters references by the template. For example if defining the parameter template as following:
|
provides the values of the parameters references by the template. For example if defining the parameter template as following:
|
||||||
```c
|
```c
|
||||||
bool params(void *data, const char *key, size_t len,
|
bool params(void *data, const char *key, size_t len,
|
||||||
tinytemplate_type_t *type,
|
|
||||||
tinytemplate_value_t *value)
|
tinytemplate_value_t *value)
|
||||||
{
|
{
|
||||||
if (len == 3 && !strncmp(key, "age", len)) {
|
if (len == 3 && !strncmp(key, "age", len)) {
|
||||||
*type = TINYTEMPLATE_TYPE_INT;
|
tinytemplate_set_int(value, 24);
|
||||||
value->as_int = 24;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -117,8 +115,7 @@ the value `24` will be associated to the parameter `age`. All other parameters w
|
|||||||
|
|
||||||
The other callback `callback` is used by TinyTemplate to output the result of the evaluation. Instead of evaluating the result to a buffer and then returning a single pointer, it calls this function to notify the caller that some bytes should be written to output. This function usually writes the bytes provided by the library to a caller-owned buffer. Here's an example callback that redirects the evaluation result to `stdout`
|
The other callback `callback` is used by TinyTemplate to output the result of the evaluation. Instead of evaluating the result to a buffer and then returning a single pointer, it calls this function to notify the caller that some bytes should be written to output. This function usually writes the bytes provided by the library to a caller-owned buffer. Here's an example callback that redirects the evaluation result to `stdout`
|
||||||
```c
|
```c
|
||||||
void callback(void *data, const char *lbl, size_t lbllen,
|
void callback(void *data, const char *str, size_t len)
|
||||||
const char *str, size_t len)
|
|
||||||
{
|
{
|
||||||
fwrite(str, 1, len, stdout);
|
fwrite(str, 1, len, stdout);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user