Changeset 3417


Ignore:
Timestamp:
2002-09-10T17:09:10+12:00 (22 years ago)
Author:
jrm21
Message:

Allow multi-line entries in config files - either ends with a "\", or
there is a newline character within the quote marks. Eg

format DocumentText \
"

<p>[foo]
_If_([bar], [bar], No bar)
</p>
<p>
[Text]

"

is now parsed as a single line, with embedded newline characters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/lib/cfgread.cpp

    r3010 r3417  
    2525
    2626#include "cfgread.h"
     27#include <ctype.h> // for isspace()
    2728
    2829int write_ini_line (ofstream &fileout, const text_t &key, const text_t value) {
     
    9091    // ignore status - assume it is "finished" as buffer is big enough
    9192    write(fileout, buffer, num_chars);
    92     delete buffer;
     93    delete[] buffer;
    9394    first = false;
    9495    here ++;
     
    101102// returns 0 on success, -1 on failure
    102103int read_cfg_line (ifstream &filein, text_tarray &values) {
    103   //  outconvertclass text_t2ascii;
     104  // we split up the line into tokens, pushing each token into the
     105  // values array. Quoted phrases are a single token. A "\" at the end
     106  // of a line continues onto the next line.
    104107
    105108  values.erase(values.begin(), values.end());
     
    108111
    109112  text_t curvalue;
    110   char c, quote;
     113  char c;
     114  filein.get(c);
    111115
    112   bool linecontinues = true;
    113   filein.get(c);
    114   while (!filein.eof() && linecontinues) {
    115     // eat up all return characters
    116     while (!filein.eof() && (c=='\n' || c=='\r')) { filein.get(c); }
     116  // skip white space
     117  while (!filein.eof() && isspace(c)) { filein.get(c); }
    117118
    118119    // ignore comments
    119     if (c == '#') {
    120       while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
     120  while (c == '#') {
     121    while (!filein.eof() && c!='\n' && c!='\r') { filein.get(c); }
     122    // skip white space...
     123    while (!filein.eof() && isspace(c)) { filein.get(c); }
     124  }
     125
     126  // deal with all the records on this line (possibly multi-line)
     127  while (!filein.eof()) {
     128    if (c=='\n' || c=='\r') { // shouldn't happen?
     129      break;
    121130    }
    122131
    123     // deal with all the records on this line
    124     while (!filein.eof() && c!='\n' && c!='\r') {
    125       // ignore white space
    126       while (!filein.eof() && (c==' ' || c=='\t')) { filein.get(c); }
    127 
    128       // get the next record
    129       if (c!='\n' && c!='\r') {
    130     curvalue.clear();
    131     quote = 0;
    132 
    133     // see if this is a quoted record
    134     if (c=='\'' || c=='\"') {
    135       quote = c;
    136       filein.get(c);
    137     }
    138 
    139     // get the value
    140     while (!filein.eof() && c!='\n' && c!='\r') {
    141       if (quote != 0 && c == quote) { // end of quote
    142         filein.get(c);
    143         break;
    144       } else if (quote == 0 && (c == ' ' || c == '\t')) {
    145         break; // space seperator
    146       }
    147       curvalue.push_back(c);
    148       filein.get(c);
    149     }
    150 
    151     // see if the record continues on the next line
    152     if ((c=='\n' || c=='\r') && curvalue=="\\") {
    153       linecontinues = true;
    154     } else {
    155       //      cerr << text_t2ascii << "\"" << curvalue << "\" ";
    156      
    157       values.push_back(curvalue);
    158       curvalue.clear();
    159       linecontinues = false;
    160     }
     132    // get the next token
     133    curvalue.clear();
     134     
     135      // see if this is a quoted phrase
     136    if (c=='\'' || c=='\"') { // starts with a quote
     137      char quote, old_c;
     138      quote = c;
     139      old_c = c;
     140      filein.get(c);
     141      while (!filein.eof() && (c != quote || old_c == '\\') ) {
     142    curvalue.push_back(c);
     143    old_c = c;
     144    filein.get(c);
     145      }
     146      // get the character after the closing quote...
     147      filein.get(c);
     148    } else { // it's not a quoted phrase
     149      // get the token
     150      while (!filein.eof() && !isspace(c)) {
     151    curvalue.push_back(c);
     152    filein.get(c);
    161153      }
    162154    }
    163   }
     155    // we now have a token or a phrase
     156   
     157    // see if we've reached the end of the line
     158    if (c == '\n' || c == '\r') {
     159      if (curvalue != "\\") { // the line DOESN'T continue. End of line.
     160    values.push_back(curvalue);
     161    break;
     162      }
     163    } else { // no new line seen
     164      values.push_back(curvalue);
     165    }
    164166
    165   //  cerr << "\n";
     167    curvalue.clear();
     168
     169    //  the record continues on the next line
     170    // remove whitespace before next token
     171    while (!filein.eof() && isspace(c)) filein.get(c);
     172
     173  } // while(1)
     174 
    166175
    167176  return 0;
Note: See TracChangeset for help on using the changeset viewer.