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