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, 2,"=");
19while ((found=_options.find("= ")) != std::string::npos) _options.replace(found, 2,"=");
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// remove a flag in the string
39void KernOptionsParser::removeFlag(const std::string& flag)
40{
41if (flag.length()==0) return; // not a flag, bye
42std::string::size_type pos, l = _options.length();
43if (l==0) return; // empty options nothing to do
44std::string f = leftMember(flag);
45std::string::size_type found = _options.find(f);
46if (found==std::string::npos) return;
47// find the end of the flag
48for ( pos=found+f.length(); pos<l && _options[pos]!=' '; pos++);
49_options.erase(found, pos);
50_options = trim(_options);
51}
52
53// remove a flag in the string
54void KernOptionsParser::addFlag(const std::string& flag)
55{
56// finding a flag can be tricky as some have a variable part of the form:
57// flag=value and what we want is to keep identify them while they will varyĆ 
58std::string key= string_left(flag,"=");
59
60std::string::size_type found = _options.find(key);
61if (found!=std::string::npos)
62removeFlag(key);
63_options = " " + _options;
64_options = flag + _options;
65_options = trim(_options);
66}
67

Archive Download this file

Revision: 453