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