1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * recptconfig.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 "recptconfig.h"
|
---|
27 | #include "fileutil.h"
|
---|
28 | #include "cfgread.h"
|
---|
29 | #include "cnfgator.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
33 | # include <ospace/std/iostream>
|
---|
34 | # include <ospace/std/fstream>
|
---|
35 | #elif defined(GSDL_USE_IOS_H)
|
---|
36 | # include <iostream.h>
|
---|
37 | # include <fstream.h>
|
---|
38 | #else
|
---|
39 | # include <iostream>
|
---|
40 | # include <fstream>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | class __site_configuration : public configurable {
|
---|
44 | private:
|
---|
45 | text_t *gsdlhome;
|
---|
46 | text_t *httpdomain;
|
---|
47 | text_t *httpprefix;
|
---|
48 | text_t *collection;
|
---|
49 | text_tset *actions;
|
---|
50 | text_tset *browsers;
|
---|
51 | int *maxrequests;
|
---|
52 | public:
|
---|
53 | __site_configuration (text_t *_gsdlhome, int *_maxrequests) {
|
---|
54 | gsdlhome = _gsdlhome;
|
---|
55 | httpdomain = NULL;
|
---|
56 | httpprefix = NULL;
|
---|
57 | collection = NULL;
|
---|
58 | actions = NULL;
|
---|
59 | browsers = NULL;
|
---|
60 | maxrequests = _maxrequests;
|
---|
61 | }
|
---|
62 |
|
---|
63 | __site_configuration (text_t *_gsdlhome, text_t *_httpdomain,
|
---|
64 | text_t *_httpprefix) {
|
---|
65 | gsdlhome = _gsdlhome;
|
---|
66 | httpdomain = _httpdomain;
|
---|
67 | httpprefix = _httpprefix;
|
---|
68 | collection = NULL;
|
---|
69 | actions = NULL;
|
---|
70 | browsers = NULL;
|
---|
71 | maxrequests = NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | __site_configuration (text_t *_gsdlhome, text_t *_httpdomain,
|
---|
75 | text_t *_httpprefix, text_t *_collection) {
|
---|
76 | gsdlhome = _gsdlhome;
|
---|
77 | httpdomain = _httpdomain;
|
---|
78 | httpprefix = _httpprefix;
|
---|
79 | collection = _collection;
|
---|
80 | actions = NULL;
|
---|
81 | browsers = NULL;
|
---|
82 | maxrequests = NULL;
|
---|
83 | }
|
---|
84 |
|
---|
85 | __site_configuration (text_t *_httpprefix, text_tset *_actions, text_tset *_browsers) {
|
---|
86 | gsdlhome = NULL;
|
---|
87 | httpdomain = NULL;
|
---|
88 | httpprefix = _httpprefix;
|
---|
89 | collection = NULL;
|
---|
90 | actions = _actions;
|
---|
91 | browsers = _browsers;
|
---|
92 | maxrequests = NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | inline void configure (const text_t &key, const text_tarray &cfgline) {
|
---|
96 | if (gsdlhome != NULL && key == "gsdlhome") {
|
---|
97 | *gsdlhome = cfgline[0];
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (httpprefix != NULL && key == "httpprefix") {
|
---|
101 | *httpprefix = cfgline[0];
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (httpdomain != NULL && key == "httpdomain") {
|
---|
105 | *httpdomain = cfgline[0];
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (collection != NULL && key == "collection") {
|
---|
109 | *collection = cfgline[0];
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (actions != NULL && key == "actions") {
|
---|
113 | actions->clear();
|
---|
114 | text_tarray::const_iterator thisAction = cfgline.begin();
|
---|
115 | text_tarray::const_iterator endAction = cfgline.end();
|
---|
116 | while (thisAction != endAction) {
|
---|
117 | actions->insert(*thisAction);
|
---|
118 | ++thisAction;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (browsers != NULL && key == "browsers") {
|
---|
123 | browsers->clear();
|
---|
124 | text_tarray::const_iterator thisBrowser = cfgline.begin();
|
---|
125 | text_tarray::const_iterator endBrowser = cfgline.end();
|
---|
126 | while (thisBrowser != endBrowser) {
|
---|
127 | browsers->insert(*thisBrowser);
|
---|
128 | ++thisBrowser;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (maxrequests != NULL && key == "maxrequests") {
|
---|
133 | *maxrequests = cfgline[0].getint();
|
---|
134 | if ((*maxrequests) < 1) {
|
---|
135 | *maxrequests = 1;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 |
|
---|
142 | // reads site configuration file returning true on success
|
---|
143 | // also sets gsdlhome and maxrequests
|
---|
144 | // gsdlsite.cfg should be in same directory as library executable
|
---|
145 | bool site_cfg_read (receptionist &recpt, text_t &gsdlhome, int &maxrequests) {
|
---|
146 | // initialise configurator, etc.
|
---|
147 | __site_configuration sitecfg (&gsdlhome, &maxrequests);
|
---|
148 | configurator gsdlconfigurator(&recpt);
|
---|
149 |
|
---|
150 | gsdlconfigurator.add_configurable(&sitecfg);
|
---|
151 |
|
---|
152 | // blank the gsdl home text
|
---|
153 | gsdlhome.clear();
|
---|
154 |
|
---|
155 | if (gsdlconfigurator.configure("gsdlsite.cfg")) {
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // this version grabs gsdlhome, httpdomain and httpprefix,
|
---|
162 | // returns false if it can't find all of them
|
---|
163 | bool site_cfg_read (text_t &gsdlhome, text_t &httpdomain,
|
---|
164 | text_t &httpprefix) {
|
---|
165 | // get gsdlhome etc
|
---|
166 | __site_configuration sitecfg(&gsdlhome, &httpdomain, &httpprefix);
|
---|
167 | configurator gsdlconfigurator(&sitecfg);
|
---|
168 |
|
---|
169 | gsdlhome.clear();
|
---|
170 | httpdomain.clear();
|
---|
171 | httpprefix.clear();
|
---|
172 |
|
---|
173 | if (gsdlconfigurator.configure("gsdlsite.cfg")) {
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return false;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // this version grabs gsdlhome, httpdomain, httpprefix, collection,
|
---|
181 | // returns false if it can't find gsdlhome, httpdomain and httpprefix
|
---|
182 | bool site_cfg_read (text_t &gsdlhome, text_t &httpdomain,
|
---|
183 | text_t &httpprefix, text_t &collection) {
|
---|
184 | // get gsdlhome etc
|
---|
185 | __site_configuration sitecfg(&gsdlhome, &httpdomain, &httpprefix, &collection);
|
---|
186 | configurator gsdlconfigurator(&sitecfg);
|
---|
187 |
|
---|
188 | gsdlhome.clear();
|
---|
189 | httpdomain.clear();
|
---|
190 | httpprefix.clear();
|
---|
191 | collection.clear();
|
---|
192 |
|
---|
193 | if (gsdlconfigurator.configure("gsdlsite.cfg") &&
|
---|
194 | !gsdlhome.empty() && !httpdomain.empty() && !httpprefix.empty()) {
|
---|
195 | return true;
|
---|
196 | }
|
---|
197 | return false;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void main_cfg_read (const text_t& collection, const text_t &gsdlhome, text_t &httpprefix, text_tset& actions, text_tset& browsers)
|
---|
201 | {
|
---|
202 | __site_configuration sitecfg(&httpprefix, &actions, &browsers);
|
---|
203 | configurator gsdlconfigurator(&sitecfg);
|
---|
204 |
|
---|
205 | httpprefix.clear();
|
---|
206 | actions.clear();
|
---|
207 | browsers.clear();
|
---|
208 |
|
---|
209 | text_t filename;
|
---|
210 |
|
---|
211 | filename_cat (filename, gsdlhome, "collect", collection, "etc", "main.cfg");
|
---|
212 | if (!file_exists(filename)) {
|
---|
213 | filename_cat (filename, gsdlhome, "etc", "main.cfg");
|
---|
214 | if (!file_exists (filename)) {
|
---|
215 | filename.clear();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (!filename.empty()) gsdlconfigurator.configure(filename);
|
---|
220 |
|
---|
221 |
|
---|
222 | if (!collection.empty()) {
|
---|
223 | filename_cat (filename, gsdlhome, "collect", collection, "etc", "collect.cfg");
|
---|
224 | if (!file_exists (filename)) {
|
---|
225 | filename_cat (filename, gsdlhome, "etc", "collect.cfg");
|
---|
226 | if (!file_exists (filename)) filename.clear();
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (!filename.empty()) gsdlconfigurator.configure(filename);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | // main_cfg_read reads both main.cfg and collect.cfg. It attempts
|
---|
234 | // to read main.cfg first so values in collect.cfg override those
|
---|
235 | // set earlier by main.cfg
|
---|
236 | bool main_cfg_read (receptionist &recpt, const text_t &gsdlhome,
|
---|
237 | const text_t &collection) {
|
---|
238 |
|
---|
239 | // read in the main configuration file
|
---|
240 | bool rv = false;
|
---|
241 |
|
---|
242 | text_t key, filename;
|
---|
243 | text_tarray cfgline;
|
---|
244 | filename = filename_cat (gsdlhome, "etc");
|
---|
245 | filename = filename_cat (filename, "main.cfg");
|
---|
246 | if (!collection.empty() && file_exists(filename)) {
|
---|
247 | rv = recpt.read_configfile(filename);
|
---|
248 | } else {
|
---|
249 | filename_cat (filename, gsdlhome, "etc", "main.cfg");
|
---|
250 | if (file_exists (filename)) {
|
---|
251 | rv = recpt.read_configfile(filename);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | // Look for collect.cfg in GSDLHOME/collect/collection-name/etc directory
|
---|
256 | // (if this is for a particular collection), and then GSDLHOME/etc.
|
---|
257 | if (!collection.empty()) {
|
---|
258 | filename = filename_cat (gsdlhome, "collect");
|
---|
259 | filename = filename_cat (filename, collection);
|
---|
260 | filename = filename_cat (filename, "etc");
|
---|
261 | filename = filename_cat (filename, "collect.cfg");
|
---|
262 | if (!file_exists (filename)) {
|
---|
263 | filename = filename_cat (gsdlhome, "etc");
|
---|
264 | filename = filename_cat (filename, "collect.cfg");
|
---|
265 | if (!file_exists (filename)) filename.clear();
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (!filename.empty()) {
|
---|
269 | rv |= recpt.read_configfile(filename);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | return rv;
|
---|
273 | }
|
---|