[108] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * cgiargs.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[533] | 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.
|
---|
[108] | 9 | *
|
---|
[533] | 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 | *
|
---|
[108] | 24 | *********************************************************************/
|
---|
| 25 |
|
---|
| 26 | #include "cgiargs.h"
|
---|
| 27 | #include "gsdlunicode.h"
|
---|
| 28 |
|
---|
[364] | 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 |
|
---|
[509] | 43 | bool operator==(const cgiarg_t &x, const cgiarg_t &y) {
|
---|
| 44 | return ((x.value == y.value) && (x.source == y.source));
|
---|
| 45 | }
|
---|
[364] | 46 |
|
---|
[509] | 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 | }
|
---|
[364] | 51 |
|
---|
[509] | 52 |
|
---|
| 53 |
|
---|
| 54 |
|
---|
[108] | 55 | // constructors
|
---|
[146] | 56 | cgiargsclass::cgiargsclass () {
|
---|
[108] | 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | // this returns NULL if there isn't an entry with a value of
|
---|
| 61 | // 'key' already defined
|
---|
| 62 |
|
---|
[364] | 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;
|
---|
[108] | 69 | }
|
---|
| 70 |
|
---|
[364] | 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);
|
---|
[108] | 74 | }
|
---|
| 75 |
|
---|
[364] | 76 | void cgiargsclass::setintarg (const text_t &key, int value,
|
---|
| 77 | cgiarg_t::source_t source) {
|
---|
| 78 | setarg (key, value, source);
|
---|
[108] | 79 | }
|
---|
| 80 |
|
---|
[364] | 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);
|
---|
[108] | 84 | }
|
---|
| 85 |
|
---|
[364] | 86 | void cgiargsclass::setcarg (const text_t &key, unsigned short c,
|
---|
| 87 | cgiarg_t::source_t source) {
|
---|
[108] | 88 | text_t t;
|
---|
| 89 | t.push_back(c);
|
---|
[364] | 90 | setarg(key,t, source);
|
---|
[108] | 91 | }
|
---|
| 92 |
|
---|
[364] | 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);
|
---|
[108] | 96 | }
|
---|
| 97 |
|
---|
[364] | 98 | text_t *cgiargsclass::getarg (const text_t &key) {
|
---|
[108] | 99 | iterator here = args.find (key);
|
---|
| 100 | if (here == args.end()) return NULL;
|
---|
| 101 |
|
---|
[364] | 102 | return &((*here).second.value);
|
---|
[108] | 103 | }
|
---|
| 104 |
|
---|
[364] | 105 | int cgiargsclass::getintarg (const text_t &key) {
|
---|
[108] | 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
|
---|
[364] | 114 | ostream &operator<<(ostream &outs, const cgiargsclass &args) {
|
---|
[108] | 115 | utf8outconvertclass text_t2utf8;
|
---|
| 116 | cgiargsclass::const_iterator here = args.begin ();
|
---|
| 117 | cgiargsclass::const_iterator end = args.end ();
|
---|
| 118 |
|
---|
| 119 | outs << "*** cgiargsclass\n";
|
---|
| 120 |
|
---|
[364] | 121 | while (here != end) {
|
---|
| 122 | outs << text_t2utf8 << " \"" << (*here).first << "\"=\"" <<
|
---|
| 123 | (*here).second.value << "\"\n";
|
---|
[9620] | 124 | ++here;
|
---|
[364] | 125 | }
|
---|
[108] | 126 | outs << "\n";
|
---|
| 127 |
|
---|
| 128 | return outs;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
[146] | 132 |
|
---|
| 133 | cgiarginfo::cgiarginfo () {
|
---|
| 134 | multiplechar = false;
|
---|
[776] | 135 | multiplevalue = false;
|
---|
[146] | 136 | defaultstatus = weak;
|
---|
[802] | 137 | argdefault.clear();
|
---|
[528] | 138 | savedarginfo = can;
|
---|
[146] | 139 | }
|
---|
| 140 |
|
---|
[509] | 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) &&
|
---|
[776] | 146 | (x.multiplevalue == y.multiplevalue) &&
|
---|
[509] | 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) &&
|
---|
[776] | 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))))))))))))));
|
---|
[509] | 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
[146] | 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.
|
---|
[154] | 178 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiarginfo &info) {
|
---|
[146] | 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) {
|
---|
[154] | 187 | if (logout != NULL) {
|
---|
[7429] | 188 | outconvertclass text_t2ascii;
|
---|
[154] | 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 | }
|
---|
[146] | 193 | return false; // found a clash
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[776] | 196 | if (orginfo->multiplevalue != info.multiplevalue) {
|
---|
| 197 | if (logout != NULL) {
|
---|
[7429] | 198 | outconvertclass text_t2ascii;
|
---|
[776] | 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 |
|
---|
[146] | 206 | if (orginfo->multiplechar != info.multiplechar) {
|
---|
[154] | 207 | if (logout != NULL) {
|
---|
[7429] | 208 | outconvertclass text_t2ascii;
|
---|
[154] | 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 | }
|
---|
[146] | 213 | return false; // found a clash
|
---|
| 214 | }
|
---|
[154] | 215 |
|
---|
[146] | 216 | if (!info.multiplechar && info.argdefault.size() > 1) {
|
---|
[154] | 217 | if (logout != NULL) {
|
---|
[7429] | 218 | outconvertclass text_t2ascii;
|
---|
[154] | 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 | }
|
---|
[150] | 223 | return false; // found a problem
|
---|
[146] | 224 | }
|
---|
| 225 |
|
---|
[150] | 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)) {
|
---|
[154] | 231 | if (logout != NULL) {
|
---|
[7429] | 232 | outconvertclass text_t2ascii;
|
---|
[154] | 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 | }
|
---|
[150] | 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 |
|
---|
[146] | 245 | if (orginfo->defaultstatus > info.defaultstatus) {
|
---|
| 246 | return true;
|
---|
| 247 | }
|
---|
| 248 | orginfo->defaultstatus = info.defaultstatus;
|
---|
| 249 | orginfo->argdefault = info.argdefault;
|
---|
[150] | 250 |
|
---|
[146] | 251 | return true;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[154] | 254 | bool cgiargsinfoclass::addarginfo (ostream *logout, const cgiargsinfoclass &info) {
|
---|
[146] | 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;
|
---|
[9620] | 260 | ++here;
|
---|
[146] | 261 | }
|
---|
| 262 |
|
---|
| 263 | return true; // made it, no clashes
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[7429] | 266 | bool 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 | }
|
---|
[9620] | 296 | ++thisInfo;
|
---|
[7429] | 297 | }
|
---|
| 298 | return true;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | bool 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;
|
---|
[9620] | 307 | ++thisArg;
|
---|
[7429] | 308 | }
|
---|
| 309 | return true;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[146] | 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 |
|
---|
[150] | 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 | }
|
---|