Changes to fill_template() that reduce memory usage without having
[privoxy.git] / list.c
diff --git a/list.c b/list.c
index bc5476b..255d6fd 100644 (file)
--- a/list.c
+++ b/list.c
@@ -1,4 +1,4 @@
-const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 oes Exp $";
+const char list_rcs[] = "$Id: list.c,v 1.5 2001/06/29 21:45:41 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/list.c,v $
@@ -34,59 +34,19 @@ const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 oes Exp $";
  *
  * Revisions   :
  *    $Log: list.c,v $
- *    Revision 1.3  2001/06/03 11:03:48  oes
- *    Makefile/in
+ *    Revision 1.5  2001/06/29 21:45:41  oes
+ *    Indentation, CRLF->LF, Tab-> Space
  *
- *    introduced cgi.c
+ *    Revision 1.4  2001/06/29 13:30:22  oes
+ *    - Added Convenience function enlist_unique_header(),
+ *      which takes the Header name and value as separate
+ *      arguments and thus saves the pain of sprintf()ing
+ *      and determining the Header name length to enlist_unique
+ *    - Improved comments
+ *    - Removed logentry from cancelled commit
  *
- *    actions.c:
- *
- *    adapted to new enlist_unique arg format
- *
- *    conf loadcfg.c
- *
- *    introduced confdir option
- *
- *    filters.c filtrers.h
- *
- *     extracted-CGI relevant stuff
- *
- *    jbsockets.c
- *
- *     filled comment
- *
- *    jcc.c
- *
- *     support for new cgi mechansim
- *
- *    list.c list.h
- *
- *    functions for new list type: "map"
- *    extended enlist_unique
- *
- *    miscutil.c .h
- *    introduced bindup()
- *
- *    parsers.c parsers.h
- *
- *    deleted const struct interceptors
- *
- *    pcrs.c
- *    added FIXME
- *
- *    project.h
- *
- *    added struct map
- *    added struct http_response
- *    changes struct interceptors to struct cgi_dispatcher
- *    moved HTML stuff to cgi.h
- *
- *    re_filterfile:
- *
- *    changed
- *
- *    showargs.c
- *    NO TIME LEFT
+ *    Revision 1.3  2001/06/03 19:12:24  oes
+ *    functions for new struct map, extended enlist_unique
  *
  *    Revision 1.2  2001/06/01 18:49:17  jongfoster
  *    Replaced "list_share" with "list" - the tiny memory gain was not
@@ -251,6 +211,64 @@ void enlist_unique(struct list *header, const char *str, int n)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  enlist_unique_header
+ *
+ * Description :  Make a HTTP header from the two strings name and value,
+ *                and append the result into a specified string list,
+ *                if & only if there isn't already a header with that name.
+ *
+ * Parameters  :
+ *          1  :  header = pointer to list 'dummy' header
+ *          2  :  first = first string to add to the list (maybe NULL)
+ *          3  :  second = number of chars to use for uniqueness test
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+void enlist_unique_header(struct list *header, const char *name, const char *value)
+{
+   struct list *last;
+   struct list *cur = header->next;
+   int length;
+   char *dummy;
+
+   if (name == NULL || value == NULL) return;
+
+   dummy = strdup(name);
+   dummy = strsav(dummy, ": ");
+   length = strlen(dummy);
+
+   while (cur != NULL)
+   {
+      if ((cur->str != NULL) && 
+               (0 == strncmp(dummy, cur->str, length)))
+      {
+         /* Already there */
+         return;
+      }
+      cur = cur->next;
+   }
+
+   cur = (struct list *)malloc(sizeof(*cur));
+
+   if (cur != NULL)
+   {
+      cur->str  = strsav(dummy, value);
+      cur->next = NULL;
+
+      last = header->last;
+      if (last == NULL)
+      {
+         last = header;
+      }
+      last->next   = cur;
+      header->last = cur;
+   }
+}
+
+
 /*********************************************************************
  *
  * Function    :  destroy_list
@@ -283,7 +301,8 @@ void destroy_list(struct list *header)
  *
  * Function    :  list_to_text
  *
- * Description :  "Flaten" a string list into 1 long \r\n delimited string.
+ * Description :  "Flaten" a string list into 1 long \r\n delimited string,
+ *                adding an empty line at the end.
  *
  * Parameters  :
  *          1  :  h = pointer to list 'dummy' header
@@ -296,9 +315,7 @@ char *list_to_text(struct list *h)
    struct list *p;
    char *ret = NULL;
    char *s;
-   int size;
-
-   size = 0;
+   int size = 2;
 
    for (p = h->next; p ; p = p->next)
    {
@@ -326,6 +343,7 @@ char *list_to_text(struct list *h)
          *s++ = '\r'; *s++ = '\n';
       }
    }
+   *s++ = '\r'; *s++ = '\n';
 
    return(ret);
 
@@ -476,13 +494,16 @@ void list_append_list_unique(struct list *dest, const struct list *src)
  * Description :  Add a mapping from given name to given value to a
  *                given map.
  *
+ *                Note: Since all strings will be free()d in free_map()
+ *                      later, use the copy flags for constants or
+ *                      strings that will be independantly free()d.
+ *
  * Parameters  :
  *          1  :  map = map to add to
  *          2  :  name = name to add
- *          3  :  nc = flag set if name is string constant, and
- *                must be strdup()d, so it can later be free()d (FIXME!)
+ *          3  :  nc = flag set if a copy of name should be used
  *          4  :  value = value to add
- *          5  :  vc = flag set if value is string constant
+ *          5  :  vc = flag set if a copy of value should be used
  *
  * Returns     :  pointer to extended map, or NULL if failiure
  *
@@ -531,6 +552,7 @@ char *lookup(struct map *map, char *name)
       p = p->next;
    }
    return "";
+
 }