X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=list.c;h=28552d80afe7bac4030277dffa22686909140b43;hp=bc5476b970cc9d9636e286186ff84b105111d13c;hb=3939d3eae875607c76d7bb3314ff6ca6d0590f40;hpb=1e1eb52d61365adba05cbda21276be50767a7507 diff --git a/list.c b/list.c index bc5476b9..28552d80 100644 --- 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.7 2001/08/05 16:06:20 jongfoster Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/list.c,v $ @@ -34,59 +34,29 @@ 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 - * - * 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.7 2001/08/05 16:06:20 jongfoster + * Modifiying "struct map" so that there are now separate header and + * "map_entry" structures. This means that functions which modify a + * map no longer need to return a pointer to the modified map. + * Also, it no longer reverses the order of the entries (which may be + * important with some advanced template substitutions). + * + * Revision 1.6 2001/07/31 14:44:51 oes + * list_to_text() now appends empty line at end + * + * Revision 1.5 2001/06/29 21:45:41 oes + * Indentation, CRLF->LF, Tab-> Space + * + * 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 + * + * 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 @@ -119,6 +89,8 @@ const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 oes Exp $"; #include #include +#include + #ifndef _WIN32 #include #endif @@ -130,6 +102,7 @@ const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 oes Exp $"; const char list_h_rcs[] = LIST_H_VERSION; + /********************************************************************* * * Function : enlist @@ -251,6 +224,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 : name = name of header to be added + * 3 : value = value of header to be added + * + * 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 +314,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 +328,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 +356,7 @@ char *list_to_text(struct list *h) *s++ = '\r'; *s++ = '\n'; } } + *s++ = '\r'; *s++ = '\n'; return(ret); @@ -476,31 +507,41 @@ 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 + * 1 : the_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 + * Returns : N/A * *********************************************************************/ -struct map *map(struct map *map, char *name, int nc, char *value, int vc) +void map(struct map *the_map, const char *name, int nc, const char *value, int vc) { - struct map *cur; + struct map_entry *new_entry; - if (NULL == (cur = zalloc(sizeof(*cur)))) + if (NULL == (new_entry = zalloc(sizeof(*new_entry)))) { - return(NULL); + return; } - cur->name = nc ? strdup(name) : name; - cur->value = vc ? strdup(value) : value; - cur->next = map; + new_entry->name = nc ? strdup(name) : name; + new_entry->value = vc ? strdup(value) : value; + /* new_entry->next = NULL; - implied by zalloc */ - return(cur); + if (the_map->last) + { + the_map->last = the_map->last->next = new_entry; + } + else + { + the_map->last = the_map->first = new_entry; + } } @@ -518,22 +559,39 @@ struct map *map(struct map *map, char *name, int nc, char *value, int vc) * Returns : the value if found, else the empty string * *********************************************************************/ -char *lookup(struct map *map, char *name) +const char *lookup(const struct map *the_map, const char *name) { - struct map *p = map; + const struct map_entry *cur_entry = the_map->first; - while (p) + while (cur_entry) { - if (!strcmp(name, p->name)) + if (!strcmp(name, cur_entry->name)) { - return p->value; + return cur_entry->value; } - p = p->next; + cur_entry = cur_entry->next; } return ""; } +/********************************************************************* + * + * Function : new_nap + * + * Description : Create a new, empty map. + * + * Parameters : + * + * Returns : A new, empty map, or NULL if out of memory. + * + *********************************************************************/ +struct map *new_map(void) +{ + return (struct map *) zalloc(sizeof(struct map)); +} + + /********************************************************************* * * Function : free_map @@ -542,25 +600,33 @@ char *lookup(struct map *map, char *name) * depandant strings * * Parameters : - * 1 : list = list to bee freed + * 1 : cur_entry = map to be freed. May be NULL. * * Returns : N/A * *********************************************************************/ -void free_map(struct map *map) +void free_map(struct map *the_map) { - struct map *p = map; + struct map_entry *cur_entry; + struct map_entry *next_entry; - while (p) + if (the_map == NULL) { - free(p->name); - free(p->value); + return; + } - map = p->next; - free(p); - p = map; + for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry) + { + freez((char *)cur_entry->name); + freez((char *)cur_entry->value); + + next_entry = cur_entry->next; + free(cur_entry); } + the_map->first = the_map->last = NULL; + + free(the_map); }