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

Last change on this file since 1076 was 1076, checked in by cs025, 24 years ago

Correcting a correction - reinstated all lib files due to silly
CVS confusion.

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