Changes between Version 3 and Version 4 of DataParser


Ignore:
Timestamp:
2017-10-26T17:14:31Z (4 weeks ago)
Author:
peter
Comment:

Add some more sections.

Legend:

Unmodified
Added
Removed
Modified
  • DataParser

    v3 v4  
    142142}
    143143}}}
    144 The routine getTokenOptional() will return true as long as reading another number was successful. It will not generate an error if reading the number fails, and return false instead.
     144The routine getTokenOptional() will return true as long as reading another number was successful. It will not generate an error if reading the number fails, and return false instead. If reading the number fails, the token will be set to a default constructed instance of the relevant type (zero for integer and floating point numbers, an empty string for strings, etc.).
     145
     146For certain data items, a separate method has been created. The first allows you to read quoted strings:
     147{{{
     148string text;
     149d.getQuote(text);
     150}}}
     151This method will read a string between double quotes, which allows you to read strings with whitespace embedded. If the first or the second double quote cannot be found, the code will abort. There is also an optional version of this method:
     152{{{
     153string text;
     154bool success = d.getQuoteOptional(text);
     155}}}
     156This method will return true if reading was successful, and false otherwise. In the latter case, the variable text will be set to an empty string.
    145157
    146158== Skipping parts of the line ==
     159
     160The basic philosophy of the data parser is that tokens are read from the line in consecutive order. This can result in a performance penalty if the majority of those tokens are not needed. To alleviate this problem, it is possible to skip over part of the line. Two methods are available to achieve this. The first allows you to skip to a specified column number. Note that the column numbering follows the usual C style, i.e., the first column has number 0. The syntax is very simple:
     161{{{
     162long index;
     163d.getToken(index); // read first integer on the line
     164d.skipTo(56);      // skip to column 56
     165realnum Aul;
     166d.getToken(Aul);   // read next token...
     167}}}
     168Note that skipping backwards is not allowed (this wouldn't make any sense). Skipping beyond the end of the line will raise the EOL flag and all subsequent attempts to read a token will fail. This method is obviously only useful for data files that are strictly aligned on columns.
     169
     170The second method is to skip after a specified string:
     171{{{
     172d.getToken(index);  // read first integer on the line
     173d.skipAfter("Aul:"); // skip after string "Aul"
     174realnum Aul;
     175d.getToken(Aul);    // read next token...
     176}}}
     177The parser will search for the first instance of the string "Aul" on the line and position itself immediately after that text. If the string cannot be found, the code will abort. The search is case sensitive, as is the case for all actions of the data parser. This method can be called multiple times. Each time it will search the first instance of the requested string after the current position, so even if the same string occurs multiple times on a line, each call will skip to the next instance of that string.
     178
     179If the data items you are not interested in are at the end of the line, you can simply stop parsing and continue with the next line. The parser will not detect that the line was not fully parsed unless you explicitly check that yourself (see below).
    147180
    148181== Generating error messages and warnings ==