[1076] | 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 | }
|
---|
[1739] | 51 | if (key.empty()) return 0; // blank line maybe?
|
---|
[1076] | 52 | return 0;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1421] | 55 | // write out line of values to cfgfile
|
---|
| 56 | // does nothing fancy - make sure no values contain carriage returns
|
---|
| 57 | int write_cfg_line (ofstream &fileout, const text_tarray &values) {
|
---|
| 58 | outconvertclass text_t2ascii;
|
---|
[1076] | 59 |
|
---|
[1421] | 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";
|
---|
[1432] | 71 | return 0;
|
---|
[1421] | 72 | }
|
---|
| 73 |
|
---|
[1076] | 74 | // returns 0 on success, -1 on failure
|
---|
| 75 | int read_cfg_line (ifstream &filein, text_tarray &values) {
|
---|
| 76 | // outconvertclass text_t2ascii;
|
---|
| 77 |
|
---|
| 78 | values.erase(values.begin(), values.end());
|
---|
| 79 |
|
---|
| 80 | if (!filein.good()) return -1;
|
---|
| 81 |
|
---|
| 82 | text_t curvalue;
|
---|
| 83 | char c, quote;
|
---|
| 84 |
|
---|
| 85 | bool linecontinues = true;
|
---|
| 86 | filein.get(c);
|
---|
| 87 | while (!filein.eof() && linecontinues) {
|
---|
| 88 | // eat up all return characters
|
---|
| 89 | while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
|
---|
| 90 |
|
---|
| 91 | // ignore comments
|
---|
| 92 | if (c == '#') {
|
---|
| 93 | while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // deal with all the records on this line
|
---|
| 97 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
| 98 | // ignore white space
|
---|
| 99 | while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
|
---|
| 100 |
|
---|
| 101 | // get the next record
|
---|
| 102 | if (c!='\n' && c!='\r') {
|
---|
| 103 | curvalue.clear();
|
---|
| 104 | quote = 0;
|
---|
| 105 |
|
---|
| 106 | // see if this is a quoted record
|
---|
| 107 | if (c=='\'' || c=='\"') {
|
---|
| 108 | quote = c;
|
---|
| 109 | filein.get(c);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // get the value
|
---|
| 113 | while (!filein.eof() && c!='\n' && c!='\r') {
|
---|
| 114 | if (quote != 0 && c == quote) { // end of quote
|
---|
| 115 | filein.get(c);
|
---|
| 116 | break;
|
---|
| 117 | } else if (quote == 0 && (c == ' ' || c == '\t')) {
|
---|
| 118 | break; // space seperator
|
---|
| 119 | }
|
---|
| 120 | curvalue.push_back(c);
|
---|
| 121 | filein.get(c);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // see if the record continues on the next line
|
---|
| 125 | if ((c=='\n' || c=='\r') && curvalue=="\\") {
|
---|
| 126 | linecontinues = true;
|
---|
| 127 | } else {
|
---|
| 128 | // cerr << text_t2ascii << "\"" << curvalue << "\" ";
|
---|
| 129 |
|
---|
| 130 | values.push_back(curvalue);
|
---|
| 131 | curvalue.clear();
|
---|
| 132 | linecontinues = false;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | // cerr << "\n";
|
---|
| 139 |
|
---|
| 140 | return 0;
|
---|
| 141 | }
|
---|