1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * cgiargs.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | * $Id: cgiargs.cpp 802 1999-12-05 21:25:20Z sjboddie $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.11 1999/12/05 21:25:20 sjboddie
|
---|
31 | can't remember why I did this - must have been a reason ;-)
|
---|
32 |
|
---|
33 | Revision 1.10 1999/11/08 20:26:38 sjboddie
|
---|
34 | added multiplevalue option to cgiarginfo
|
---|
35 |
|
---|
36 | Revision 1.9 1999/09/07 04:56:53 sjboddie
|
---|
37 | added GPL notice
|
---|
38 |
|
---|
39 | Revision 1.8 1999/09/03 09:52:45 rjmcnab
|
---|
40 | Fixed a couple of small things
|
---|
41 |
|
---|
42 | Revision 1.7 1999/09/02 00:23:55 rjmcnab
|
---|
43 | changes to get compiling on AIX
|
---|
44 |
|
---|
45 | Revision 1.6 1999/07/11 01:02:13 rjmcnab
|
---|
46 | Stored information relating to the cgi argument's origin with the cgi argument.
|
---|
47 |
|
---|
48 | Revision 1.5 1999/02/08 01:28:00 rjmcnab
|
---|
49 |
|
---|
50 | Got the receptionist producing something using the statusaction.
|
---|
51 |
|
---|
52 | Revision 1.4 1999/02/05 10:42:41 rjmcnab
|
---|
53 |
|
---|
54 | Continued working on receptionist
|
---|
55 |
|
---|
56 | Revision 1.3 1999/02/04 10:00:54 rjmcnab
|
---|
57 |
|
---|
58 | Developed the idea of an "action" and having them define the cgi arguments
|
---|
59 | which they need and how those cgi arguments function.
|
---|
60 |
|
---|
61 | Revision 1.2 1999/01/12 01:51:06 rjmcnab
|
---|
62 |
|
---|
63 | Standard header.
|
---|
64 |
|
---|
65 | Revision 1.1 1999/01/08 08:40:54 rjmcnab
|
---|
66 |
|
---|
67 | Moved from lib directory.
|
---|
68 |
|
---|
69 | Revision 1.1 1999/01/08 07:50:30 rjmcnab
|
---|
70 |
|
---|
71 | Moved from src/library directory to lib directory.
|
---|
72 |
|
---|
73 | */
|
---|
74 |
|
---|
75 |
|
---|
76 | #include "cgiargs.h"
|
---|
77 | #include "gsdlunicode.h"
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | void cgiarg_t::clear () {
|
---|
82 | value.clear();
|
---|
83 | source = program_arg;
|
---|
84 | }
|
---|
85 |
|
---|
86 | cgiarg_t &cgiarg_t::operator=(const cgiarg_t &x) {
|
---|
87 | value=x.value;
|
---|
88 | source=x.source;
|
---|
89 | return *this;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | bool operator==(const cgiarg_t &x, const cgiarg_t &y) {
|
---|
94 | return ((x.value == y.value) && (x.source == y.source));
|
---|
95 | }
|
---|
96 |
|
---|
97 | bool operator<(const cgiarg_t &x, const cgiarg_t &y) {
|
---|
98 | return ((x.value < y.value) ||
|
---|
99 | ((x.value == y.value) && (x.source == y.source)));
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | // constructors
|
---|
106 | cgiargsclass::cgiargsclass () {
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | // this returns NULL if there isn't an entry with a value of
|
---|
111 | // 'key' already defined
|
---|
112 |
|
---|
113 | void cgiargsclass::setarg (const text_t &key, const text_t &value,
|
---|
114 | cgiarg_t::source_t source) {
|
---|
115 | cgiarg_t temparg;
|
---|
116 | temparg.value = value;
|
---|
117 | temparg.source = source;
|
---|
118 | args[key] = temparg;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void cgiargsclass::setdefaultarg (const text_t &key, const text_t &value,
|
---|
122 | cgiarg_t::source_t source) {
|
---|
123 | if (getarg(key) == NULL) setarg (key, value, source);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void cgiargsclass::setintarg (const text_t &key, int value,
|
---|
127 | cgiarg_t::source_t source) {
|
---|
128 | setarg (key, value, source);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void cgiargsclass::setdefaultintarg (const text_t &key, int value,
|
---|
132 | cgiarg_t::source_t source) {
|
---|
133 | if (getarg(key) == NULL) setintarg (key, value, source);
|
---|
134 | }
|
---|
135 |
|
---|
136 | void cgiargsclass::setcarg (const text_t &key, unsigned short c,
|
---|
137 | cgiarg_t::source_t source) {
|
---|
138 | text_t t;
|
---|
139 | t.push_back(c);
|
---|
140 | setarg(key,t, source);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void cgiargsclass::setdefaultcarg (const text_t &key, unsigned short c,
|
---|
144 | cgiarg_t::source_t source) {
|
---|
145 | if (getarg(key) == NULL) setcarg (key, c, source);
|
---|
146 | }
|
---|
147 |
|
---|
148 | text_t *cgiargsclass::getarg (const text_t &key) {
|
---|
149 | iterator here = args.find (key);
|
---|
150 | if (here == args.end()) return NULL;
|
---|
151 |
|
---|
152 | return &((*here).second.value);
|
---|
153 | }
|
---|
154 |
|
---|
155 | int cgiargsclass::getintarg (const text_t &key) {
|
---|
156 | text_t *text = getarg (key);
|
---|
157 | if (text == NULL) return 0;
|
---|
158 | return text->getint();
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | // stream operators to print cgi arguments for debugging purposes
|
---|
164 | ostream &operator<<(ostream &outs, const cgiargsclass &args) {
|
---|
165 | utf8outconvertclass text_t2utf8;
|
---|
166 | cgiargsclass::const_iterator here = args.begin ();
|
---|
167 | cgiargsclass::const_iterator end = args.end ();
|
---|
168 |
|
---|
169 | outs << "*** cgiargsclass\n";
|
---|
170 |
|
---|
171 | while (here != end) {
|
---|
172 | outs << text_t2utf8 << " \"" << (*here).first << "\"=\"" <<
|
---|
173 | (*here).second.value << "\"\n";
|
---|
174 | here++;
|
---|
175 | }
|
---|
176 | outs << "\n";
|
---|
177 |
|
---|
178 | return outs;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | cgiarginfo::cgiarginfo () {
|
---|
184 | multiplechar = false;
|
---|
185 | multiplevalue = false;
|
---|
186 | defaultstatus = weak;
|
---|
187 | argdefault.clear();
|
---|
188 | savedarginfo = can;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | bool operator==(const cgiarginfo &x, const cgiarginfo &y) {
|
---|
193 | return ((x.shortname == y.shortname) &&
|
---|
194 | (x.longname == y.longname) &&
|
---|
195 | (x.multiplechar == y.multiplechar) &&
|
---|
196 | (x.multiplevalue == y.multiplevalue) &&
|
---|
197 | (x.defaultstatus == y.defaultstatus) &&
|
---|
198 | (x.argdefault == y.argdefault) &&
|
---|
199 | (x.savedarginfo == y.savedarginfo));
|
---|
200 | }
|
---|
201 |
|
---|
202 | bool operator<(const cgiarginfo &x, const cgiarginfo &y) {
|
---|
203 | return ((x.shortname < y.shortname) ||
|
---|
204 | ((x.shortname == y.shortname) &&
|
---|
205 | ((x.longname < y.longname) ||
|
---|
206 | ((x.longname == y.longname) &&
|
---|
207 | ((x.multiplevalue < y.multiplevalue) ||
|
---|
208 | ((x.multiplevalue == y.multiplevalue) &&
|
---|
209 | ((x.multiplechar < y.multiplechar) ||
|
---|
210 | ((x.multiplechar == y.multiplechar) &&
|
---|
211 | ((x.defaultstatus < y.defaultstatus) ||
|
---|
212 | ((x.defaultstatus == y.defaultstatus) &&
|
---|
213 | ((x.argdefault < y.argdefault) ||
|
---|
214 | ((x.argdefault == y.argdefault) &&
|
---|
215 | ((x.savedarginfo < y.savedarginfo))))))))))))));
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | // constructor
|
---|
220 | cgiargsinfoclass::cgiargsinfoclass () {
|
---|
221 | }
|
---|
222 |
|
---|
223 | // addarginfo will combine the information with the present
|
---|
224 | // information. If name clashes were detected then the information
|
---|
225 | // will be written to logout and addarginfo will return false. No
|
---|
226 | // processing with the arguments should be done if this happens
|
---|
227 | // as the results will be meaningless.
|
---|
228 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiarginfo &info) {
|
---|
229 | outconvertclass text_t2ascii;
|
---|
230 |
|
---|
231 | cgiarginfo *orginfo = getarginfo (info.shortname);
|
---|
232 | if (orginfo == NULL) {
|
---|
233 | argsinfo[info.shortname] = info;
|
---|
234 | return true; // no clashes
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (orginfo->longname != info.longname) {
|
---|
238 | if (logout != NULL) {
|
---|
239 | (*logout) << text_t2ascii << "Error: cgi argument name clash for argument \""
|
---|
240 | << info.shortname << "\".\nOne long name was\n \"" << orginfo->longname
|
---|
241 | << "\"\nand the other was\n \"" << info.longname << "\".\n\n";
|
---|
242 | }
|
---|
243 | return false; // found a clash
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (orginfo->multiplevalue != info.multiplevalue) {
|
---|
247 | if (logout != NULL) {
|
---|
248 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
249 | << "\" was given as being a single value option\n"
|
---|
250 | << "and a multiple value option.\n\n";
|
---|
251 | }
|
---|
252 | return false; // found a clash
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (orginfo->multiplechar != info.multiplechar) {
|
---|
256 | if (logout != NULL) {
|
---|
257 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
258 | << "\" was given as being a single character option\n"
|
---|
259 | << "and a multiple character option.\n\n";
|
---|
260 | }
|
---|
261 | return false; // found a clash
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (!info.multiplechar && info.argdefault.size() > 1) {
|
---|
265 | if (logout != NULL) {
|
---|
266 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
267 | << "\" was defined as being a single character option\n"
|
---|
268 | << "but a multiple character default was given.\n\n";
|
---|
269 | }
|
---|
270 | return false; // found a problem
|
---|
271 | }
|
---|
272 |
|
---|
273 | // make sure there is no clashes in the savedarginfo
|
---|
274 | if ((orginfo->savedarginfo==cgiarginfo::mustnot &&
|
---|
275 | info.savedarginfo==cgiarginfo::must) ||
|
---|
276 | (orginfo->savedarginfo==cgiarginfo::must &&
|
---|
277 | info.savedarginfo==cgiarginfo::mustnot)) {
|
---|
278 | if (logout != NULL) {
|
---|
279 | (*logout) << text_t2ascii << "Error: it was specified that cgi argument \""
|
---|
280 | << info.shortname << "\" should be saved in the state\n"
|
---|
281 | << "information and that it should not be save in the state information.\n\n";
|
---|
282 | }
|
---|
283 | return false; // found a clash
|
---|
284 | }
|
---|
285 | // the only time orginfo->savedarginfo can change is when it is set
|
---|
286 | // to "can"
|
---|
287 | if (orginfo->savedarginfo == cgiarginfo::can)
|
---|
288 | orginfo->savedarginfo = info.savedarginfo;
|
---|
289 |
|
---|
290 |
|
---|
291 | if (orginfo->defaultstatus > info.defaultstatus) {
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 | orginfo->defaultstatus = info.defaultstatus;
|
---|
295 | orginfo->argdefault = info.argdefault;
|
---|
296 |
|
---|
297 | return true;
|
---|
298 | }
|
---|
299 |
|
---|
300 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiargsinfoclass &info) {
|
---|
301 | const_iterator here = info.begin ();
|
---|
302 | const_iterator end = info.end ();
|
---|
303 |
|
---|
304 | while (here != end) {
|
---|
305 | if (!addarginfo (logout, (*here).second)) return false;
|
---|
306 | here++;
|
---|
307 | }
|
---|
308 |
|
---|
309 | return true; // made it, no clashes
|
---|
310 | }
|
---|
311 |
|
---|
312 | cgiarginfo *cgiargsinfoclass::getarginfo (const text_t &key) {
|
---|
313 | iterator here = argsinfo.find (key);
|
---|
314 | if (here == argsinfo.end()) return NULL;
|
---|
315 |
|
---|
316 | return &((*here).second);
|
---|
317 | }
|
---|
318 |
|
---|
319 | const cgiarginfo *cgiargsinfoclass::getarginfo (const text_t &key) const {
|
---|
320 | const_iterator here = argsinfo.find (key);
|
---|
321 | if (here == argsinfo.end()) return NULL;
|
---|
322 |
|
---|
323 | return &((*here).second);
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 |
|
---|