Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

template.c

Go to the documentation of this file.
00001 
00007 /* {{{ Initial headers */
00008 /*
00009  * $LastChangedDate: 2003-11-27 01:55:17 +0100 (Thu, 27 Nov 2003) $
00010  * $LastChangedRevision: 5 $
00011  * $LastChangedBy: ckruse $
00012  *
00013  */
00014 /* }}} */
00015 
00016 /* {{{ Includes */
00017 #include "config.h"
00018 #include "defines.h"
00019 
00020 #include <dlfcn.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <time.h>
00024 
00025 #include <string.h>
00026 
00027 #include "utils.h"
00028 #include "hashlib.h"
00029 #include "template.h"
00030 /* }}} */
00031 
00032 #ifndef DOXYGEN
00033 /*
00034  * Returns:          nothing
00035  * Parameters:
00036  *   - void *data    the entry data
00037  *
00038  * this function cleans up hash entry
00039  *
00040  */
00041 void tpl_cf_cleanup_var(void *data) {
00042   t_cf_tpl_variable *v = (t_cf_tpl_variable *)data;
00043 
00044   str_cleanup(v->data);
00045   free(v->data);
00046 }
00047 #endif
00048 
00049 /*
00050  * Returns: nothing
00051  * Parameters:
00052  *   - const u_char *file      the absolute path to the file
00053  *   - t_cf_template *tpl    a pointer to the template variable
00054  *
00055  * this function binds a template lib
00056  *
00057  */
00058 int tpl_cf_init(t_cf_template *tpl,const u_char *fname) {
00059   if((tpl->tpl = dlopen(fname,RTLD_LAZY)) == NULL) {
00060     fprintf(stderr,"%s\n",dlerror());
00061     return -1;
00062   }
00063 
00064   str_init(&tpl->parsed);
00065 
00066   tpl->varlist = cf_hash_new(tpl_cf_cleanup_var);
00067 
00068   return 0;
00069 }
00070 
00071 /*
00072  * Returns: nothing
00073  * Parameters:
00074  *   - t_cf_template *tpl    a pointer to the template variable
00075  *   - const u_char *name     the name of the variable
00076  *   - const u_char *val      the value of the variable
00077  *   - int len               the length of the content in val
00078  *
00079  * this function sets a variable value
00080  *
00081  */
00082 void tpl_cf_setvar(t_cf_template *tpl,u_char *vname,const u_char *value,int len,int escapehtml) {
00083   t_cf_tpl_variable var;
00084 
00085   var.data = fo_alloc(NULL,1,sizeof(t_string),FO_ALLOC_CALLOC);
00086   var.escape_html = escapehtml;
00087 
00088   str_char_set(var.data,value,len);
00089 
00090   cf_hash_set(tpl->varlist,vname,strlen(vname),&var,sizeof(t_cf_tpl_variable));
00091 }
00092 
00093 /*
00094  * Returns: nothing
00095  * Parameters:
00096  *   - t_cf_template *tpl    a pointer to the template variable
00097  *   - const u_char *name     the name of the variable
00098  *   - const u_char *val      the value of the variable
00099  *   - int len               the length of the content in val
00100  *
00101  * this function appends a value to a variable
00102  *
00103  */
00104 int tpl_cf_appendvar(t_cf_template *tpl,u_char *vname,const u_char *value,int len) {
00105   t_cf_tpl_variable *var = (t_cf_tpl_variable *)cf_hash_get(tpl->varlist,vname,strlen(vname));
00106 
00107   if(var) {
00108     str_chars_append(var->data,value,len);
00109     return 0;
00110   }
00111 
00112   return -1;
00113 }
00114 
00115 /*
00116  * Returns: nothing
00117  * Parameters:
00118  *   - t_cf_template *tpl    a pointer to the template variable
00119  *   - const u_char *name     the name of the variable
00120  *
00121  * this function frees a template variable
00122  *
00123  */
00124 void tpl_cf_freevar(t_cf_template *tpl,u_char *vname) {
00125   cf_hash_entry_delete(tpl->varlist,vname,strlen(vname));
00126 }
00127 
00128 /*
00129  * Returns: nothing
00130  * Parameters:
00131  *   - t_cf_template *tpl    a pointer to the template variable
00132  *
00133  * this function starts the parsing process
00134  *
00135  */
00136 void tpl_cf_parse(t_cf_template *tpl) {
00137   void *pa = dlsym(tpl->tpl,"parse");
00138 
00139   if(pa) {
00140     t_parse x = (t_parse)pa;
00141     x(tpl);
00142   }
00143 }
00144 
00145 /*
00146  * Returns: nothing
00147  * Parameters:
00148  *   - t_cf_template *tpl    a pointer to the template variable
00149  *
00150  * this function starts the parsing process and spits it out
00151  * to memory
00152  *
00153  */
00154 void tpl_cf_parse_to_mem(t_cf_template *tpl) {
00155   void *pa = dlsym(tpl->tpl,"parse_to_mem");
00156 
00157   if(pa) {
00158     t_parse_mem x = (t_parse_mem)pa;
00159     x(tpl);
00160   }
00161 }
00162 
00163 /*
00164  * Returns: nothing
00165  * Parameters:
00166  *   - t_cf_template *tpl    a pointer to the template variable
00167  *
00168  * this function frees the internal template structures
00169  *
00170  */
00171 void tpl_cf_finish(t_cf_template *tpl) {
00172   dlclose(tpl->tpl);
00173   str_cleanup(&tpl->parsed);
00174   cf_hash_destroy(tpl->varlist);
00175 }
00176 
00177 /*
00178  * Returns: t_cf_variable *  a pointer to the variable
00179  * Parameters:
00180  *   - t_cf_template *tpl    a pointer to the template variable
00181  *   - const u_char *name     the variable name
00182  *
00183  * this function searches a template variable and returns it
00184  * if not found, it returns NULL
00185  *
00186  */
00187 const t_cf_tpl_variable *tpl_cf_getvar(t_cf_template *tpl,u_char *name) {
00188   t_cf_tpl_variable *var = (t_cf_tpl_variable *)cf_hash_get(tpl->varlist,name,strlen(name));
00189 
00190   return (const t_cf_tpl_variable *)var;
00191 }
00192 
00193 /* eof */

Generated on Sun Apr 25 16:37:39 2004 for Classic Forum by doxygen 1.3.5