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