1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * gsdlsitecfg.cpp --
|
---|
4 | * Copyright (C) 2008 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 |
|
---|
27 | #include "gsdlsitecfg.h"
|
---|
28 |
|
---|
29 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
30 | # include <ospace/std/iostream>
|
---|
31 | # include <ospace/std/fstream>
|
---|
32 | #elif defined(GSDL_USE_IOS_H)
|
---|
33 | # include <iostream.h>
|
---|
34 | # include <fstream.h>
|
---|
35 | #else
|
---|
36 | # include <iostream>
|
---|
37 | # include <fstream>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 | class __site_configuration : public configurable {
|
---|
42 | private:
|
---|
43 | text_t *gsdlhome;
|
---|
44 | text_t *httpdomain;
|
---|
45 | text_t *httpprefix;
|
---|
46 | text_t *collection;
|
---|
47 | text_tset *actions;
|
---|
48 | text_tset *browsers;
|
---|
49 | int *maxrequests;
|
---|
50 | public:
|
---|
51 | __site_configuration (text_t *_gsdlhome, int *_maxrequests) {
|
---|
52 | gsdlhome = _gsdlhome;
|
---|
53 | httpdomain = NULL;
|
---|
54 | httpprefix = NULL;
|
---|
55 | collection = NULL;
|
---|
56 | actions = NULL;
|
---|
57 | browsers = NULL;
|
---|
58 | maxrequests = _maxrequests;
|
---|
59 | }
|
---|
60 |
|
---|
61 | __site_configuration (text_t *_gsdlhome, text_t *_httpdomain,
|
---|
62 | text_t *_httpprefix) {
|
---|
63 | gsdlhome = _gsdlhome;
|
---|
64 | httpdomain = _httpdomain;
|
---|
65 | httpprefix = _httpprefix;
|
---|
66 | collection = NULL;
|
---|
67 | actions = NULL;
|
---|
68 | browsers = NULL;
|
---|
69 | maxrequests = NULL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | __site_configuration (text_t *_gsdlhome, text_t *_httpdomain,
|
---|
73 | text_t *_httpprefix, text_t *_collection) {
|
---|
74 | gsdlhome = _gsdlhome;
|
---|
75 | httpdomain = _httpdomain;
|
---|
76 | httpprefix = _httpprefix;
|
---|
77 | collection = _collection;
|
---|
78 | actions = NULL;
|
---|
79 | browsers = NULL;
|
---|
80 | maxrequests = NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | __site_configuration (text_t *_httpprefix, text_tset *_actions, text_tset *_browsers) {
|
---|
84 | gsdlhome = NULL;
|
---|
85 | httpdomain = NULL;
|
---|
86 | httpprefix = _httpprefix;
|
---|
87 | collection = NULL;
|
---|
88 | actions = _actions;
|
---|
89 | browsers = _browsers;
|
---|
90 | maxrequests = NULL;
|
---|
91 | }
|
---|
92 |
|
---|
93 | inline void configure (const text_t &key, const text_tarray &cfgline) {
|
---|
94 | if (gsdlhome != NULL && key == "gsdlhome") {
|
---|
95 | *gsdlhome = cfgline[0];
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (httpprefix != NULL && key == "httpprefix") {
|
---|
99 | *httpprefix = cfgline[0];
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (httpdomain != NULL && key == "httpdomain") {
|
---|
103 | *httpdomain = cfgline[0];
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (collection != NULL && key == "collection") {
|
---|
107 | *collection = cfgline[0];
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (actions != NULL && key == "actions") {
|
---|
111 | actions->clear();
|
---|
112 | text_tarray::const_iterator thisAction = cfgline.begin();
|
---|
113 | text_tarray::const_iterator endAction = cfgline.end();
|
---|
114 | while (thisAction != endAction) {
|
---|
115 | actions->insert(*thisAction);
|
---|
116 | ++thisAction;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (browsers != NULL && key == "browsers") {
|
---|
121 | browsers->clear();
|
---|
122 | text_tarray::const_iterator thisBrowser = cfgline.begin();
|
---|
123 | text_tarray::const_iterator endBrowser = cfgline.end();
|
---|
124 | while (thisBrowser != endBrowser) {
|
---|
125 | browsers->insert(*thisBrowser);
|
---|
126 | ++thisBrowser;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (maxrequests != NULL && key == "maxrequests") {
|
---|
131 | *maxrequests = cfgline[0].getint();
|
---|
132 | if ((*maxrequests) < 1) {
|
---|
133 | *maxrequests = 1;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | };
|
---|
138 |
|
---|
139 |
|
---|
140 | // reads site configuration file returning true on success
|
---|
141 | // also sets gsdlhome and maxrequests
|
---|
142 | // gsdlsite.cfg should be in same directory as library executable
|
---|
143 | bool site_cfg_read (configurator gsdlconfigurator, text_t &gsdlhome, int &maxrequests)
|
---|
144 | {
|
---|
145 | __site_configuration sitecfg(&gsdlhome, &maxrequests);
|
---|
146 | gsdlconfigurator.add_configurable(&sitecfg);
|
---|
147 |
|
---|
148 | // blank the gsdl home text
|
---|
149 | gsdlhome.clear();
|
---|
150 |
|
---|
151 | if (gsdlconfigurator.configure("gsdlsite.cfg"))
|
---|
152 | {
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | // this version grabs gsdlhome, httpdomain and httpprefix,
|
---|
161 | // returns false if it can't find all of them
|
---|
162 | bool site_cfg_read (text_t &gsdlhome, text_t &httpdomain,
|
---|
163 | text_t &httpprefix)
|
---|
164 | {
|
---|
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 | {
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 |
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | // this version grabs gsdlhome, httpdomain, httpprefix, collection,
|
---|
183 | // returns false if it can't find gsdlhome, httpdomain and httpprefix
|
---|
184 | bool site_cfg_read (text_t &gsdlhome, text_t &httpdomain,
|
---|
185 | text_t &httpprefix, text_t &collection)
|
---|
186 | {
|
---|
187 | // get gsdlhome etc
|
---|
188 | __site_configuration sitecfg(&gsdlhome, &httpdomain, &httpprefix, &collection);
|
---|
189 | configurator gsdlconfigurator(&sitecfg);
|
---|
190 |
|
---|
191 | gsdlhome.clear();
|
---|
192 | httpdomain.clear();
|
---|
193 | httpprefix.clear();
|
---|
194 | collection.clear();
|
---|
195 |
|
---|
196 | if (gsdlconfigurator.configure("gsdlsite.cfg") &&
|
---|
197 | !gsdlhome.empty() && !httpdomain.empty() && !httpprefix.empty())
|
---|
198 | {
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | return false;
|
---|
203 | }
|
---|