Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/KernOptionsParser.cpp

1/*
2 * KernOptionsParser.cpp
3 * ChameleonPrefPane
4 *
5 * Created by Rekursor on 1/23/10.
6 */
7
8#include "KernOptionsParser.h"
9#include "string_util.h"
10
11bool KernOptionsParser::parseOptions(const char* options)
12{
13_options = options ? trim(options) : "";
14std::string token, temp=_options;
15
16// first remove spaces around '=' to simplify parsing
17std::string::size_type found = std::string::npos;
18while ((found=_options.find(" =")) != std::string::npos) _options.replace(found, 3,"=");
19while ((found=_options.find("= ")) != std::string::npos) _options.replace(found, 3,"=");
20
21// then tokenize the string
22_optionsList = tokenize(_options);
23
24return true;
25}
26
27static const std::string sEmpty="";
28
29// get the corresponding kern option "xxxx=yyyy"
30const std::string& KernOptionsParser::stringFromKey(const std::string& key) const
31{
32for (std::list<std::string>::const_iterator it=_optionsList.begin(); it!=_optionsList.end(); it++)
33if (it->find(key)!=std::string::npos) return *it;
34
35return sEmpty;
36}
37
38// get the left member of kern option "xxxx=yyyy"
39const std::string& KernOptionsParser::leftMember(const std::string& expression) const
40{
41static std::string sLeft;
42std::string::size_type pos = expression.find('=');
43
44if (pos!=std::string::npos)
45return (sLeft = expression.substr(0, pos));
46return expression;
47}
48
49// get the right member of kern option "xxxx=yyyy"
50const std::string& KernOptionsParser::rightMember(const std::string& expression) const
51{
52static std::string sLeft;
53std::string::size_type pos = expression.find('=');
54
55if (pos!=std::string::npos)
56return (sLeft = expression.substr(pos+1));
57return expression;
58}
59// remove a flag in the string
60void KernOptionsParser::removeFlag(const std::string& flag)
61{
62if (flag.length()==0) return; // not a flag, bye
63std::string::size_type pos, l = _options.length();
64if (l==0) return; // empty options nothing to do
65std::string f = string_left(flag, "=");
66std::string::size_type found = _options.find(f);
67if (found==std::string::npos) return;
68// find the end of the flag
69for ( pos=found+f.length(); pos<l && _options[pos]!=' '; pos++);
70_options.erase(found, pos);
71_options = trim(_options);
72}
73
74// remove a flag in the string
75void KernOptionsParser::addFlag(const std::string& flag)
76{
77// finding a flag can be tricky as some have a variable part of the form:
78// flag=value and what we want is to keep identify them while they will varyĆ 
79std::string key= string_left(flag,"=");
80
81std::string::size_type found = _options.find(key);
82if (found!=std::string::npos)
83removeFlag(key);
84_options = " " + _options;
85_options = flag + _options;
86_options = trim(_options);
87}
88

Archive Download this file

Revision: 432