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

s11n::key_value_parser Class Reference

key_value_parser is a class for parsing "key=value"-style lines, like those which would come from a configuration file. More...

#include <key_value_parser.h>

List of all members.

Public Member Functions

 key_value_parser (const string &line)
 Creates a new key_value_parser and runs parse( line ).

 key_value_parser ()
 Creates an empty key_value_parser.

bool parse (const string &line, const string &delimiter="=")
 Parses 'line' into a key/value pair.

const string & key () const
 Returns the parsed-out key.

void key (const string &v)
 Sets this object's key().

const string & value () const
 Returns the parsed-out value (may be empty).

void value (const string &v)
 Sets this object's value().

const string & line () const
 Returns the last whole line passed to parse().


Detailed Description

key_value_parser is a class for parsing "key=value"-style lines, like those which would come from a configuration file.

Definition at line 18 of file key_value_parser.h.


Member Function Documentation

const string& s11n::key_value_parser::key  )  const [inline]
 

Returns the parsed-out key.

Only valid if parse() returned true.

Definition at line 75 of file key_value_parser.h.

bool s11n::key_value_parser::parse const string &  line,
const string &  delimiter = "="
 

Parses 'line' into a key/value pair.

To be parseable the line must be in the form:

key=value

Extra whitespace around the '=' removed, as are leading and trailing whitespace around the key and value. This behaviour is arguable but probably desireable in most cases (it is in all of mine, and i wrote the damned thing ;).

todo: add a whitespace removal policy as optional 3rd argument?

delimiter is the string which separates the key and value, so a line in the format:

key{alternateDelimiter}value...

(minus the braces) is parseable. alternateDelimiter can be a set of possible delimiters, such as " \t".

That is: parse( "one;two",";" ) results in key() == "one" and value() == "two"

This function returns false if it does not consider the line to be parseable. Use key() and value() to get the parsed values. Use line() to get the whole string passed to parse (as if you'd ever need it, though subclasses might). line() /is/ guaranteed to be set to line by this call, unlike key() and value().

If this function returns false, the values returned by key() and value() cannot be considered reliable (i.e., they are undefined).

This function will return false if a line contains no key (like '=value'), but empty values are not an error (i.e., they will not cause this function to return false). BUG: in some cases (when delimiter is a space) a no-value key can fail to parse.

const string& s11n::key_value_parser::value  )  const [inline]
 

Returns the parsed-out value (may be empty).

Only valid if parse() returned true.

Definition at line 90 of file key_value_parser.h.


The documentation for this class was generated from the following file:
Generated on Tue Jul 20 10:46:49 2004 for s11n by doxygen 1.3.7