Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

KeyValueParser.h

00001 // KeyValueParser.h
00002 #ifndef SGB_KEYVALUEPARSER_H
00003 #define SGB_KEYVALUEPARSER_H
00004 
00005 #include <string>
00006 #include <map>
00007 #include <list>
00008 using namespace std;
00009 /**
00010 License: Public Domain
00011 Author: stephan@wanderinghorse.net
00012 */
00013 
00014 // typedef std::map<std::string,std::string> StringStringMap;
00015 // typedef StringStringMap::iterator StringStringIterator;
00016 // typedef StringStringMap::const_iterator StringStringConstIterator;
00017 // typedef StringStringMap::value_type StringStringPair;
00018 
00019 // typedef std::map<std::string,bool> TrueMap;
00020 // typedef TrueMap::iterator TrueMapIterator;
00021 // typedef TrueMap::value_type TrueMapPair;
00022 
00023 
00024 // typedef std::map<std::string,bool> StringBoolMap;
00025 // typedef StringBoolMap::iterator StringBoolIterator;
00026 // typedef StringBoolMap::value_type StringBoolPair;
00027 
00028 // typedef std::list<std::string> StringList;
00029 // typedef StringList::iterator StringListIterator;
00030 
00031 /**
00032 KeyValueParser is a class for parsing "key=value"-style lines,
00033 like those which would come from a configuration file.
00034 */
00035 class KeyValueParser
00036 {
00037  public:
00038    KeyValueParser( const string &line = "" );
00039     virtual ~KeyValueParser() {}; // for polymorphism
00040 
00041     // enters k.getKey()=k.getValue() os.
00042     friend std::ostream & operator << (std::ostream &os, const KeyValueParser &);
00043 
00044     /**
00045        Parses 'line' into a key/value pair. To be parseable the line must be in the form:
00046          key=value
00047        Extra whitespace around the '=' are considered to be part of the key and/or value,
00048        but this is a bug/missing feature.
00049        If alternateDelimiter is a non-empty string then a line in the format:
00050          key{alternateDelimiter}value...
00051        (minus the braces) is also accepted as parseable. A key=value form always has precedence,
00052        however, so keys with spaces in them will work:
00053          some key=some value
00054        Note: the alternateDelimiter feature is Largely Untested.
00055        This function returns false if it does not consider the line to be parseable.
00056        Use getKey() and getValue() to get the parsed values. Use getLine() to get the whole
00057        string passed to parse (as if you'd ever need it, though subclasses might).
00058        Note that getLine() is set regardless of whether this function returns true or false,
00059        but the key and value are only set if this function returns true.
00060        This function will return false if a line contains no key (like '=value').
00061     */
00062     bool parse( const string &line, const string & alternateDelimiter = "" );
00063     const string &getKey() const;
00064     const string &getValue() const;
00065     const string &getLine() const;
00066 
00067  private:
00068         string m_key;
00069         string m_val;
00070         string m_line;
00071         typedef std::map<char,bool> CharBoolMap;
00072         typedef CharBoolMap::iterator CharBoolMapIterator;
00073         typedef CharBoolMap::value_type CharBoolPair;
00074 
00075         static CharBoolMap commentChars;
00076         CharBoolMapIterator commentIt;
00077         string::const_iterator strIt;
00078 };
00079 #endif // SGB_KEYVALUEPARSER_H

Generated on Mon Aug 11 14:06:55 2003 for libfunutil by doxygen1.2.18