Fixed minor memory leak.
[privoxy.git] / list.c
diff --git a/list.c b/list.c
index a44c0e9..bc5476b 100644 (file)
--- a/list.c
+++ b/list.c
@@ -1,4 +1,4 @@
-const char list_rcs[] = "$Id: list.c,v NOT CHECKED IN $";
+const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/list.c,v $
@@ -34,6 +34,79 @@ const char list_rcs[] = "$Id: list.c,v NOT CHECKED IN $";
  *
  * Revisions   :
  *    $Log: list.c,v $
+ *    Revision 1.3  2001/06/03 11:03:48  oes
+ *    Makefile/in
+ *
+ *    introduced cgi.c
+ *
+ *    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.2  2001/06/01 18:49:17  jongfoster
+ *    Replaced "list_share" with "list" - the tiny memory gain was not
+ *    worth the extra complexity.
+ *
+ *    Revision 1.1  2001/05/31 21:11:53  jongfoster
+ *    - Moved linked list support to new "list.c" file.
+ *      Structure definitions are still in project.h,
+ *      function prototypes are now in "list.h".
+ *    - Added support for "struct list_share", which is identical
+ *      to "struct list" except it saves memory by not duplicating
+ *      the strings.  Obviously, this only works if there is some
+ *      other way of managing the memory used by the strings.
+ *      (These list_share lists are used for lists which last
+ *      for only 1 request, and where all the list entries are
+ *      just coming directly from entries in the actionsfile.)
+ *      Note that you still need to destroy list_share lists
+ *      properly to free the nodes - it's only the strings
+ *      which are shared.
+ *
  *
  *********************************************************************/
 \f
@@ -93,28 +166,66 @@ void enlist(struct list *header, const char *str)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  enlist_first
+ *
+ * Description :  Append a string as first element into a specified
+ *                string list.
+ *
+ * Parameters  :
+ *          1  :  header = pointer to list 'dummy' header
+ *          2  :  str = string to add to the list (maybe NULL)
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+void enlist_first(struct list *header, const char *str)
+{
+   struct list *cur = (struct list *)malloc(sizeof(*cur));
+
+   if (cur)
+   {
+      cur->str  = (str ? strdup(str) : NULL);
+      cur->next = header->next;
+
+      header->next = cur;
+      if (header->last == NULL)
+      {
+         header->last = cur;
+      }
+   }
+
+}
+
+
 /*********************************************************************
  *
  * Function    :  enlist_unique
  *
  * Description :  Append a string into a specified string list,
  *                if & only if it's not there already.
+ *                If the n argument is nonzero, only compare up to
+ *                the nth character. 
  *
  * Parameters  :
  *          1  :  header = pointer to list 'dummy' header
  *          2  :  str = string to add to the list (maybe NULL)
+ *          3  :  n = number of chars to use for uniqueness test
  *
  * Returns     :  N/A
  *
  *********************************************************************/
