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 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "cfgread.h"
|
---|
27 | #include <ctype.h> // for isspace()
|
---|
28 |
|
---|
29 | int write_ini_line (ofstream &fileout, const text_t &key, const text_t value) {
|
---|
30 | if (key.empty() || value.empty()) return -1;
|
---|
31 | outconvertclass text_t2ascii;
|
---|
32 | fileout << text_t2ascii << key << "=" << value << "\n";
|
---|
33 | return 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | int read_ini_line (ifstream &filein, text_t &key, text_t &value) {
|
---|
37 | if (filein.eof()) return -1;
|
---|
38 |
|
---|
39 | key.clear();
|
---|
40 | value.clear();
|
---|
41 | char c;
|
---|
42 | filein.get(c);
|
---|
43 |
|
---|
44 | int foundeq = 0;
|
---|
45 | while (!filein.eof() && c != '\n') {
|
---|
46 | if (!foundeq && c == '=') {foundeq = 1; filein.get(c);}
|
---|
47 |
|
---|
48 | if (foundeq) value.push_back(c);
|
---|
49 | else key.push_back(c);
|
---|
50 | filein.get(c);
|
---|
51 | }
|
---|
52 | if (key.empty()) return 0; // blank line maybe?
|
---|
53 | return 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | // write out line of values to cfgfile
|
---|
57 | // does nothing fancy - make sure no values contain carriage returns
|
---|
58 | int write_cfg_line (ofstream &fileout, const text_tarray &values) {
|
---|
59 | outconvertclass text_t2ascii;
|
---|
60 |
|
---|
61 | text_tarray::const_iterator here = values.begin();
|
---|
62 | text_tarray::const_iterator end = values.end();
|
---|
63 |
|
---|
64 | bool first = true;
|
---|
65 | while (here != end) {
|
---|
66 | if (first) fileout << text_t2ascii << *here;
|
---|
67 | else fileout << text_t2ascii << " \"" << *here << "\"";
|
---|
68 | first = false;
|
---|
69 | here ++;
|
---|
70 | }
|
---|
71 | fileout << "\n";
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | // same, but use a file descriptor this time
|
---|
76 | int write_cfg_line (int fileout, const text_tarray &values) {
|
---|
77 | outconvertclass text_t2ascii;
|
---|
78 |
|
---|
79 | text_tarray::const_iterator here = values.begin();
|
---|
80 | text_tarray::const_iterator end = values.end();
|
---|
81 |
|
---|
82 | if (here != end) {
|
---|
83 | char *s=here->getcstr();
|
---|
84 | write(fileout,s ,here->size());
|
---|
85 | delete s;
|
---|
86 |
|
---|
87 | ++here;
|
---|
88 | }
|
---|
89 | while (here != end) {
|
---|
90 | write(fileout, " \"", 2);
|
---|
91 | char *s=here->getcstr();
|
---|
92 | write(fileout,s ,here->size());
|
---|
93 | delete s;
|
---|
94 | write(fileout, "\"", 1);
|
---|
95 |
|
---|
96 | ++here;
|
---|
97 | }
|
---|
98 | write(fileout,"\n",1);
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | // returns 0 on success, -1 on failure
|
---|
104 | int read_cfg_line (ifstream &filein, text_tarray &values) {
|
---|
105 | // we split up the line into tokens, pushing each token into the
|
---|
106 | // values array. Quoted phrases are a single token. A "\" at the end
|
---|
107 | // of a line continues onto the next line.
|
---|
108 |
|
---|
109 | values.erase(values.begin(), values.end());
|
---|
110 |
|
---|
111 | if (!filein.good()) return -1;
|
---|
112 |
|
---|
113 | text_t curvalue;
|
---|
114 | char c;
|
---|
115 | filein.get(c);
|
---|
116 |
|
---|
117 | // skip white space
|
---|
118 | while (!filein.eof() && isspace(c)) { filein.get(c); }
|
---|
119 |
|
---|
120 | // ignore comments
|
---|
121 | while (c == '#') {
|
---|
122 | while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
|
---|
123 | // skip white space...
|
---|
124 | while (!filein.eof() && isspace(c)) { filein.get(c); }
|
---|
125 | }
|
---|
126 |
|
---|
127 | // deal with all the records on this line (possibly multi-line)
|
---|
128 | while (!filein.eof()) {
|
---|
129 | if (c=='\n' || c=='\r') { // shouldn't happen?
|
---|
130 | break;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // get the next token
|
---|
134 | curvalue.clear();
|
---|
135 |
|
---|
136 | // see if this is a quoted phrase
|
---|
137 | if (c=='\'' || c=='\"') { // starts with a quote
|
---|
138 | char quote, old_c;
|
---|
139 | quote = c;
|
---|
140 | old_c = c;
|
---|
141 | filein.get(c);
|
---|
142 | while (!filein.eof() && (c != quote || old_c == '\\') ) {
|
---|
143 | /* Turn eol into space, in case other parsing bits expect eol to
|
---|
144 | also mean end of parsing... */
|
---|
145 | if (c=='\r' || c=='\n') c=' ';
|
---|
146 | curvalue.push_back(c);
|
---|
147 | old_c = c;
|
---|
148 | filein.get(c);
|
---|
149 | }
|
---|
150 | // get the character after the closing quote...
|
---|
151 | filein.get(c);
|
---|
152 | } else { // it's not a quoted phrase
|
---|
153 | // get the token
|
---|
154 | while (!filein.eof() && !isspace(c)) {
|
---|
155 | curvalue.push_back(c);
|
---|
156 | filein.get(c);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | // we now have a token or a phrase
|
---|
160 |
|
---|
161 | // see if we've reached the end of the line
|
---|
162 | if (c == '\n' || c == '\r') {
|
---|
163 | if (curvalue != "\\") { // the line DOESN'T continue. End of line.
|
---|
164 | values.push_back(curvalue);
|
---|
165 | break;
|
---|
166 | } else {
|
---|
167 | // swallow up the EOL chars
|
---|
168 | while (!filein.eof() && (c=='\r' || c=='\n')) filein.get(c);
|
---|
169 | }
|
---|
170 | } else { // no new line seen
|
---|
171 | values.push_back(curvalue);
|
---|
172 | }
|
---|
173 |
|
---|
174 | curvalue.clear();
|
---|
175 |
|
---|
176 | // remove whitespace (but not newline/CR chars) before next token
|
---|
177 | while (!filein.eof() && (c==' ' || c=='\t')) filein.get(c);
|
---|
178 |
|
---|
179 | } // while(1)
|
---|
180 |
|
---|
181 |
|
---|
182 | return 0;
|
---|
183 | }
|
---|