source: main/trunk/greenstone2/common-src/src/lib/cfgread.cpp

Last change on this file was 22770, checked in by ak19, 14 years ago

More changes for ticket 152: making the collectdir moveable. When using server.exe, if GLI was last shutdown when another collectdir was active, this needs to be read in from the user config.xml by GLI, and server.exe needs to use it.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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 *********************************************************************/
25
26#include "cfgread.h"
27#include <cctype> // for isspace()
28
29int 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
36int 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 == '=') {
47 foundeq = 1;
48 filein.get(c);
49 // now that we've read the next character, we should check it's not \n
50 // if it is, then we had a line of the form 'hostIP=', and we should exit the loop
51 if (c == '\n') break;
52 }
53 if (foundeq) value.push_back(c);
54 else key.push_back(c);
55 filein.get(c);
56 }
57 if (key.empty()) return 0; // blank line maybe?
58 return 0;
59}
60
61// write out line of values to cfgfile
62// does nothing fancy - make sure no values contain carriage returns
63int write_cfg_line (ofstream &fileout, const text_tarray &values) {
64 outconvertclass text_t2ascii;
65
66 text_tarray::const_iterator here = values.begin();
67 text_tarray::const_iterator end = values.end();
68
69 bool first = true;
70 while (here != end) {
71 if (first) fileout << text_t2ascii << *here;
72 else fileout << text_t2ascii << " \"" << *here << "\"";
73 first = false;
74 ++here;
75 }
76 fileout << "\n";
77 return 0;
78}
79
80// same, but use a file descriptor this time
81int write_cfg_line (int fileout, const text_tarray &values) {
82 outconvertclass text_t2ascii;
83
84 text_tarray::const_iterator here = values.begin();
85 text_tarray::const_iterator end = values.end();
86
87 if (here != end) {
88 char *s=here->getcstr();
89 write(fileout,s ,here->size());
90 delete []s;
91
92 ++here;
93 }
94 while (here != end) {
95 write(fileout, " \"", 2);
96 char *s=here->getcstr();
97 write(fileout,s ,here->size());
98 delete []s;
99 write(fileout, "\"", 1);
100
101 ++here;
102 }
103 write(fileout,"\n",1);
104 return 0;
105}
106
107
108// returns 0 on success, -1 on failure
109int read_cfg_line (ifstream &filein, text_tarray &values) {
110 // we split up the line into tokens, pushing each token into the
111 // values array. Quoted phrases are a single token. A "\" at the end
112 // of a line continues onto the next line.
113
114 values.erase(values.begin(), values.end());
115
116 if (!filein.good()) return -1;
117
118 text_t curvalue;
119 char c1;
120 filein.get(c1);
121
122 // skip white space
123 while (!filein.eof() && isspace((unsigned char) c1)) { filein.get(c1); }
124
125 // ignore comments
126 while (!filein.eof() && c1 == '#') {
127 while (!filein.eof() && c1!='\n' && c1!='\r') { filein.get(c1); }
128 // skip white space...
129 while (!filein.eof() && isspace((unsigned char) c1)) { filein.get(c1); }
130 }
131
132 // deal with all the records on this line (possibly multi-line)
133
134 while (!filein.eof()) {
135 if (c1=='\n' || c1=='\r') { // shouldn't happen?
136 break;
137 }
138
139 // get the next token
140 curvalue.clear();
141
142 bool inquote=false;
143 char quotemark='"';
144 char preceding='\0'; // 1-char state to allow \" and \'
145 // see if this is a quoted phrase
146 if (c1=='\'' || c1=='\"') { // starts with a quote
147 inquote=true;
148 quotemark = c1;
149 preceding = c1; // just to initialise
150 filein.get(c1);
151 }
152
153 // get token or a whole phrase
154 while (!filein.eof()) {
155 if (isspace((unsigned char) c1)) {
156 if (! inquote) {
157 // end of token, not inside quote marks
158 break;
159 } else {
160 // inside quote marks.
161 /* Turn eol into space, in case other parsing bits expect eol to
162 also mean end of parsing... */
163 c1=' ';
164 }
165 }
166 if (c1 == quotemark && inquote && preceding != '\\') {
167 // end of quoted phrase found
168 inquote=false;
169 filein.get(c1);
170 continue;
171 }
172
173 // add current char to token/phrase
174 // see if current byte is part of a multibyte char (utf-8 only!)
175 unsigned short int c; // text_t uses 16bit unicode
176 unsigned char uc1=(unsigned)c1;
177 if (uc1 < 0x80) {
178 c=uc1;
179 } else if (uc1 >= 0xc0 && uc1 <= 0xdf) {
180 // 2-byte utf-8
181 unsigned char c2;
182 // two byte character
183 filein.get((char&)c2); // get takes a signed char
184 c = ((uc1 & 0x1f) << 6) + (c2 & 0x3f);
185 } else if (uc1 >= 0xe0 && uc1 <= 0xef) {
186 // 3-byte character
187 unsigned char c2, c3;
188 filein.get((char&)c2);
189 filein.get((char&)c3);
190 c = ((uc1 & 0xf) << 12) + ((c2 & 0x3f) << 6)
191 + (c3 & 0x3f);
192 } else {c=uc1;} // we don't do group2/plane0 (4,5,6-byte utf-8)
193
194 curvalue.push_back(c); // 16bit unicode
195 if (inquote)
196 preceding = c1;
197
198 filein.get(c1);
199 }
200 // we now have a token or a phrase
201
202 // see if we've reached the end of the line
203 if (c1 == '\n' || c1 == '\r') {
204 if (curvalue != "\\") { // the line DOESN'T continue. End of line.
205 values.push_back(curvalue);
206 break; // end of token/phrase
207 } else {
208 // swallow up the EOL chars
209 while (!filein.eof() && (c1=='\r' || c1=='\n')) filein.get(c1);
210 // the current token "\\" will be cleared below
211 }
212 } else { // no new line seen
213 values.push_back(curvalue);
214 }
215
216 curvalue.clear();
217
218 // remove whitespace (but not newline/CR chars) before next token
219 while (!filein.eof() && (c1==' ' || c1=='\t')) filein.get(c1);
220
221 } // while(1)
222
223
224 return 0;
225}
Note: See TracBrowser for help on using the repository browser.