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 |
|
---|
31 | void cgiarg_t::clear () {
|
---|
32 | value.clear();
|
---|
33 | source = program_arg;
|
---|
34 | }
|
---|
35 |
|
---|
36 | cgiarg_t &cgiarg_t::operator=(const cgiarg_t &x) {
|
---|
37 | value=x.value;
|
---|
38 | source=x.source;
|
---|
39 | return *this;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | bool operator==(const cgiarg_t &x, const cgiarg_t &y) {
|
---|
44 | return ((x.value == y.value) && (x.source == y.source));
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool 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
|
---|
56 | cgiargsclass::cgiargsclass () {
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | // this returns NULL if there isn't an entry with a value of
|
---|
61 | // 'key' already defined
|
---|
62 |
|
---|
63 | void 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 |
|
---|
71 | void 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 |
|
---|
76 | void cgiargsclass::setintarg (const text_t &key, int value,
|
---|
77 | cgiarg_t::source_t source) {
|
---|
78 | setarg (key, value, source);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void 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 |
|
---|
86 | void 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 |
|
---|
93 | void 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 |
|
---|
98 | text_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 |
|
---|
105 | int 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
|
---|
114 | ostream &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 |
|
---|
133 | cgiarginfo::cgiarginfo () {
|
---|
134 | multiplechar = false;
|
---|
135 | multiplevalue = false;
|
---|
136 | defaultstatus = weak;
|
---|
137 | argdefault.clear();
|
---|
138 | savedarginfo = can;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | bool 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 |
|
---|
152 | bool 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
|
---|
170 | cgiargsinfoclass::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.
|
---|
178 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiarginfo &info) {
|
---|
179 | outconvertclass text_t2ascii;
|
---|
180 |
|
---|
181 | cgiarginfo *orginfo = getarginfo (info.shortname);
|
---|
182 | if (orginfo == NULL) {
|
---|
183 | argsinfo[info.shortname] = info;
|
---|
184 | return true; // no clashes
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (orginfo->longname != info.longname) {
|
---|
188 | if (logout != NULL) {
|
---|
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 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
199 | << "\" was given as being a single value option\n"
|
---|
200 | << "and a multiple value option.\n\n";
|
---|
201 | }
|
---|
202 | return false; // found a clash
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (orginfo->multiplechar != info.multiplechar) {
|
---|
206 | if (logout != NULL) {
|
---|
207 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
208 | << "\" was given as being a single character option\n"
|
---|
209 | << "and a multiple character option.\n\n";
|
---|
210 | }
|
---|
211 | return false; // found a clash
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (!info.multiplechar && info.argdefault.size() > 1) {
|
---|
215 | if (logout != NULL) {
|
---|
216 | (*logout) << text_t2ascii << "Error: cgi argument \"" << info.shortname
|
---|
217 | << "\" was defined as being a single character option\n"
|
---|
218 | << "but a multiple character default was given.\n\n";
|
---|
219 | }
|
---|
220 | return false; // found a problem
|
---|
221 | }
|
---|
222 |
|
---|
223 | // make sure there is no clashes in the savedarginfo
|
---|
224 | if ((orginfo->savedarginfo==cgiarginfo::mustnot &&
|
---|
225 | info.savedarginfo==cgiarginfo::must) ||
|
---|
226 | (orginfo->savedarginfo==cgiarginfo::must &&
|
---|
227 | info.savedarginfo==cgiarginfo::mustnot)) {
|
---|
228 | if (logout != NULL) {
|
---|
229 | (*logout) << text_t2ascii << "Error: it was specified that cgi argument \""
|
---|
230 | << info.shortname << "\" should be saved in the state\n"
|
---|
231 | << "information and that it should not be save in the state information.\n\n";
|
---|
232 | }
|
---|
233 | return false; // found a clash
|
---|
234 | }
|
---|
235 | // the only time orginfo->savedarginfo can change is when it is set
|
---|
236 | // to "can"
|
---|
237 | if (orginfo->savedarginfo == cgiarginfo::can)
|
---|
238 | orginfo->savedarginfo = info.savedarginfo;
|
---|
239 |
|
---|
240 |
|
---|
241 | if (orginfo->defaultstatus > info.defaultstatus) {
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 | orginfo->defaultstatus = info.defaultstatus;
|
---|
245 | orginfo->argdefault = info.argdefault;
|
---|
246 |
|
---|
247 | return true;
|
---|
248 | }
|
---|
249 |
|
---|
250 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiargsinfoclass &info) {
|
---|
251 | const_iterator here = info.begin ();
|
---|
252 | const_iterator end = info.end ();
|
---|
253 |
|
---|
254 | while (here != end) {
|
---|
255 | if (!addarginfo (logout, (*here).second)) return false;
|
---|
256 | here++;
|
---|
257 | }
|
---|
258 |
|
---|
259 | return true; // made it, no clashes
|
---|
260 | }
|
---|
261 |
|
---|
262 | cgiarginfo *cgiargsinfoclass::getarginfo (const text_t &key) {
|
---|
263 | iterator here = argsinfo.find (key);
|
---|
264 | if (here == argsinfo.end()) return NULL;
|
---|
265 |
|
---|
266 | return &((*here).second);
|
---|
267 | }
|
---|
268 |
|
---|
269 | const cgiarginfo *cgiargsinfoclass::getarginfo (const text_t &key) const {
|
---|
270 | const_iterator here = argsinfo.find (key);
|
---|
271 | if (here == argsinfo.end()) return NULL;
|
---|
272 |
|
---|
273 | return &((*here).second);
|
---|
274 | }
|
---|