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 | ␊ |
11 | bool KernOptionsParser::parseOptions(const char* options)␊ |
12 | {␊ |
13 | ␉_options = options ? trim(options) : "";␊ |
14 | ␉std::string token, temp=_options;␊ |
15 | ␉␊ |
16 | ␉// first remove spaces around '=' to simplify parsing␊ |
17 | ␉std::string::size_type found = std::string::npos;␊ |
18 | ␉while ((found=_options.find(" =")) != std::string::npos) _options.replace(found, 2,"=");␊ |
19 | ␉while ((found=_options.find("= ")) != std::string::npos) _options.replace(found, 2,"=");␊ |
20 | ␉␊ |
21 | ␉// then tokenize the string␊ |
22 | ␉_optionsList = tokenize(_options);␊ |
23 | ␊ |
24 | ␉return true;␊ |
25 | }␊ |
26 | ␊ |
27 | static const std::string sEmpty="";␊ |
28 | ␊ |
29 | // get the corresponding kern option "xxxx=yyyy"␊ |
30 | const std::string& KernOptionsParser::stringFromKey(const std::string& key) const␊ |
31 | {␊ |
32 | ␉for (std::list<std::string>::const_iterator it=_optionsList.begin(); it!=_optionsList.end(); it++)␊ |
33 | ␉␉if (it->find(key)!=std::string::npos) return *it;␊ |
34 | ␉␊ |
35 | ␉return sEmpty;␊ |
36 | }␊ |
37 | ␊ |
38 | // remove a flag in the string␊ |
39 | void KernOptionsParser::removeFlag(const std::string& flag)␊ |
40 | {␊ |
41 | ␉if (flag.length()==0) return; // not a flag, bye␊ |
42 | ␉std::string::size_type pos, l = _options.length();␉␊ |
43 | ␉if (l==0) return; // empty options nothing to do␉␊ |
44 | ␉std::string f = leftMember(flag);␊ |
45 | ␉std::string::size_type found = _options.find(f);␊ |
46 | ␉if (found==std::string::npos) return;␊ |
47 | ␉// find the end of the flag␊ |
48 | ␉for ( pos=found+f.length(); pos<l && _options[pos]!=' '; pos++);␊ |
49 | ␉if (pos==l) _options.erase(found); // eol and no space found , remove till the end␊ |
50 | ␉else _options.erase(found, pos-found+1);␊ |
51 | ␉_options = trim(_options);␊ |
52 | }␊ |
53 | ␊ |
54 | // remove a flag in the string␊ |
55 | void KernOptionsParser::addFlag(const std::string& flag)␊ |
56 | {␊ |
57 | ␉// finding a flag can be tricky as some have a variable part of the form:␊ |
58 | ␉// flag=value and what we want is to keep identify them while they will varyĆ ␊ |
59 | ␉std::string key= string_left(flag,"=");␊ |
60 | ␉␊ |
61 | ␉std::string::size_type found = _options.find(key);␊ |
62 | ␉if (found!=std::string::npos) ␊ |
63 | ␉␉removeFlag(key);␊ |
64 | ␉_options = " " + _options;␊ |
65 | ␉_options = flag + _options;␊ |
66 | ␉_options = trim(_options);␊ |
67 | }␊ |
68 | |