From: jongfoster Date: Wed, 1 Aug 2001 21:33:18 +0000 (+0000) Subject: Changes to fill_template() that reduce memory usage without having X-Git-Tag: v_2_9_9~161 X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=commitdiff_plain;h=fa3f696c4bfed2ee296f589ad6fb1eb505304ff1 Changes to fill_template() that reduce memory usage without having an impact on performance. I also renamed some variables so as not to clash with the C++ keywords "new" and "template". --- diff --git a/cgi.c b/cgi.c index 83bd2cb7..77a7e6c2 100644 --- a/cgi.c +++ b/cgi.c @@ -1,4 +1,4 @@ -const char cgi_rcs[] = "$Id: cgi.c,v 1.14 2001/08/01 00:19:03 jongfoster Exp $"; +const char cgi_rcs[] = "$Id: cgi.c,v 1.15 2001/08/01 21:19:22 jongfoster Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $ @@ -36,6 +36,9 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.14 2001/08/01 00:19:03 jongfoster Exp $"; * * Revisions : * $Log: cgi.c,v $ + * Revision 1.15 2001/08/01 21:19:22 jongfoster + * Moving file version information to a separate CGI page. + * * Revision 1.14 2001/08/01 00:19:03 jongfoster * New function: map_conditional() for an if-then-else syntax. * Changing to use new version of show_defines() @@ -899,20 +902,22 @@ void free_http_response(struct http_response *rsp) * Returns : char * with filled out form, or NULL if failiure * *********************************************************************/ -char *fill_template(struct client_state *csp, const char *template, struct map *exports) +char *fill_template(struct client_state *csp, const char *templatename, struct map *exports) { struct map *m; - pcrs_job *job, *joblist = NULL; + pcrs_job *job; char buf[BUFFER_SIZE]; - char *new, *old = NULL; + char *tmp_out_buffer; + char *file_buffer = NULL; int size; + int error; FILE *fp; /* * Open template file or fail */ - snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template); + snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, templatename); if(NULL == (fp = fopen(buf, "r"))) { @@ -921,29 +926,6 @@ char *fill_template(struct client_state *csp, const char *template, struct map * } - /* - * Assemble pcrs joblist from exports map - */ - for (m = exports; m; m = m->next) - { - int error; - - /* Enclose name in @@ */ - snprintf(buf, BUFFER_SIZE, "@%s@", m->name); - - /* Make and chain job */ - if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) ) - { - log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error); - } - else - { - job->next = joblist; - joblist = job; - } - } - - /* * Read the file, ignoring comments */ @@ -952,7 +934,7 @@ char *fill_template(struct client_state *csp, const char *template, struct map * /* skip lines starting with '#' */ if(*buf == '#') continue; - old = strsav(old, buf); + file_buffer = strsav(file_buffer, buf); } fclose(fp); @@ -960,22 +942,38 @@ char *fill_template(struct client_state *csp, const char *template, struct map * /* * Execute the jobs */ - size = strlen(old) + 1; - new = old; + size = strlen(file_buffer) + 1; - for (job = joblist; NULL != job; job = job->next) + /* + * Assemble pcrs joblist from exports map + */ + for (m = exports; m != NULL; m = m->next) { - pcrs_execute(job, old, size, &new, &size); - if (old != buf) free(old); - old = new; + /* Enclose name in @@ */ + snprintf(buf, BUFFER_SIZE, "@%s@", m->name); + + /* Make and run job */ + if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) ) + { + log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error); + } + else + { + pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size); + if (file_buffer != tmp_out_buffer) + { + free(file_buffer); + file_buffer = tmp_out_buffer; + } + pcrs_free_job(job); + } } /* - * Free the jobs & return + * Return */ - pcrs_free_joblist(joblist); - return(new); + return(file_buffer); }