.Dd October 1, 2000 .Dt sf_fmt 3 .Os .Sh NAME .Nm format_init , .Nm format_free , .Nm format_metarule , .Nm formatf , .Nm format_lastresult , .Nm format_lastsize , .Nm format_detach .Nd template formatting functions. .Sh SYNOPSIS .Fd #include .Pp .Ft fmt_base * .Fn format_init .Ft void .Fn format_free "fmt_base *base" .Ft int .Fn format_metarule "fmt_base *base" "char leftBrace" "char rightBrace" "char **(*function)(char *found, void *optKeyPassed)" .Ft char * .Fn formatf "fmt_base *base" "char *template" "void *optKeyToPass" .Ft char * .Fn format_lastresult "fmt_base *base" "size_t *optSize" .Ft size_t .Fn format_lastsize "fmt_base *base" .Ft char * .Fn format_detach "fmt_base *base" "size_t *optReturnSize" .Sh DESCRIPTION It is widely used for programs to read a template and display the data according to it. .Pp 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. .Xr strfunc 3 library will automagically do the dirty work for you. .Pp The internal language of defining the template is powerful yet simple. Before we run into detailed explanation, let's talk about programming. .Pp First, programmer needed to initialize the formatting engine by defining the formatting rules. Formatting rules are stored inside the special structure .Ar fmt_base . .Fn format_init creates an empty structure and returns the pointer to it. One may define a numerous formatting bases and randomly use them. Formatting base may be freed using .Fn format_free "fmt_base *" . Please note that .Fn format_init will never return the NULL pointer. .Pp Second, there is a need to fill this empty structure with some formatting rules using the .Fn format_metarule function. Formatting rules are defined by specifiyng a braces and the function which will handle the data inside the braces. .Pp Say, we have a template "abc${def}ghi". Programmer should call the .Fn format_metarule "fb" "'{'" "'}'" "handler" , where .Ar fb is the pointer to the formatting base returned by .Fn format_init . If two braces are equal, the result is undefined. .Ar handler is the function which should be defined as .Pp .Ft char ** .Fn handler "char *found" "void *optKeyPassed" . .Pp It is possible to define a number of such a rules for the single .Ar fmt_base . The handler function should return the pointer to the internal .Ar 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. .Pp When you're finished to fill the .Ar fmt_base with the rules, the function .Fn formatf can be used multiple times to convert the specified template to the destination text. The optional .Ar optKeyToPass argument may be specified to pass some additional data, if necessary. .Pp .Fn formatf will place the output to the buffer located in the .Ar fmt_base and return a pointer to it. The pointer will never be NULL, and is not to be freed. If you need this buffer to be completely yours and do not want to .Xr strdup 3 it, there is a function called .Fn format_detach "fmt_base *" "size_t *optReturnSize" to achive this. .Pp Two other functions, .Ft char * .Fn format_lastresult "fmt_base *" "size_t *optSize" and .Ft size_t .Fn format_lastsize "fmt_base *" , are to be used to obtain the pointer to the buffer and its size without invoking .Fn formatf once more. .Sh ACTIVE TEMPLATE DEFINITION If a template is not containing the special tokens it will considered as plain text and returned unmodified. .Pp Special token and the whole template are defined in the following BNF: .Bd -literal -offset indent