1 /************************************************************************
2 qDecoder - Web Application Interface for C/C++ http://www.qDecoder.org
3
4 Copyright (C) 2001 The qDecoder Project.
5 Copyright (C) 1999,2000 Hongik Internet, Inc.
6 Copyright (C) 1998 Nobreak Technologies, Inc.
7 Copyright (C) 1996,1997 Seung-young Kim.
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 Copyright Disclaimer:
24 Hongik Internet, Inc., hereby disclaims all copyright interest.
25 President, Christopher Roh, 6 April 2000
26
27 Nobreak Technologies, Inc., hereby disclaims all copyright interest.
28 President, Yoon Cho, 6 April 2000
29
30 Seung-young Kim, hereby disclaims all copyright interest.
31 Author, Seung-young Kim, 6 April 2000
32 ************************************************************************/
33
34 #include "qDecoder.h"
35 #include "qInternal.h"
36
37
38 /**********************************************
39 ** Linked List(Entry) Routines
40 **********************************************/
41
42
43 /**********************************************
44 ** Usage : _EntryAdd(first entry, name, value);
45 ** Return: New entry pointer.
46 ** Do : Add entry at last but if same name exists, replace it.
47 **********************************************/
48 Q_Entry *_EntryAdd(Q_Entry *first, char *name, char *value) {
49 Q_Entry *entries;
50
51 if(!strcmp(name, "")) return NULL;
52
53 /* check same name */
54 for(entries = first; entries; entries = entries->next) {
55 if(!strcmp(entries->name, name)) {
56 free(entries->value);
57 entries->value = strdup(value);
58 return entries;
59 }
60 }
61
62 /* new entry */
63 entries = (Q_Entry *)malloc(sizeof(Q_Entry));
64 entries->name = strdup(name);
65 entries->value = strdup(value);
66 entries->next = NULL;
67
68 /* If first is not NULL, find last entry then make a link*/
69 if(first) {
70 for(; first->next; first = first->next);
71 first->next = entries;
72 }
73
74 return entries;
75 }
76
77 /**********************************************
78 ** Usage : _EntryRemove(first entry, name to remove);
79 ** Return: first entry pointer.
80 ** Do : Remove entry if same name exists, remove all.
81 **********************************************/
82 Q_Entry *_EntryRemove(Q_Entry *first, char *name) {
83 Q_Entry *entries, *prev_entry;
84
85 if(!strcmp(name, "")) return first;
86
87 for(prev_entry = NULL, entries = first; entries;) {
88 if(!strcmp(entries->name, name)) { /* found */
89 Q_Entry *next;
90
91 next = entries->next;
92
93 /* remove entry itself*/
94 free(entries->name);
95 free(entries->value);
96 free(entries);
97
98 /* remove entry link from linked-list */
99 if(prev_entry == NULL) first = next;
100 else prev_entry->next = next;
101 entries = next;
102 }
103 else { /* next */
104 prev_entry = entries;
105 entries = entries->next;
106 }
107 }
108
109 return first;
110 }
111
112 /**********************************************
113 ** Usage : _EntryValue(pointer of the first entry, name);
114 ** Return: Success pointer of value string, Fail NULL.
115 ** Do : Find value string pointer.
116 ** It find value in linked list.
117 **********************************************/
118 char *_EntryValue(Q_Entry *first, char *name) {
119 Q_Entry *entries;
120
121 for(entries = first; entries; entries = entries->next) {
122 if(!strcmp(name, entries->name))return (entries->value);
123 }
124 return NULL;
125 }
126
127 /**********************************************
128 ** Usage : _EntryiValue(pointer of the first entry, name);
129 ** Return: Success integer of value string, Fail 0.
130 ** Do : Find value string pointer and convert to integer.
131 **********************************************/
132 int _EntryiValue(Q_Entry *first, char *name) {
133 char *str;
134
135 str = _EntryValue(first, name);
136 if(str == NULL) return 0;
137 return atoi(str);
138 }
139
140 /**********************************************
141 ** Usage : _EntryNo(pointer of the first entry, name);
142 ** Return: Success no. Fail 0;
143 ** Do : Find sequence number of value string pointer.
144 **********************************************/
145 int _EntryNo(Q_Entry *first, char *name) {
146 Q_Entry *entries;
147 int no;
148
149 for(no = 1, entries = first; entries; no++, entries = entries->next) {
150 if(!strcmp(name, entries->name)) return no;
151 }
152 return 0;
153 }
154
155 /**********************************************
156 ** Usage : _EntryPrint(pointer of the first entry);
157 ** Return: Amount of entries.
158 ** Do : Print all parsed value & name for debugging.
159 **********************************************/
160 int _EntryPrint(Q_Entry *first) {
161 Q_Entry *entries;
162 int amount;
163
164 qContentType("text/html");
165
166 for(amount = 0, entries = first; entries; amount++, entries = entries->next) {
167 printf("'%s' = '%s'<br>\n" , entries->name, entries->value);
168 }
169
170 return amount;
171 }
172
173 /**********************************************
174 ** Usage : _EntryFree(pointer of the first entry);
175 ** Do : Make free of linked list memory.
176 **********************************************/
177 void _EntryFree(Q_Entry *first) {
178 Q_Entry *entries;
179
180 for(; first; first = entries) {
181 entries = first->next; /* copy next to tmp */
182 free(first->name);
183 free(first->value);
184 free(first);
185 }
186 }
187
188 /**********************************************
189 ** Usage : _EntrySave(pointer of the first entry, filename);
190 ** Return: Success 1, Fail 0.
191 ** Do : Save entries into file.
192 **********************************************/
193 int _EntrySave(Q_Entry *first, char *filename) {
194 FILE *fp;
195 char gmt[32];
196
197 qGetGMTime(gmt, (time_t)0);
198 if((fp = qfopen(filename, "wt")) == NULL) return 0;
199
200 fprintf(fp, "# automatically generated by qDecoder at %s.\n", gmt);
201 fprintf(fp, "# %s\n", filename);
202 for(; first; first = first->next) {
203 char *encvalue;
204
205 encvalue = qURLencode(first->value);
206 fprintf(fp, "%s=%s\n", first->name, encvalue);
207 free(encvalue);
208 }
209
210 qfclose(fp);
211 return 1;
212 }
213
214 /**********************************************
215 ** Usage : _EntryLoad(filename);
216 ** Return: Success pointer of first entry, Fail NULL.
217 ** Do : Load entries from given filename.
218 **********************************************/
219 Q_Entry *_EntryLoad(char *filename) {
220 Q_Entry *first, *entries;
221
222 if((first = qfDecoder(filename)) == NULL) return NULL;
223
224 for(entries = first; entries; entries = entries->next) {
225 qURLdecode(entries->value);
226 }
227
228 return first;
229 }
230