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 | ␉std::string::size_type found = _options.find(flag);␊ |
63 | ␉if (found==std::string::npos) return;␊ |
64 | ␉_options.erase(found, found+flag.length());␊ |
65 | ␉_options = trim(_options);␊ |
66 | }␊ |
67 | ␊ |
68 | // remove a flag in the string␊ |
69 | void KernOptionsParser::addFlag(const std::string& flag)␊ |
70 | {␊ |
71 | ␉std::string::size_type found = _options.find(flag);␊ |
72 | ␉if (found!=std::string::npos) return;␊ |
73 | ␉_options = " " + _options;␊ |
74 | ␉_options = flag + _options;␊ |
75 | ␉_options = trim(_options);␊ |
76 | }␊ |
77 | |