1 /* HTAssoc.c
2 ** ASSOCIATION LIST FOR STORING NAME-VALUE PAIRS.
3 **
4 ** (c) COPYRIGHT MIT 1995.
5 ** Please first read the full copyright statement in the file COPYRIGH.
6 ** @(#) $Id$
7 **
8 ** NAMES NOT CASE SENSITIVE, AND ONLY COMMON LENGTH
9 ** IS CHECKED (allows abbreviations; well, length is
10 ** taken from lookup-up name, so if table contains
11 ** a shorter abbrev it is not found).
12 ** AUTHORS:
13 ** AL Ari Luotonen luotonen@dxcern.cern.ch
14 ** HFN Henrik Frystyk frystyk@w3.org
15 **
16 ** HISTORY:
17 **
18 **
19 ** BUGS:
20 **
21 **
22 */
23
24 /* Library include files */
25 #include "wwwsys.h"
26 #include "HTUtils.h"
27 #include "HTString.h"
28 #include "HTAssoc.h" /* Implemented here */
29
HTAssocList_new(void)30 PUBLIC HTAssocList * HTAssocList_new (void)
31 {
32 return HTList_new();
33 }
34
HTAssocList_delete(HTAssocList * list)35 PUBLIC BOOL HTAssocList_delete (HTAssocList * list)
36 {
37 if (list) {
38 HTAssocList *cur = list;
39 HTAssoc *assoc;
40 while (NULL != (assoc = (HTAssoc*)HTList_nextObject(cur))) {
41 HT_FREE(assoc->name);
42 HT_FREE(assoc->value);
43 HT_FREE(assoc);
44 }
45 return HTList_delete(list);
46 }
47 return NO;
48 }
49
HTAssocList_addObject(HTAssocList * list,const char * name,const char * value)50 PUBLIC BOOL HTAssocList_addObject (HTAssocList * list,
51 const char * name, const char * value)
52 {
53 if (list && name) {
54 HTAssoc * assoc;
55 if ((assoc = (HTAssoc *) HT_CALLOC(1, sizeof(HTAssoc))) == NULL)
56 HT_OUTOFMEM("HTAssoc_add");
57 StrAllocCopy(assoc->name, name);
58 if (value) StrAllocCopy(assoc->value, value);
59 return HTList_addObject(list, (void *) assoc);
60 } else {
61 HTTRACE(UTIL_TRACE, "HTAssoc_add: ERROR: assoc list NULL!!\n");
62 }
63 return NO;
64 }
65
HTAssocList_replaceObject(HTAssocList * list,const char * name,const char * value)66 PUBLIC BOOL HTAssocList_replaceObject (HTAssocList * list,
67 const char * name, const char * value)
68 {
69 if (list && name) {
70 HTAssocList * cur = list;
71 HTAssoc * assoc;
72 int len = strlen(name);
73 while ((assoc = (HTAssoc *) HTList_nextObject(cur))) {
74 if (!strncasecomp(assoc->name, name, len)) {
75 StrAllocCopy(assoc->name, name);
76 if (value) StrAllocCopy(assoc->value, value);
77 return YES;
78 }
79 }
80 return HTAssocList_addObject(list, name, value);
81 }
82 return NO;
83 }
84
HTAssocList_findObject(HTAssocList * list,const char * name)85 PUBLIC char * HTAssocList_findObject (HTAssocList * list, const char * name)
86 {
87 if (list && name) {
88 HTAssocList * cur = list;
89 HTAssoc * assoc;
90 int len = strlen(name);
91 while ((assoc = (HTAssoc *) HTList_nextObject(cur))) {
92 if (!strncasecomp(assoc->name, name, len))
93 return assoc->value;
94 }
95 }
96 return NULL;
97 }
98
HTAssocList_findObjectExact(HTAssocList * list,const char * name)99 PUBLIC char * HTAssocList_findObjectExact (HTAssocList * list, const char * name)
100 {
101 if (list && name) {
102 HTAssocList * cur = list;
103 HTAssoc * assoc;
104 while ((assoc = (HTAssoc *) HTList_nextObject(cur))) {
105 if (!strcasecomp(assoc->name, name))
106 return assoc->value;
107 }
108 }
109 return NULL;
110 }
111
HTAssocList_findObjectCaseSensitive(HTAssocList * list,const char * name)112 PUBLIC char * HTAssocList_findObjectCaseSensitive (HTAssocList * list, const char * name)
113 {
114 if (list && name) {
115 HTAssocList * cur = list;
116 HTAssoc * assoc;
117 int len = strlen(name);
118 while ((assoc = (HTAssoc *) HTList_nextObject(cur))) {
119 if (!strncmp(assoc->name, name, len))
120 return assoc->value;
121 }
122 }
123 return NULL;
124 }
125
HTAssocList_findObjectCaseSensitiveExact(HTAssocList * list,const char * name)126 PUBLIC char * HTAssocList_findObjectCaseSensitiveExact (HTAssocList * list, const char * name)
127 {
128 if (list && name) {
129 HTAssocList * cur = list;
130 HTAssoc * assoc;
131 while ((assoc = (HTAssoc *) HTAssocList_nextObject(cur))) {
132 if (!strcmp(HTAssoc_name(assoc), name))
133 return HTAssoc_value(assoc);
134 }
135 }
136 return NULL;
137 }
138
139 /*
140 ** Searches the whole list and removes all elements with this name
141 */
HTAssocList_removeObject(HTAssocList * list,const char * name)142 PUBLIC BOOL HTAssocList_removeObject (HTAssocList * list, const char * name)
143 {
144 BOOL found = NO;
145 if (list && name) {
146 HTAssocList * cur = list;
147 HTAssoc * assoc;
148 int len = strlen(name);
149 while ((assoc = (HTAssoc *) HTList_nextObject(cur))) {
150 if (!strncasecomp(assoc->name, name, len)) {
151 HTList_removeObject(list, assoc);
152 HT_FREE(assoc);
153 found = YES;
154 cur = list;
155 }
156 }
157 }
158 return found;
159 }
160