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

Last change on this file since 3010 was 3010, checked in by jrm21, 22 years ago

overloaded write_cfg_line() to take a file descriptor as argument instead
of an ofstream.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 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
28int write_ini_line (ofstream &fileout, const text_t &key, const text_t value) {
29 if (key.empty() || value.empty()) return -1;
30 outconvertclass text_t2ascii;
31 fileout << text_t2ascii << key << "=" << value << "\n";
32 return 0;
33}
34
35int read_ini_line (ifstream &filein, text_t &key, text_t &value) {
36 if (filein.eof()) return -1;
37
38 key.clear();
39 value.clear();
40 char c;
41 filein.get(c);
42
43 int foundeq = 0;
44 while (!filein.eof() && c != '\n') {
45 if (!foundeq && c == '=') {foundeq = 1; filein.get(c);}
46
47 if (foundeq) value.push_back(c);
48 else key.push_back(c);
49 filein.get(c);
50 }
51 if (key.empty()) return 0; // blank line maybe?
52 return 0;
53}
54
55// write out line of values to cfgfile
56// does nothing fancy - make sure no values contain carriage returns
57int write_cfg_line (ofstream &fileout, const text_tarray &values) {
58 outconvertclass text_t2ascii;
59
60 text_tarray::const_iterator here = values.begin();
61 text_tarray::const_iterator end = values.end();
62
63 bool first = true;
64 while (here != end) {
65 if (first) fileout << text_t2ascii << *here;
66 else fileout << text_t2ascii << " \"" << *here << "\"";
67 first = false;
68 here ++;
69 }
70 fileout << "\n";
71 return 0;
72}
73// same, but use a file descriptor this time
74int write_cfg_line (int fileout, const text_tarray &values) {
75 outconvertclass text_t2ascii;
76
77 text_tarray::const_iterator here = values.begin();
78 text_tarray::const_iterator end = values.end();
79
80 bool first = true;
81 while (here != end) {
82 if (first) text_t2ascii << *here;
83 else text_t2ascii << " \"" << *here << "\"";
84
85 size_t buffersize=here->size() + 4; // +4 in case of " and spaces
86 char *buffer=new char[buffersize];
87 size_t num_chars;
88 convertclass::status_t status;
89 text_t2ascii.convert(buffer, buffersize, num_chars, status);
90 // ignore status - assume it is "finished" as buffer is big enough
91 write(fileout, buffer, num_chars);
92 delete buffer;
93 first = false;
94 here ++;
95 }
96 write(fileout,"\n",1);
97 return 0;
98}
99
100
101// returns 0 on success, -1 on failure
102int read_cfg_line (ifstream &filein, text_tarray &values) {
103 // outconvertclass text_t2ascii;
104
105 values.erase(values.begin(), values.end());
106
107 if (!filein.good()) return -1;
108
109 text_t curvalue;
110 char c, quote;
111
112 bool linecontinues = true;
113 filein.get(c);
114 while (!filein.eof() && linecontinues) {
115 // eat up all return characters
116 while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
117
118 // ignore comments
119 if (c == '#') {
120 while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
121 }
122
123 // deal with all the records on this line
124 while (!filein.eof() && c!='\n' && c!='\r') {
125 // ignore white space
126 while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
127
128 // get the next record
129 if (c!='\n' && c!='\r') {
130 curvalue.clear();
131 quote = 0;
132
133 // see if this is a quoted record
134 if (c=='\'' || c=='\"') {
135 quote = c;
136 filein.get(c);
137 }
138
139 // get the value
140 while (!filein.eof() && c!='\n' && c!='\r') {
141 if (quote != 0 && c == quote) { // end of quote
142 filein.get(c);
143 break;
144 } else if (quote == 0 && (c == ' ' || c == '\t')) {
145 break; // space seperator
146 }
147 curvalue.push_back(c);
148 filein.get(c);
149 }
150
151 // see if the record continues on the next line
152 if ((c=='\n' || c=='\r') && curvalue=="\\") {
153 linecontinues = true;
154 } else {
155 // cerr << text_t2ascii << "\"" << curvalue << "\" ";
156
157 values.push_back(curvalue);
158 curvalue.clear();
159 linecontinues = false;
160 }
161 }
162 }
163 }
164
165 // cerr << "\n";
166
167 return 0;
168}
Note: See TracBrowser for help on using the repository browser.