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 534 1999-09-07 04:57:43Z sjboddie $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.5 1999/09/07 04:57:41 sjboddie
|
---|
31 | added gpl notice
|
---|
32 |
|
---|
33 | Revision 1.4 1999/02/02 09:54:26 rjmcnab
|
---|
34 |
|
---|
35 | Changed the configuration file reader so that any space can be used to
|
---|
36 | seperate items and so that quotes can be used to contain items. I also
|
---|
37 | allowed a '\' to indicate that the line continues on the next line.
|
---|
38 |
|
---|
39 | Revision 1.3 1999/01/12 01:50:57 rjmcnab
|
---|
40 |
|
---|
41 | Standard header.
|
---|
42 |
|
---|
43 | Revision 1.2 1999/01/08 02:33:13 rjmcnab
|
---|
44 |
|
---|
45 | Added standard header to source files.
|
---|
46 |
|
---|
47 | */
|
---|
48 |
|
---|
49 |
|
---|
50 | #include "cfgread.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | // returns 0 on success, -1 on failure
|
---|
54 | int read_cfg_line (ifstream &filein, text_tarray &values) {
|
---|
55 | // outconvertclass text_t2ascii;
|
---|
56 |
|
---|
57 | values.erase(values.begin(), values.end());
|
---|
58 |
|
---|
59 | if (!filein.good()) return -1;
|
---|
60 |
|
---|
61 | text_t curvalue;
|
---|
62 | char c, quote;
|
---|
63 |
|
---|
64 | bool linecontinues = true;
|
---|
65 | filein.get(c);
|
---|
66 | while (!filein.eof() && linecontinues) {
|
---|
67 | // eat up all return characters
|
---|
68 | while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
|
---|
69 |
|
---|
70 | // ignore comments
|
---|
71 | if (c == '#') {
|
---|
72 | while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
|
---|
73 | }
|
---|
74 |
|
---|
75 | // deal with all the records on this line
|
---|
76 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
77 | // ignore white space
|
---|
78 | while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
|
---|
79 |
|
---|
80 | // get the next record
|
---|
81 | if (c!='\n' && c!='\r') {
|
---|
82 | curvalue.clear();
|
---|
83 | quote = 0;
|
---|
84 |
|
---|
85 | // see if this is a quoted record
|
---|
86 | if (c=='\'' || c=='\"') {
|
---|
87 | quote = c;
|
---|
88 | filein.get(c);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // get the value
|
---|
92 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
93 | if (quote != 0 && c == quote) { // end of quote
|
---|
94 | filein.get(c);
|
---|
95 | break;
|
---|
96 | } else if (quote == 0 && (c == ' ' || c == '\t')) {
|
---|
97 | break; // space seperator
|
---|
98 | }
|
---|
99 | curvalue.push_back(c);
|
---|
100 | filein.get(c);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // see if the record continues on the next line
|
---|
104 | if ((c=='\n' || c=='\r') && curvalue=="\\") {
|
---|
105 | linecontinues = true;
|
---|
106 | } else {
|
---|
107 | // cerr << text_t2ascii << "\"" << curvalue << "\" ";
|
---|
108 |
|
---|
109 | values.push_back(curvalue);
|
---|
110 | curvalue.clear();
|
---|
111 | linecontinues = false;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | // cerr << "\n";
|
---|
118 |
|
---|
119 | return 0;
|
---|
120 | }
|
---|