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, 3,"=");␊ |
19 | ␉while ((found=_options.find("= ")) != std::string::npos) _options.replace(found, 3,"=");␊ |
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 | // get the left member of kern option "xxxx=yyyy"␊ |
39 | const std::string& KernOptionsParser::leftMember(const std::string& expression) const␊ |
40 | {␊ |
41 | ␉static std::string sLeft;␊ |
42 | ␉std::string::size_type pos = expression.find('=');␊ |
43 | ␉␊ |
44 | ␉if (pos!=std::string::npos)␊ |
45 | ␉␉return (sLeft = expression.substr(0, pos));␊ |
46 | ␉return expression;␊ |
47 | }␊ |
48 | ␊ |
49 | // get the right member of kern option "xxxx=yyyy"␊ |
50 | const std::string& KernOptionsParser::rightMember(const std::string& expression) const␊ |
51 | {␊ |
52 | ␉static std::string sLeft;␊ |
53 | ␉std::string::size_type pos = expression.find('=');␊ |
54 | ␉␊ |
55 | ␉if (pos!=std::string::npos)␊ |
56 | ␉␉return (sLeft = expression.substr(pos+1));␊ |
57 | ␉return expression;␊ |
58 | }␊ |
59 | // remove a flag in the string␊ |
60 | void KernOptionsParser::removeFlag(const std::string& flag)␊ |
61 | {␊ |
62 | ␉if (flag.length()==0) return; // not a flag, bye␊ |
63 | ␉std::string::size_type pos, l = _options.length();␉␊ |
64 | ␉if (l==0) return; // empty options nothing to do␉␊ |
65 | ␉std::string f = string_left(flag, "=");␊ |
66 | ␉std::string::size_type found = _options.find(f);␊ |
67 | ␉if (found==std::string::npos) return;␊ |
68 | ␉// find the end of the flag␊ |
69 | ␉for ( pos=found+flag.length(); pos<l && _options[pos]!=' '; pos++);␊ |
70 | ␉_options.erase(found, pos);␊ |
71 | ␉_options = trim(_options);␊ |
72 | }␊ |
73 | ␊ |
74 | // remove a flag in the string␊ |
75 | void 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Ć ␊ |
79 | ␉std::string key= string_left(flag,"=");␊ |
80 | ␉␊ |
81 | ␉std::string::size_type found = _options.find(key);␊ |
82 | ␉if (found!=std::string::npos) ␊ |
83 | ␉␉removeFlag(key);␊ |
84 | ␉_options = " " + _options;␊ |
85 | ␉_options = flag + _options;␊ |
86 | ␉_options = trim(_options);␊ |
87 | }␊ |
88 | |