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 |
|
---|
28 | int 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 |
|
---|
35 | int 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 -1;
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | // returns 0 on success, -1 on failure
|
---|
57 | int read_cfg_line (ifstream &filein, text_tarray &values) {
|
---|
58 | // outconvertclass text_t2ascii;
|
---|
59 |
|
---|
60 | values.erase(values.begin(), values.end());
|
---|
61 |
|
---|
62 | if (!filein.good()) return -1;
|
---|
63 |
|
---|
64 | text_t curvalue;
|
---|
65 | char c, quote;
|
---|
66 |
|
---|
67 | bool linecontinues = true;
|
---|
68 | filein.get(c);
|
---|
69 | while (!filein.eof() && linecontinues) {
|
---|
70 | // eat up all return characters
|
---|
71 | while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
|
---|
72 |
|
---|
73 | // ignore comments
|
---|
74 | if (c == '#') {
|
---|
75 | while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
|
---|
76 | }
|
---|
77 |
|
---|
78 | // deal with all the records on this line
|
---|
79 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
80 | // ignore white space
|
---|
81 | while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
|
---|
82 |
|
---|
83 | // get the next record
|
---|
84 | if (c!='\n' && c!='\r') {
|
---|
85 | curvalue.clear();
|
---|
86 | quote = 0;
|
---|
87 |
|
---|
88 | // see if this is a quoted record
|
---|
89 | if (c=='\'' || c=='\"') {
|
---|
90 | quote = c;
|
---|
91 | filein.get(c);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // get the value
|
---|
95 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
96 | if (quote != 0 && c == quote) { // end of quote
|
---|
97 | filein.get(c);
|
---|
98 | break;
|
---|
99 | } else if (quote == 0 && (c == ' ' || c == '\t')) {
|
---|
100 | break; // space seperator
|
---|
101 | }
|
---|
102 | curvalue.push_back(c);
|
---|
103 | filein.get(c);
|
---|
104 | }
|
---|
105 |
|
---|
106 | // see if the record continues on the next line
|
---|
107 | if ((c=='\n' || c=='\r') && curvalue=="\\") {
|
---|
108 | linecontinues = true;
|
---|
109 | } else {
|
---|
110 | // cerr << text_t2ascii << "\"" << curvalue << "\" ";
|
---|
111 |
|
---|
112 | values.push_back(curvalue);
|
---|
113 | curvalue.clear();
|
---|
114 | linecontinues = false;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | // cerr << "\n";
|
---|
121 |
|
---|
122 | return 0;
|
---|
123 | }
|
---|