source: trunk/gsdl/lib/cfgread.cpp@ 1270

Last change on this file since 1270 was 1270, checked in by sjboddie, 24 years ago

Receptionist now caches collection information to avoid making multiple
get_collectinfo calls to collection server

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/**********************************************************************
2 *
3 * cfgread.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 * $Id: cfgread.cpp 1270 2000-07-05 21:49:36Z sjboddie $
25 *
26 *********************************************************************/
27
28/*
29 $Log$
30 Revision 1.9 2000/07/05 21:49:22 sjboddie
31 Receptionist now caches collection information to avoid making multiple
32 get_collectinfo calls to collection server
33
34 Revision 1.8 2000/04/06 19:57:57 cs025
35 Correcting a correction - reinstated all lib files due to silly
36 CVS confusion.
37
38 Revision 1.6 1999/12/05 21:08:28 sjboddie
39 added support for reading and writing windows ini files
40
41 Revision 1.5 1999/09/07 04:57:41 sjboddie
42 added gpl notice
43
44 Revision 1.4 1999/02/02 09:54:26 rjmcnab
45
46 Changed the configuration file reader so that any space can be used to
47 seperate items and so that quotes can be used to contain items. I also
48 allowed a '\' to indicate that the line continues on the next line.
49
50 Revision 1.3 1999/01/12 01:50:57 rjmcnab
51
52 Standard header.
53
54 Revision 1.2 1999/01/08 02:33:13 rjmcnab
55
56 Added standard header to source files.
57
58 */
59
60
61#include "cfgread.h"
62
63int write_ini_line (ofstream &fileout, const text_t &key, const text_t value) {
64 if (key.empty() || value.empty()) return -1;
65 outconvertclass text_t2ascii;
66 fileout << text_t2ascii << key << "=" << value << "\n";
67 return 0;
68}
69
70int read_ini_line (ifstream &filein, text_t &key, text_t &value) {
71 if (filein.eof()) return -1;
72
73 key.clear();
74 value.clear();
75 char c;
76 filein.get(c);
77
78 int foundeq = 0;
79 while (!filein.eof() && c != '\n') {
80 if (!foundeq && c == '=') {foundeq = 1; filein.get(c);}
81
82 if (foundeq) value.push_back(c);
83 else key.push_back(c);
84 filein.get(c);
85 }
86 if (key.empty()) return -1;
87 return 0;
88}
89
90
91// returns 0 on success, -1 on failure
92int read_cfg_line (ifstream &filein, text_tarray &values) {
93 // outconvertclass text_t2ascii;
94
95 values.erase(values.begin(), values.end());
96
97 if (!filein.good()) return -1;
98
99 text_t curvalue;
100 char c, quote;
101
102 bool linecontinues = true;
103 filein.get(c);
104 while (!filein.eof() && linecontinues) {
105 // eat up all return characters
106 while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
107
108 // ignore comments
109 if (c == '#') {
110 while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
111 }
112
113 // deal with all the records on this line
114 while (!filein.eof() && c!='\n' && c!='\r') {
115 // ignore white space
116 while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
117
118 // get the next record
119 if (c!='\n' && c!='\r') {
120 curvalue.clear();
121 quote = 0;
122
123 // see if this is a quoted record
124 if (c=='\'' || c=='\"') {
125 quote = c;
126 filein.get(c);
127 }
128
129 // get the value
130 while (!filein.eof() && c!='\n' && c!='\r') {
131 if (quote != 0 && c == quote) { // end of quote
132 filein.get(c);
133 break;
134 } else if (quote == 0 && (c == ' ' || c == '\t')) {
135 break; // space seperator
136 }
137 curvalue.push_back(c);
138 filein.get(c);
139 }
140
141 // see if the record continues on the next line
142 if ((c=='\n' || c=='\r') && curvalue=="\\") {
143 linecontinues = true;
144 } else {
145 // cerr << text_t2ascii << "\"" << curvalue << "\" ";
146
147 values.push_back(curvalue);
148 curvalue.clear();
149 linecontinues = false;
150 }
151 }
152 }
153 }
154
155 // cerr << "\n";
156
157 return 0;
158}
Note: See TracBrowser for help on using the repository browser.