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