-void enlist_unique(struct list *header, const char *str)
+void enlist_unique(struct list *header, const char *str, int n)
 {
    struct list *last;
    struct list *cur = header->next;
 
    while (cur != NULL)
    {
-      if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
+      if ((cur->str != NULL) && (
+         (n && (0 == strncmp(str, cur->str, n))) || 
+         (!n && (0 == strcmp(str, cur->str)))))
       {
          /* Already there */
          return;
@@ -336,7 +447,8 @@ void list_duplicate(struct list *dest, const struct list *src)
  *
  * Function    :  list_append_list_unique
  *
- * Description :  Append a string list to another list
+ * Description :  Append a string list to another list.
+ *                Duplicate items are not added.
  *
  * Parameters  :
  *          1  :  dest = pointer to destination for merge.  Caller allocs.
@@ -351,7 +463,7 @@ void list_append_list_unique(struct list *dest, const struct list *src)
 
    while (cur)
    {
-      enlist_unique(dest, cur->str);
+      enlist_unique(dest, cur->str, 0);
       cur = cur->next;
    }
 }
@@ -359,245 +471,101 @@ void list_append_list_unique(struct list *dest, const struct list *src)
 
 /*********************************************************************
  *
- * Function    :  destroy_list_share
+ * Function    :  map
  *
- * Description :  Destroy a string list (opposite of enlist)
+ * Description :  Add a mapping from given name to given value to a
+ *                given map.
  *
  * Parameters  :
- *          1  :  h = pointer to list 'dummy' header
+ *          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!)
+ *          4  :  value = value to add
+ *          5  :  vc = flag set if value is string constant
  *
- * Returns     :  N/A
+ * Returns     :  pointer to extended map, or NULL if failiure
  *
  *********************************************************************/
-void destroy_list_share(struct list_share *h)
+struct map *map(struct map *map, char *name, int nc, char *value, int vc)
 {
-   struct list_share *p, *n;
+   struct map *cur;
 
-   for (p = h->next; p ; p = n)
+   if (NULL == (cur = zalloc(sizeof(*cur))))
    {
-      n = p->next;
-      free(p);
+      return(NULL);
    }
 
-   memset(h, '\0', sizeof(*h));
+   cur->name  = nc ? strdup(name) : name;
+   cur->value = vc ? strdup(value) : value;
+   cur->next = map;
 
-}
-
-
-/*********************************************************************
- *
- * Function    :  enlist_share
- *
- * Description :  Append a string into a specified string list.
- *
- * Parameters  :
- *          1  :  header = pointer to list 'dummy' header
- *          2  :  str = string to add to the list (maybe NULL)
- *
- * Returns     :  N/A
- *
- *********************************************************************/
-void enlist_share(struct list_share *header, const char *str)
-{
-   struct list_share *cur = (struct list_share *)malloc(sizeof(*cur));
-   struct list_share *last;
-
-   if (cur)
-   {
-      cur->str  = (str ? strdup(str) : NULL);
-      cur->next = NULL;
-
-      last = header->last;
-      if (last == NULL)
-      {
-         last = header;
-      }
-
-      last->next   = cur;
-      header->last = cur;
-   }
+   return(cur);
 
 }
 
 
 /*********************************************************************
  *
- * Function    :  enlist_unique_share
+ * Function    :  lookup
  *
- * Description :  Append a string into a specified string list,
- *                if & only if it's not there already.
+ * Description :  Look up an item with a given name in a map, and
+ *                return its value
  *
  * Parameters  :
- *          1  :  header = pointer to list 'dummy' header
- *          2  :  str = string to add to the list (maybe NULL)
+ *          1  :  name = name parameter to look for
  *
- * Returns     :  N/A
+ * Returns     :  the value if found, else the empty string
  *
  *********************************************************************/
-void enlist_unique_share(struct list_share *header, const char *str)
+char *lookup(struct map *map, char *name)
 {
-   struct list_share *last;
-   struct list_share *cur = header->next;
+   struct map *p = map;
 
-   while (cur != NULL)
+   while (p)
    {
-      if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
+      if (!strcmp(name, p->name))
       {
-         /* Already there */
-         return;
+         return p->value;
       }
-      cur = cur->next;
-   }
-
-   cur = (struct list_share *)malloc(sizeof(*cur));
-
-   if (cur != NULL)
-   {
-      cur->str  = str;
-      cur->next = NULL;
-
-      last = header->last;
-      if (last == NULL)
-      {
-         last = header;
-      }
-      last->next   = cur;
-      header->last = cur;
+      p = p->next;
    }
+   return "";
 }
 
 
 /*********************************************************************
  *
- * Function    :  list_append_list_unique_share
+ * Function    :  free_map
  *
- * Description :  Append a string list to another list
+ * Description :  Free the memory occupied by a map and its
+ *                depandant strings
  *
  * Parameters  :
- *          1  :  dest = pointer to destination for merge.  Caller allocs.
- *          2  :  src = pointer to source for merge.
+ *          1  :  list = list to bee freed
  *
  * Returns     :  N/A
  *
  *********************************************************************/
-void list_append_list_unique_share(struct list_share *dest, const struct list *src)
+void free_map(struct map *map)
 {
-   struct list * cur = src->next;
+   struct map *p = map;
 
-   while (cur)
+   while (p)
    {
-      enlist_unique_share(dest, cur->str);
-      cur = cur->next;
-   }
-}
-
-
-/*********************************************************************
- *
- * Function    :  list_remove_item_share
- *
- * Description :  Remove a string from a specified string list.
- *
- * Parameters  :
- *          1  :  header = pointer to list 'dummy' header
- *          2  :  str = string to remove from the list
- *
- * Returns     :  Number of times it was removed.
- *
- *********************************************************************/
-int list_remove_item_share(struct list_share *header, const char *str)
-{
-   struct list_share *prev = header;
-   struct list_share *cur = prev->next;
-   int count = 0;
-
-   while (cur != NULL)
-   {
-      if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
-      {
-         count++;
-
-         prev->next = cur->next;
-         free(cur);
-      }
-      else
-      {
-         prev = cur;
-      }
-      cur = prev->next;
-   }
-
-   header->last = prev;
-
-   return count;
-}
-
-
-/*********************************************************************
- *
- * Function    :  list_remove_list_share
- *
- * Description :  Remove all strings in one list from another list.
- *                This is currently a brute-force algorithm
- *                (it compares every pair of strings).
- *
- * Parameters  :
- *          1  :  dest = list to change
- *          2  :  src = list of strings to remove
- *
- * Returns     :  Total number of strings removed.
- *
- *********************************************************************/
-int list_remove_list_share(struct list_share *dest, const struct list *src)
-{
-   struct list *cur = src->next;
-   int count = 0;
+      free(p->name);
+      free(p->value);
 
-   while (cur != NULL)
-   {
-      if (cur->str != NULL)
-      {
-         count += list_remove_item_share(dest, cur->str);
-      }
-      cur = cur->next;
+      map = p->next;
+      free(p);
+      p = map;
    }
 
-   return count;
 }
 
 
-/*********************************************************************
- *
- * Function    :  list_duplicate_share
- *
- * Description :  Duplicate a string list
- *
- * Parameters  :
- *          1  :  dest = pointer to destination for copy.  Caller allocs.
- *          2  :  src = pointer to source for copy.
- *
- * Returns     :  N/A
- *
- *********************************************************************/
-void list_duplicate_share(struct list_share *dest, const struct list *src)
-{
-   struct list * cur_src = src->next;
-   struct list_share * cur_dest = dest;
-
-   memset(dest, '\0', sizeof(*dest));
-
-   while (cur_src)
-   {
-      cur_dest = cur_dest->next = (struct list_share *)zalloc(sizeof(*cur_dest));
-      if (cur_dest == NULL)
-      {
-         return;
-      }
-      cur_dest->str = cur_src->str;
-      cur_src = cur_src->next;
-   }
-
-   dest->last = cur_dest;
-
-}
-
+/*
+  Local Variables:
+  tab-width: 3
+  end:
+*/