/********************************************************************** * * cfgread.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * A component of the Greenstone digital library software * from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: cfgread.cpp 1076 2000-04-06 19:58:04Z cs025 $ * *********************************************************************/ /* $Log$ Revision 1.8 2000/04/06 19:57:57 cs025 Correcting a correction - reinstated all lib files due to silly CVS confusion. Revision 1.6 1999/12/05 21:08:28 sjboddie added support for reading and writing windows ini files Revision 1.5 1999/09/07 04:57:41 sjboddie added gpl notice Revision 1.4 1999/02/02 09:54:26 rjmcnab Changed the configuration file reader so that any space can be used to seperate items and so that quotes can be used to contain items. I also allowed a '\' to indicate that the line continues on the next line. Revision 1.3 1999/01/12 01:50:57 rjmcnab Standard header. Revision 1.2 1999/01/08 02:33:13 rjmcnab Added standard header to source files. */ #include "cfgread.h" void collectioninfo_t::clear () { gsdl_gsdlhome.clear(); gsdl_gdbmhome.clear(); } int write_ini_line (ofstream &fileout, const text_t &key, const text_t value) { if (key.empty() || value.empty()) return -1; outconvertclass text_t2ascii; fileout << text_t2ascii << key << "=" << value << "\n"; return 0; } int read_ini_line (ifstream &filein, text_t &key, text_t &value) { if (filein.eof()) return -1; key.clear(); value.clear(); char c; filein.get(c); int foundeq = 0; while (!filein.eof() && c != '\n') { if (!foundeq && c == '=') {foundeq = 1; filein.get(c);} if (foundeq) value.push_back(c); else key.push_back(c); filein.get(c); } if (key.empty()) return -1; return 0; } // returns 0 on success, -1 on failure int read_cfg_line (ifstream &filein, text_tarray &values) { // outconvertclass text_t2ascii; values.erase(values.begin(), values.end()); if (!filein.good()) return -1; text_t curvalue; char c, quote; bool linecontinues = true; filein.get(c); while (!filein.eof() && linecontinues) { // eat up all return characters while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); } // ignore comments if (c == '#') { while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); } } // deal with all the records on this line while (!filein.eof() && c!='\n' && c!='\r') { // ignore white space while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); } // get the next record if (c!='\n' && c!='\r') { curvalue.clear(); quote = 0; // see if this is a quoted record if (c=='\'' || c=='\"') { quote = c; filein.get(c); } // get the value while (!filein.eof() && c!='\n' && c!='\r') { if (quote != 0 && c == quote) { // end of quote filein.get(c); break; } else if (quote == 0 && (c == ' ' || c == '\t')) { break; // space seperator } curvalue.push_back(c); filein.get(c); } // see if the record continues on the next line if ((c=='\n' || c=='\r') && curvalue=="\\") { linecontinues = true; } else { // cerr << text_t2ascii << "\"" << curvalue << "\" "; values.push_back(curvalue); curvalue.clear(); linecontinues = false; } } } } // cerr << "\n"; return 0; }