Source: fun/KeyValueParser.h


Annotated List
Files
Globals
Hierarchy
Index
// KeyValueParser.h
#ifndef SGB_KEYVALUEPARSER_H
#define SGB_KEYVALUEPARSER_H

#include 
#include 
#include 
using namespace std;
/**
License: Public Domain
Author: stephan@wanderinghorse.net
*/

// typedef std::map StringStringMap;
// typedef StringStringMap::iterator StringStringIterator;
// typedef StringStringMap::const_iterator StringStringConstIterator;
// typedef StringStringMap::value_type StringStringPair;

// typedef std::map TrueMap;
// typedef TrueMap::iterator TrueMapIterator;
// typedef TrueMap::value_type TrueMapPair;


// typedef std::map StringBoolMap;
// typedef StringBoolMap::iterator StringBoolIterator;
// typedef StringBoolMap::value_type StringBoolPair;

// typedef std::list StringList;
// typedef StringList::iterator StringListIterator;

/**
KeyValueParser is a class for parsing "key=value"-style lines,
like those which would come from a configuration file.
*/
class KeyValueParser
{
 public:
   KeyValueParser( const string &line = "" );
    virtual ~KeyValueParser() {}; // for polymorphism

    // enters k.getKey()=k.getValue() os.
    friend std::ostream & operator << (std::ostream &os, const KeyValueParser &);

    /**
       Parses 'line' into a key/value pair. To be parseable the line must be in the form:
         key=value
       Extra whitespace around the '=' are considered to be part of the key and/or value,
       but this is a bug/missing feature.
       If alternateDelimiter is a non-empty string then a line in the format:
         key{alternateDelimiter}value...
       (minus the braces) is also accepted as parseable. A key=value form always has precedence,
       however, so keys with spaces in them will work:
         some key=some value
       Note: the alternateDelimiter feature is Largely Untested.
       This function returns false if it does not consider the line to be parseable.
       Use getKey() and getValue() to get the parsed values. Use getLine() to get the whole
       string passed to parse (as if you'd ever need it, though subclasses might).
       Note that getLine() is set regardless of whether this function returns true or false,
       but the key and value are only set if this function returns true.
       This function will return false if a line contains no key (like '=value').
    */
    bool parse( const string &line, const string & alternateDelimiter = "" );
    const string &getKey() const;
    const string &getValue() const;
    const string &getLine() const;

 private:
        string m_key;
        string m_val;
        string m_line;
        typedef std::map CharBoolMap;
        typedef CharBoolMap::iterator CharBoolMapIterator;
        typedef CharBoolMap::value_type CharBoolPair;

        static CharBoolMap commentChars;
        CharBoolMapIterator commentIt;
        string::const_iterator strIt;
};
#endif // SGB_KEYVALUEPARSER_H

Generated by: stephan on cheyenne on Mon Aug 11 14:06:52 2003, using kdoc 2.0a54.