source: trunk/gsdl/src/recpt/cgiargs.cpp@ 9620

Last change on this file since 9620 was 9620, checked in by kjdon, 19 years ago

added some x++ -> ++x changes submitted by Emanuel Dejanu

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