/* Copyright 2005 Garrett Rooney. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ETL_TEMPLATE_H #define ETL_TEMPLATE_H #include #include #include #include #define ETL_TEMPLATE_MAJOR_VERSION 0 #define ETL_TEMPLATE_MINOR_VERSION 1 #define ETL_TEMPLATE_PATCH_VERSION 0 #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /* the types of variables that can appear in a template. */ typedef enum { ETL_TEMPLATE_SCALAR, /* -> char * */ ETL_TEMPLATE_ARRAY, /* -> apr_array_header_t of etl_template_variable_t's */ ETL_TEMPLATE_HASH /* -> apr_hash_t of char * to variable */ } etl_template_variable_type_t; /* a variable that can be accessed inside a template. */ typedef struct { etl_template_variable_type_t type; union { char *s; apr_array_header_t *a; apr_hash_t *h; } contents; } etl_template_variable_t; /* note: the following helper functions are TOTALLY OPTIONAL, in fact there * are situations (usually with the int and string versions) where you * DON'T want to use them because they result in excess copying, but * i find myself writing them over and over again, so here they are. */ /* helper function to turn an int into a template variable. the * variable is allocated in POOL. */ etl_template_variable_t * etl_template_make_int (int i, apr_pool_t *pool); /* helper function to turn a string STR into a template variable. * the variable is allocated in POOL. */ etl_template_variable_t * etl_template_make_str (const char *str, apr_pool_t *pool); /* helper function to create a template variable wrapped around an * empty array. the array is initially sized to hold NITEMS and is * allocated in POOL. */ etl_template_variable_t * etl_template_make_array (int nitems, apr_pool_t *pool); /* helper function to create a template variable wrapped around an * empty hash. the array is allocated in POOL. */ etl_template_variable_t * etl_template_make_hash (apr_pool_t *pool); /* a parsed template. */ typedef struct etl_template_t etl_template_t; /* parse something that looks like this from IN: * * [% for x in array %] * [% print x %] * [% end %] * * [% if boolean %] * literal text turns into a print * [% else %] * other literal text in the other case * [% end %] * * return a template in *TMPL, allocated in POOL. */ apr_status_t etl_template_parse (etl_template_t **tmpl, apr_bucket_brigade *in, apr_pool_t *pool); /* parse a template out of the file FNAME, returning it in *TMPL and * allocating it in POOL. */ apr_status_t etl_template_parse_file (etl_template_t **tmpl, const char *fname, apr_pool_t *pool); /* execute TMPL in the context of ENVIRONMENT sending output to OUT and * using POOL for temporary allocation. * * ENVIRONMENT is a hash mapping char *'s to etl_template_variable_t *'s. */ apr_status_t etl_template_execute (etl_template_t *tmpl, apr_hash_t *environment, apr_bucket_brigade *out, apr_pool_t *pool); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* ETL_TEMPLATE_H */