source: trunk/gsdl/src/recpt/recptconfig.cpp@ 7381

Last change on this file since 7381 was 7302, checked in by kjdon, 20 years ago

removed a strange piece of code: 'return true; return false;' :-) site_cfg_read(gsdlhome, httpdomain, httpprefix) no longer tests whether these strings are empty (and then return true anyway) and now returns false only if it couldn't configure. the calling code now must check whether gsdlhome is empty before using it

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
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
43class __site_configuration : public configurable {
44private:
45 text_t *gsdlhome;
46 text_t *httpdomain;
47 text_t *httpprefix;
48 int *maxrequests;
49public:
50 __site_configuration (text_t *_gsdlhome, int *_maxrequests) {
51 gsdlhome = _gsdlhome;
52 httpdomain = NULL;
53 httpprefix = NULL;
54 maxrequests = _maxrequests;
55 }
56
57 __site_configuration (text_t *_gsdlhome, text_t *_httpdomain,
58 text_t *_httpprefix) {
59 gsdlhome = _gsdlhome;
60 httpdomain = _httpdomain;
61 httpprefix = _httpprefix;
62 maxrequests = NULL;
63 }
64
65 inline void configure (const text_t &key, const text_tarray &cfgline) {
66 if (key == "gsdlhome") {
67 *gsdlhome = cfgline[0];
68 }
69
70 if (key == "httpprefix" &&
71 httpprefix != NULL) {
72 *httpprefix = cfgline[0];
73 }
74
75 if (key == "httpdomain" &&
76 httpdomain != NULL) {
77 *httpdomain = cfgline[0];
78 }
79
80 if (key == "maxrequests" &&
81 maxrequests != NULL) {
82 *maxrequests = cfgline[0].getint();
83 if ((*maxrequests) < 1) {
84 *maxrequests = 1;
85 }
86 }
87 }
88};
89
90
91// reads site configuration file returning true on success
92// also sets gsdlhome and maxrequests
93// gsdlsite.cfg should be in same directory as library executable
94bool site_cfg_read (receptionist &recpt, text_t &gsdlhome, int &maxrequests) {
95 // initialise configurator, etc.
96 __site_configuration sitecfg (&gsdlhome, &maxrequests);
97 configurator gsdlconfigurator(&recpt);
98
99 gsdlconfigurator.add_configurable(&sitecfg);
100
101 // blank the gsdl home text
102 gsdlhome.clear();
103
104 if (gsdlconfigurator.configure("gsdlsite.cfg")) {
105 return true;
106 }
107 return false;
108}
109
110// this version grabs gsdlhome, httpdomain and httpprefix,
111// returns false if it can't find all of them
112bool site_cfg_read (text_t &gsdlhome, text_t &httpdomain,
113 text_t &httpprefix) {
114 // get gsdlhome etc
115 __site_configuration sitecfg(&gsdlhome, &httpdomain, &httpprefix);
116 configurator gsdlconfigurator(&sitecfg);
117
118 gsdlhome.clear();
119 httpdomain.clear();
120 httpprefix.clear();
121
122 if (gsdlconfigurator.configure("gsdlsite.cfg")) {
123 return true;
124 }
125
126 return false;
127}
128
129// main_cfg_read reads both main.cfg and collect.cfg. It attempts
130// to read main.cfg first so values in collect.cfg override those
131// set earlier by main.cfg
132bool main_cfg_read (receptionist &recpt, const text_t &gsdlhome,
133 const text_t &collection) {
134
135 // read in the main configuration file
136 bool rv = false;
137
138 text_t key, filename;
139 text_tarray cfgline;
140 filename = filename_cat (gsdlhome, "etc");
141 filename = filename_cat (filename, "main.cfg");
142 if (file_exists (filename)) {
143 rv = recpt.read_configfile(filename);
144 }
145
146 // Look for collect.cfg in GSDLHOME/collect/collection-name/etc directory
147 // (if this is for a particular collection), and then GSDLHOME/etc.
148 if (!collection.empty()) {
149 filename = filename_cat (gsdlhome, "collect");
150 filename = filename_cat (filename, collection);
151 filename = filename_cat (filename, "etc");
152 filename = filename_cat (filename, "collect.cfg");
153 if (!file_exists (filename)) {
154 filename = filename_cat (gsdlhome, "etc");
155 filename = filename_cat (filename, "collect.cfg");
156 if (!file_exists (filename)) filename.clear();
157 }
158
159 if (!filename.empty()) {
160 rv |= recpt.read_configfile(filename);
161 }
162 }
163 return rv;
164}
Note: See TracBrowser for help on using the repository browser.