~ubuntu-branches/ubuntu/quantal/miwm/quantal

« back to all changes in this revision

Viewing changes to EKeyValueParser.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jari Aalto
  • Date: 2010-01-04 15:25:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100104152534-l3fdvt162le460cv
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
   License: Public Domain
 
3
   Author: stephan@wanderinghorse.net
 
4
*/
 
5
//#include <ctype.h> // for isspace()
 
6
#include "EKeyValueParser.h"
 
7
#include <iostream>
 
8
using namespace std;
 
9
EKeyValueParser::charbool_map_type EKeyValueParser::commentChars;
 
10
 
 
11
std::ostream &
 
12
operator<<(ostream &os, const EKeyValueParser &obj)
 
13
{
 
14
        os << obj.key();
 
15
        os << std::string("="); // gcc3 bitches if this is a char *???
 
16
        os <<obj.value();
 
17
        return os;
 
18
}
 
19
 
 
20
//         bool
 
21
//         operator>>( const EKeyValueParser &obj, std::istream &is )
 
22
//         {
 
23
// //                 E
 
24
// //                 is.getline
 
25
// //                 return obj.parse(;
 
26
//                 return false;
 
27
//         }
 
28
 
 
29
EKeyValueParser::EKeyValueParser()
 
30
{
 
31
        EKeyValueParser::init();
 
32
}
 
33
 
 
34
EKeyValueParser::EKeyValueParser( const string &ln )
 
35
{
 
36
        EKeyValueParser::init();
 
37
        m_key = m_val = "";
 
38
        parse( ln );
 
39
}
 
40
 
 
41
bool
 
42
EKeyValueParser::parse( const string &ln, const string & delim )
 
43
{
 
44
        m_line = ln;
 
45
        if( m_line.length() < 2 ) return false;
 
46
 
 
47
        commentIt = commentChars.find( ln[0] );
 
48
        if( commentIt != commentChars.end() ) return false;
 
49
 
 
50
        m_key = "";
 
51
        m_val = "";
 
52
 
 
53
        string::size_type offset = m_line.find( delim );
 
54
 
 
55
        if( offset == string::npos ) return false;
 
56
 
 
57
 
 
58
        m_key = m_line.substr( 0, offset );
 
59
        m_val = m_line.substr( offset + delim.length() );
 
60
 
 
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 );
 
67
        }
 
68
        while( !m_key.empty() && (m_key.find_first_of( space ) == 0 ) )
 
69
        { // leading key whitespace
 
70
                m_key.erase( 0, 1 );
 
71
        }
 
72
 
 
73
        while( !m_val.empty() && (m_val.find_first_of( space ) == 0 ) )
 
74
        { // leading val whitespace
 
75
                m_val.erase( 0, 1 );
 
76
        }
 
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 );
 
80
        }
 
81
        // Whew. Blessed indeed is Perl.
 
82
        //CERR << "m_key=["<<m_key<<"] m_val=["<<m_val<<"]"<<endl;
 
83
        return m_key.size() > 0;
 
84
}
 
85
 
 
86
void
 
87
EKeyValueParser::init()
 
88
{
 
89
        static bool inited = false;
 
90
        if( !inited && (inited=true) )
 
91
        {
 
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();
 
96
        }
 
97
 
 
98
}
 
99