3
Author: stephan@wanderinghorse.net
5
//#include <ctype.h> // for isspace()
6
#include "EKeyValueParser.h"
9
EKeyValueParser::charbool_map_type EKeyValueParser::commentChars;
12
operator<<(ostream &os, const EKeyValueParser &obj)
15
os << std::string("="); // gcc3 bitches if this is a char *???
21
// operator>>( const EKeyValueParser &obj, std::istream &is )
25
// // return obj.parse(;
29
EKeyValueParser::EKeyValueParser()
31
EKeyValueParser::init();
34
EKeyValueParser::EKeyValueParser( const string &ln )
36
EKeyValueParser::init();
42
EKeyValueParser::parse( const string &ln, const string & delim )
45
if( m_line.length() < 2 ) return false;
47
commentIt = commentChars.find( ln[0] );
48
if( commentIt != commentChars.end() ) return false;
53
string::size_type offset = m_line.find( delim );
55
if( offset == string::npos ) return false;
58
m_key = m_line.substr( 0, offset );
59
m_val = m_line.substr( offset + delim.length() );
61
// strip leading/trailing spaces from m_key and m_val.
62
// there must be a simpler (or at least more graceful) way...
63
static const std::string space(" \t");
64
while( !m_key.empty() && m_key.find_last_of( space ) == (m_key.size()-1) )
65
{ // trailing key whitespace
66
m_key.erase( m_key.size()-1 );
68
while( !m_key.empty() && (m_key.find_first_of( space ) == 0 ) )
69
{ // leading key whitespace
73
while( !m_val.empty() && (m_val.find_first_of( space ) == 0 ) )
74
{ // leading val whitespace
77
while( !m_val.empty() && m_val.find_last_of( space ) == (m_val.size()-1) )
78
{ // trailing val whitespace
79
m_val.erase( m_val.size()-1 );
81
// Whew. Blessed indeed is Perl.
82
//CERR << "m_key=["<<m_key<<"] m_val=["<<m_val<<"]"<<endl;
83
return m_key.size() > 0;
87
EKeyValueParser::init()
89
static bool inited = false;
90
if( !inited && (inited=true) )
92
commentChars[';'] = true; // classic config-file style
93
commentChars['#'] = true; // bash-style
94
commentChars['/'] = true; // assume '/' or '/*'. Honestly, though, it's this way only because it simplifies searching :/
95
EKeyValueParser::init();