root/gsdl/trunk/src/lib/gsdlsitecfg.cpp @ 16310

Revision 16310, 6.2 KB (checked in by davidb, 12 years ago)

Introduction of 'collecthome' which parallels 'gsdlhome' to allow the toplevel collect folder to be outside of the gsdlhome area

  • Property svn:executable set to *
Line 
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#include "fileutil.h"
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
41class __site_configuration : public configurable {
42private:
43  text_t *gsdlhome;
44  text_t *collecthome;
45  text_t *httpdomain;
46  text_t *httpprefix;
47  text_t *collection;
48  text_tset *actions;
49  text_tset *browsers;
50  int    *maxrequests;
51public:
52  __site_configuration (text_t *_gsdlhome, text_t* _collecthome, int *_maxrequests) {
53    gsdlhome = _gsdlhome;
54    collecthome = _collecthome;
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* _collecthome,
64            text_t *_httpdomain, text_t *_httpprefix) {
65    gsdlhome    = _gsdlhome;
66    collecthome  = _collecthome;
67    httpdomain = _httpdomain;
68    httpprefix = _httpprefix;
69    collection = NULL;
70    actions = NULL;
71    browsers = NULL;
72    maxrequests = NULL;
73  }
74
75  __site_configuration (text_t *_gsdlhome, text_t* _collecthome,
76            text_t *_httpdomain,
77            text_t *_httpprefix, text_t *_collection) {
78    gsdlhome    = _gsdlhome;
79    collecthome  = _collecthome;
80    httpdomain = _httpdomain;
81    httpprefix = _httpprefix;
82    collection = _collection;
83    actions = NULL;
84    browsers = NULL;
85    maxrequests = NULL;
86  }
87
88  __site_configuration (text_t *_httpprefix, text_tset *_actions, text_tset *_browsers) {
89    gsdlhome    = NULL;
90    collecthome  = NULL;
91    httpdomain = NULL;
92    httpprefix = _httpprefix;
93    collection = NULL;
94    actions = _actions;
95    browsers = _browsers;
96    maxrequests = NULL;
97  }
98
99  inline void configure (const text_t &key, const text_tarray &cfgline) {
100    if (gsdlhome != NULL && key == "gsdlhome") {
101      *gsdlhome = cfgline[0];
102
103      if ((collecthome != NULL) && (collecthome->empty())) {
104    // defaults to <gsdlhome>/collect
105    *collecthome = filename_cat(*gsdlhome,"collect");
106      }
107    }
108
109    if (collecthome != NULL && key == "collecthome") {
110      if (!collecthome->empty()) {
111    // if it has been previously set to default, free and then reassign
112    collecthome->clear();
113      }
114      *collecthome = cfgline[0];
115    }
116
117    if (httpprefix != NULL && key == "httpprefix") {
118      *httpprefix = cfgline[0];
119    }
120
121    if (httpdomain != NULL && key == "httpdomain") {
122      *httpdomain = cfgline[0];
123    }
124
125    if (collection != NULL && key == "collection") {
126      *collection = cfgline[0];
127    }
128
129    if (actions != NULL && key == "actions") {
130      actions->clear();
131      text_tarray::const_iterator thisAction = cfgline.begin();
132      text_tarray::const_iterator endAction = cfgline.end();
133      while (thisAction != endAction) {
134    actions->insert(*thisAction);
135    ++thisAction;
136      }
137    }
138
139    if (browsers != NULL && key == "browsers") {
140      browsers->clear();
141      text_tarray::const_iterator thisBrowser = cfgline.begin();
142      text_tarray::const_iterator endBrowser = cfgline.end();
143      while (thisBrowser != endBrowser) {
144    browsers->insert(*thisBrowser);
145    ++thisBrowser;
146      }
147    }
148
149    if (maxrequests != NULL && key == "maxrequests") {
150      *maxrequests = cfgline[0].getint();
151      if ((*maxrequests) < 1) {
152    *maxrequests = 1;
153      }
154    }
155  }
156};
157
158
159// reads site configuration file returning true on success
160// also sets gsdlhome and maxrequests
161// gsdlsite.cfg should be in same directory as library executable
162bool site_cfg_read (configurator gsdlconfigurator, text_t &gsdlhome,
163            text_t& collecthome, int &maxrequests)
164{
165  __site_configuration sitecfg(&gsdlhome, &collecthome, &maxrequests);
166  gsdlconfigurator.add_configurable(&sitecfg);
167
168  // blank the gsdlhome and collecthome text
169  gsdlhome.clear();
170  collecthome.clear();
171
172  if (gsdlconfigurator.configure("gsdlsite.cfg"))
173  {
174    return true;
175  }
176
177  return false;
178}
179
180
181// this version grabs gsdlhome, collecthome, httpdomain and httpprefix,
182// returns false if it can't find all of them
183bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
184            text_t &httpprefix)
185{
186  // get gsdlhome etc
187  __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix);
188  configurator gsdlconfigurator(&sitecfg);
189
190  gsdlhome.clear();
191  collecthome.clear();
192  httpdomain.clear();
193  httpprefix.clear();
194
195  if (gsdlconfigurator.configure("gsdlsite.cfg"))
196  {
197    return true;
198  }
199
200  return false;
201}
202
203
204// this version grabs gsdlhome, collecthome, httpdomain, httpprefix, collection,
205// returns false if it can't find gsdlhome, httpdomain and httpprefix
206bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
207            text_t &httpprefix, text_t &collection)
208{
209  // get gsdlhome etc
210  __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix, &collection);
211  configurator gsdlconfigurator(&sitecfg);
212   
213  gsdlhome.clear();
214  collecthome.clear();
215  httpdomain.clear();
216  httpprefix.clear();
217  collection.clear();
218   
219  if (gsdlconfigurator.configure("gsdlsite.cfg") &&
220      !gsdlhome.empty() && !collecthome.empty()
221      && !httpdomain.empty() && !httpprefix.empty())
222  {
223    return true;
224  }
225
226  return false;
227}
Note: See TracBrowser for help on using the browser.