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