|
NAMEformat_init , format_free ,
format_metarule , formatf ,
format_lastresult ,
format_lastsize , format_detach
—
template formatting functions.
SYNOPSIS#include <strfunc.h>
void
int
char *
char *
size_t
char *
DESCRIPTIONIt is widely used for programs to read a template and display the data according to it.Those functions forms a powerful solution to work with a kind of "active" templates. The term "active" means that some operations, like a test of existence of a keyword, or equality tests, can be placed directly to the template and no special handling needed in an application to support it. strfunc(3) library will automagically do the dirty work for you. The internal language of defining the template is powerful yet simple. Before we run into detailed explanation, let's talk about programming. First, programmer needed to initialize the formatting engine by
defining the formatting rules. Formatting rules are stored inside the
special structure fmt_base.
Second, there is a need to fill this empty structure with some
formatting rules using the Say, we have a template "abc${def}ghi". Programmer
should call the
char **
It is possible to define a number of such a rules for the single fmt_base. The handler function should return the pointer to the internal char ** data when it is possible, and NULL pointer otherwise. Please NOTE that this pointer is never modified or freed and probably it should be the semi-static structure inside the handler function. When you're finished to fill the fmt_base
with the rules, the function
Two other functions, char *
ACTIVE TEMPLATE DEFINITIONIf a template is not containing the special tokens it will considered as plain text and returned unmodified.Special token and the whole template are defined in the following BNF: <template> := *(*<string> *<token> *<string>) <token> := <simple> | <join> | <index> | <choice> | <equality> <simple> := '$' <LB> <param> <RB> <join> := '$' <LB> <param> '+' <delimiter> <RB> <index> := '$' <LB> <param> '[' <number> ']' <RB> <choice> := '$' <LB> <param> '?' <iftrue> ':' <iffalse> <RB> <equality> := '$' <LB> <param> { "==" | "!=" } *<string> '?' <iftrue> ':' <iffalse> <RB> <LB> := <the second argument of format_metarule()> <RB> := <the third argument of format_metarule()> <param> := <string> <delimiter> := <string> <iftrue> := <template> <iffalse> := <template> <string> := *<CHAR> <number> := 1*<character from '0' to '9'> The word param defined above, will be passed as the first argument to the handler function. ACTIVE TEMPLATE EXAMPLEThe following is an example of the typical template. It may be placed to a file, then read and passed toformatf (). It is also can be
defined as the argument's value within the configuration file read by
cfgread(3).
Login: ${login[0]} Password: ${password} Username: ${name?${name}:Unknown name} Comments: ${comment+, } $<status==Busy?User is busy> You can see the index token "${login[0]}", the simple token "${password}", the join token right after the "Comments: ", and equality token is the whole last string. Please note that the last token is formed using the angle braces: you should specify an additional handler function to handle this case. Refer to the PROGRAMMING EXAMPLE section.
PROGRAMMING EXAMPLEHere's how to implement the above template example's parsing.#include <strfunc.h> char **handler1(char *found, void *optKeyPassed); char **handler2(char *found, void *optKeyPassed); int main() { fmt_base *fb; char *template = "Login: ${login[0]}\nPassword: ${password}\nUsername: ${name?${name}:Unknown name}\nComments: ${comment+, }\n$<status==Busy?User is busy>\n"; char *s; /* Create empty structure */ fb = format_init(); /* Add one formatting rule */ format_metarule(fb, '{', '}', handler1); /* Add another formatting rule to parse angle braces */ format_metarule(fb, '<', '>', handler1); /* Format the template */ s = formatf(fb, template, NULL); /* Print out the result */ printf("%s", s); /* Free the formatting structure */ format_free(fb); return 0; }; char ** handler1(char *found, void *optKeyPassed) { static char *arr[3] = { NULL, NULL, NULL }; (void)optKeyPassed; arr[1] = NULL; if(strcasecmp(found, "login") == 0) { arr[0] = "john"; return arr; }; if(strcasecmp(found, "name") == 0) { arr[0] = "John Smith"; return arr; }; if(strcasecmp(found, "password") == 0) { arr[0] = "123"; return arr; }; if(strcasecmp(found, "comment") == 0) { arr[0] = "Comment value #1"; arr[1] = "Comment value #2"; return arr; }; return NULL; }; char ** handler2(char *found, void *optKeyPassed) { static char *arr[] = { NULL, NULL }; (void)optKeyPassed; if(strcasecmp(found, status) == 0) { arr[0] = "busy"; return arr; }; return NULL; }; SEE ALSOstrfunc(3), cfgread(3).AUTHORSLev Walkin <vlm@lionet.info>
Visit the GSP FreeBSD Man Page Interface. |