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