[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"
|
---|
[9515] | 27 | #include <cctype> // for isspace()
|
---|
[1076] | 28 |
|
---|
[8727] | 29 | int write_ini_line (ofstream &fileout, const text_t &key, const text_t &value) {
|
---|
[1076] | 30 | if (key.empty() || value.empty()) return -1;
|
---|
| 31 | outconvertclass text_t2ascii;
|
---|
| 32 | fileout << text_t2ascii << key << "=" << value << "\n";
|
---|
| 33 | return 0;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | int read_ini_line (ifstream &filein, text_t &key, text_t &value) {
|
---|
| 37 | if (filein.eof()) return -1;
|
---|
| 38 |
|
---|
| 39 | key.clear();
|
---|
| 40 | value.clear();
|
---|
| 41 | char c;
|
---|
| 42 | filein.get(c);
|
---|
| 43 |
|
---|
| 44 | int foundeq = 0;
|
---|
| 45 | while (!filein.eof() && c != '\n') {
|
---|
[22770] | 46 | if (!foundeq && c == '=') {
|
---|
| 47 | foundeq = 1;
|
---|
| 48 | filein.get(c);
|
---|
| 49 | // now that we've read the next character, we should check it's not \n
|
---|
| 50 | // if it is, then we had a line of the form 'hostIP=', and we should exit the loop
|
---|
| 51 | if (c == '\n') break;
|
---|
| 52 | }
|
---|
[1076] | 53 | if (foundeq) value.push_back(c);
|
---|
| 54 | else key.push_back(c);
|
---|
| 55 | filein.get(c);
|
---|
| 56 | }
|
---|
[1739] | 57 | if (key.empty()) return 0; // blank line maybe?
|
---|
[1076] | 58 | return 0;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1421] | 61 | // write out line of values to cfgfile
|
---|
| 62 | // does nothing fancy - make sure no values contain carriage returns
|
---|
| 63 | int write_cfg_line (ofstream &fileout, const text_tarray &values) {
|
---|
| 64 | outconvertclass text_t2ascii;
|
---|
[1076] | 65 |
|
---|
[1421] | 66 | text_tarray::const_iterator here = values.begin();
|
---|
| 67 | text_tarray::const_iterator end = values.end();
|
---|
| 68 |
|
---|
| 69 | bool first = true;
|
---|
| 70 | while (here != end) {
|
---|
| 71 | if (first) fileout << text_t2ascii << *here;
|
---|
| 72 | else fileout << text_t2ascii << " \"" << *here << "\"";
|
---|
| 73 | first = false;
|
---|
[8727] | 74 | ++here;
|
---|
[1421] | 75 | }
|
---|
| 76 | fileout << "\n";
|
---|
[1432] | 77 | return 0;
|
---|
[1421] | 78 | }
|
---|
[3528] | 79 |
|
---|
[3010] | 80 | // same, but use a file descriptor this time
|
---|
| 81 | int write_cfg_line (int fileout, const text_tarray &values) {
|
---|
| 82 | outconvertclass text_t2ascii;
|
---|
[1421] | 83 |
|
---|
[3010] | 84 | text_tarray::const_iterator here = values.begin();
|
---|
| 85 | text_tarray::const_iterator end = values.end();
|
---|
| 86 |
|
---|
[3528] | 87 | if (here != end) {
|
---|
| 88 | char *s=here->getcstr();
|
---|
| 89 | write(fileout,s ,here->size());
|
---|
[8727] | 90 | delete []s;
|
---|
[3528] | 91 |
|
---|
| 92 | ++here;
|
---|
| 93 | }
|
---|
[3010] | 94 | while (here != end) {
|
---|
[3528] | 95 | write(fileout, " \"", 2);
|
---|
| 96 | char *s=here->getcstr();
|
---|
| 97 | write(fileout,s ,here->size());
|
---|
[8727] | 98 | delete []s;
|
---|
[3528] | 99 | write(fileout, "\"", 1);
|
---|
[3010] | 100 |
|
---|
[3528] | 101 | ++here;
|
---|
[3010] | 102 | }
|
---|
| 103 | write(fileout,"\n",1);
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[1076] | 108 | // returns 0 on success, -1 on failure
|
---|
| 109 | int read_cfg_line (ifstream &filein, text_tarray &values) {
|
---|
[3417] | 110 | // we split up the line into tokens, pushing each token into the
|
---|
| 111 | // values array. Quoted phrases are a single token. A "\" at the end
|
---|
| 112 | // of a line continues onto the next line.
|
---|
[1076] | 113 |
|
---|
| 114 | values.erase(values.begin(), values.end());
|
---|
| 115 |
|
---|
| 116 | if (!filein.good()) return -1;
|
---|
| 117 |
|
---|
| 118 | text_t curvalue;
|
---|
[7731] | 119 | char c1;
|
---|
[7705] | 120 | filein.get(c1);
|
---|
[1076] | 121 |
|
---|
[3417] | 122 | // skip white space
|
---|
[9515] | 123 | while (!filein.eof() && isspace((unsigned char) c1)) { filein.get(c1); }
|
---|
[3417] | 124 |
|
---|
[1076] | 125 | // ignore comments
|
---|
[20710] | 126 | while (!filein.eof() && c1 == '#') {
|
---|
[7705] | 127 | while (!filein.eof() && c1!='\n' && c1!='\r') { filein.get(c1); }
|
---|
[3417] | 128 | // skip white space...
|
---|
[9515] | 129 | while (!filein.eof() && isspace((unsigned char) c1)) { filein.get(c1); }
|
---|
[3417] | 130 | }
|
---|
| 131 |
|
---|
| 132 | // deal with all the records on this line (possibly multi-line)
|
---|
[7705] | 133 |
|
---|
[3417] | 134 | while (!filein.eof()) {
|
---|
[7705] | 135 | if (c1=='\n' || c1=='\r') { // shouldn't happen?
|
---|
[3417] | 136 | break;
|
---|
[1076] | 137 | }
|
---|
| 138 |
|
---|
[3417] | 139 | // get the next token
|
---|
| 140 | curvalue.clear();
|
---|
| 141 |
|
---|
[7705] | 142 | bool inquote=false;
|
---|
[7731] | 143 | char quotemark='"';
|
---|
[7733] | 144 | char preceding='\0'; // 1-char state to allow \" and \'
|
---|
[7705] | 145 | // see if this is a quoted phrase
|
---|
| 146 | if (c1=='\'' || c1=='\"') { // starts with a quote
|
---|
| 147 | inquote=true;
|
---|
| 148 | quotemark = c1;
|
---|
| 149 | preceding = c1; // just to initialise
|
---|
| 150 | filein.get(c1);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // get token or a whole phrase
|
---|
| 154 | while (!filein.eof()) {
|
---|
[9515] | 155 | if (isspace((unsigned char) c1)) {
|
---|
[7705] | 156 | if (! inquote) {
|
---|
| 157 | // end of token, not inside quote marks
|
---|
| 158 | break;
|
---|
| 159 | } else {
|
---|
| 160 | // inside quote marks.
|
---|
| 161 | /* Turn eol into space, in case other parsing bits expect eol to
|
---|
| 162 | also mean end of parsing... */
|
---|
| 163 | c1=' ';
|
---|
| 164 | }
|
---|
[3417] | 165 | }
|
---|
[7705] | 166 | if (c1 == quotemark && inquote && preceding != '\\') {
|
---|
| 167 | // end of quoted phrase found
|
---|
| 168 | inquote=false;
|
---|
| 169 | filein.get(c1);
|
---|
| 170 | continue;
|
---|
[3417] | 171 | }
|
---|
[7705] | 172 |
|
---|
| 173 | // add current char to token/phrase
|
---|
| 174 | // see if current byte is part of a multibyte char (utf-8 only!)
|
---|
| 175 | unsigned short int c; // text_t uses 16bit unicode
|
---|
[7731] | 176 | unsigned char uc1=(unsigned)c1;
|
---|
| 177 | if (uc1 < 0x80) {
|
---|
| 178 | c=uc1;
|
---|
| 179 | } else if (uc1 >= 0xc0 && uc1 <= 0xdf) {
|
---|
[7705] | 180 | // 2-byte utf-8
|
---|
| 181 | unsigned char c2;
|
---|
| 182 | // two byte character
|
---|
[7733] | 183 | filein.get((char&)c2); // get takes a signed char
|
---|
[7731] | 184 | c = ((uc1 & 0x1f) << 6) + (c2 & 0x3f);
|
---|
| 185 | } else if (uc1 >= 0xe0 && uc1 <= 0xef) {
|
---|
[7705] | 186 | // 3-byte character
|
---|
| 187 | unsigned char c2, c3;
|
---|
[7733] | 188 | filein.get((char&)c2);
|
---|
| 189 | filein.get((char&)c3);
|
---|
[7731] | 190 | c = ((uc1 & 0xf) << 12) + ((c2 & 0x3f) << 6)
|
---|
| 191 | + (c3 & 0x3f);
|
---|
[7733] | 192 | } else {c=uc1;} // we don't do group2/plane0 (4,5,6-byte utf-8)
|
---|
[7705] | 193 |
|
---|
| 194 | curvalue.push_back(c); // 16bit unicode
|
---|
| 195 | if (inquote)
|
---|
| 196 | preceding = c1;
|
---|
| 197 |
|
---|
| 198 | filein.get(c1);
|
---|
[3417] | 199 | }
|
---|
| 200 | // we now have a token or a phrase
|
---|
| 201 |
|
---|
| 202 | // see if we've reached the end of the line
|
---|
[7705] | 203 | if (c1 == '\n' || c1 == '\r') {
|
---|
[3417] | 204 | if (curvalue != "\\") { // the line DOESN'T continue. End of line.
|
---|
| 205 | values.push_back(curvalue);
|
---|
[7705] | 206 | break; // end of token/phrase
|
---|
[3521] | 207 | } else {
|
---|
| 208 | // swallow up the EOL chars
|
---|
[7705] | 209 | while (!filein.eof() && (c1=='\r' || c1=='\n')) filein.get(c1);
|
---|
| 210 | // the current token "\\" will be cleared below
|
---|
[3417] | 211 | }
|
---|
| 212 | } else { // no new line seen
|
---|
| 213 | values.push_back(curvalue);
|
---|
| 214 | }
|
---|
[1076] | 215 |
|
---|
[3417] | 216 | curvalue.clear();
|
---|
[1076] | 217 |
|
---|
[3521] | 218 | // remove whitespace (but not newline/CR chars) before next token
|
---|
[7705] | 219 | while (!filein.eof() && (c1==' ' || c1=='\t')) filein.get(c1);
|
---|
[1076] | 220 |
|
---|
[3417] | 221 | } // while(1)
|
---|
| 222 |
|
---|
[1076] | 223 |
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|