• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "key_parser.h"
17 #include<regex>
18 #include "resource_util.h"
19 
20 namespace OHOS {
21 namespace Global {
22 namespace Restool {
23 using namespace std;
24 map<string, vector<KeyParam>> KeyParser::caches_ = {};
Parse(const string & folderName,vector<KeyParam> & keyparams)25 bool KeyParser::Parse(const string &folderName, vector<KeyParam> &keyparams)
26 {
27     if (folderName == "base") {
28         return true;
29     }
30 
31     if (caches_.count(folderName) != 0) {
32         keyparams = caches_[folderName];
33         return true;
34     }
35     vector<string> keyParts;
36     ResourceUtil::Split(folderName, keyParts, "-");
37 
38     vector<parse_key_founction> founctions = {
39         ParseMccMnc,
40         ParseLSR,
41         ParseOrientation,
42         ParseDeviceType,
43         ParseNightMode,
44         ParseResolution,
45     };
46 
47     if (!ParseMatch(keyParts, keyparams, founctions)) {
48         return false;
49     }
50     caches_.emplace(folderName, keyparams);
51     return true;
52 }
53 
ParseMatch(const vector<string> & keys,vector<KeyParam> & keyparams,const vector<parse_key_founction> & founctions)54 bool KeyParser::ParseMatch(const vector<string> &keys,
55     vector<KeyParam> &keyparams, const vector<parse_key_founction> &founctions)
56 {
57     size_t next = 0;
58     for (const auto &key : keys) {
59         bool parseMatch = false;
60         for (size_t i = next; i < founctions.size(); i++) {
61             if (founctions[i](key, keyparams)) {
62                 parseMatch = true;
63                 next = i;
64                 break;
65             }
66         }
67 
68         if (!parseMatch) {
69             return false;
70         }
71     }
72     return true;
73 }
74 
ParseMatchBySeq(const vector<string> & keys,vector<KeyParam> & keyparams,const vector<parse_key_founction> & founctions)75 bool KeyParser::ParseMatchBySeq(const vector<string> &keys,
76     vector<KeyParam> &keyparams, const vector<parse_key_founction> &founctions)
77 {
78     for (size_t i = 0; i < keys.size(); i++) {
79         if (!founctions[i](keys[i], keyparams)) {
80             return false;
81         }
82     }
83     return true;
84 }
85 
ParseMccMnc(const string & folderName,vector<KeyParam> & keyparams)86 bool KeyParser::ParseMccMnc(const string &folderName, vector<KeyParam> &keyparams)
87 {
88     vector<parse_key_founction> founctions = {
89         ParseMcc,
90         ParseMnc,
91     };
92 
93     vector<string> keyParts;
94     ResourceUtil::Split(folderName, keyParts, "_");
95     if (keyParts.size() > founctions.size()) {
96         return false;
97     }
98     return ParseMatchBySeq(keyParts, keyparams, founctions);
99 }
100 
ParseMcc(const string & folderName,vector<KeyParam> & keyparams)101 bool KeyParser::ParseMcc(const string &folderName, vector<KeyParam> &keyparams)
102 {
103     regex mcc("mcc(\\d{3})");
104     if (!regex_match(folderName, mcc)) {
105         return false;
106     }
107 
108     PushMccMnc(folderName, KeyType::MCC, keyparams);
109     return true;
110 }
111 
ParseMnc(const string & folderName,vector<KeyParam> & keyparams)112 bool KeyParser::ParseMnc(const string &folderName, vector<KeyParam> &keyparams)
113 {
114     regex mcc("mnc(\\d{2,3})");
115     if (!regex_match(folderName, mcc)) {
116         return false;
117     }
118 
119     PushMccMnc(folderName, KeyType::MNC, keyparams);
120     return true;
121 }
122 
ParseLSR(const string & folderName,vector<KeyParam> & keyparams)123 bool KeyParser::ParseLSR(const string &folderName, vector<KeyParam> &keyparams)
124 {
125     map<string, vector<parse_key_founction>> founctionModels = {
126         { "all", { ParseLanguage, ParseScript, ParseRegion } },
127         { "language script", { ParseLanguage, ParseScript} },
128         { "language region", { ParseLanguage, ParseRegion } },
129     };
130 
131     vector<string> keyParts;
132     ResourceUtil::Split(folderName, keyParts, "_");
133     if (keyParts.size() > founctionModels["all"].size()) {
134         return false;
135     }
136 
137     for (auto model : founctionModels) {
138         vector<KeyParam> tmp;
139         if (ParseMatchBySeq(keyParts, tmp, model.second)) {
140             keyparams.insert(keyparams.end(), tmp.begin(), tmp.end());
141             return true;
142         }
143     }
144     return false;
145 }
146 
ParseLanguage(const string & folderName,vector<KeyParam> & keyparams)147 bool KeyParser::ParseLanguage(const string &folderName, vector<KeyParam> &keyparams)
148 {
149     if (g_deviceMap.find(folderName) != g_deviceMap.end()) {
150         return false;
151     }
152 
153     regex language("[a-z]{2,3}");
154     if (!regex_match(folderName, language)) {
155         return false;
156     }
157 
158     PushLSR(folderName, KeyType::LANGUAGE, keyparams);
159     return true;
160 }
161 
ParseScript(const string & folderName,vector<KeyParam> & keyparams)162 bool KeyParser::ParseScript(const string &folderName, vector<KeyParam> &keyparams)
163 {
164     if (folderName.length() != SCRIPT_LENGHT) {
165         return false;
166     }
167 
168     regex script("^[A-Z][a-z]{3}");
169     if (!regex_match(folderName, script)) {
170         return false;
171     }
172 
173     PushLSR(folderName, KeyType::SCRIPT, keyparams);
174     return true;
175 }
176 
ParseRegion(const string & folderName,vector<KeyParam> & keyparams)177 bool KeyParser::ParseRegion(const string &folderName, vector<KeyParam> &keyparams)
178 {
179     if (folderName.length() < MIN_REGION_LENGHT || folderName.length() > MAX_REGION_LENGHT) {
180         return false;
181     }
182 
183     regex regionOfNumber("[0-9]{3}");
184     regex regionOfSupper("[A-Z]{2,3}");
185 
186     if (!regex_match(folderName, regionOfNumber) && !regex_match(folderName, regionOfSupper)) {
187         return false;
188     }
189 
190     PushLSR(folderName, KeyType::REGION, keyparams);
191     return true;
192 }
193 
ParseOrientation(const string & folderName,vector<KeyParam> & keyparams)194 bool KeyParser::ParseOrientation(const string &folderName, vector<KeyParam> &keyparams)
195 {
196     auto it = g_orientaionMap.find(folderName);
197     if (it == g_orientaionMap.end()) {
198         return false;
199     }
200 
201     PushValue(static_cast<uint32_t>(it->second), KeyType::ORIENTATION, keyparams);
202     return true;
203 }
204 
ParseDeviceType(const string & folderName,vector<KeyParam> & keyparams)205 bool KeyParser::ParseDeviceType(const string &folderName, vector<KeyParam> &keyparams)
206 {
207     auto it = g_deviceMap.find(folderName);
208     if (it == g_deviceMap.end()) {
209         return false;
210     }
211 
212     PushValue(static_cast<uint32_t>(it->second), KeyType::DEVICETYPE, keyparams);
213     return true;
214 }
215 
ParseNightMode(const string & folderName,vector<KeyParam> & keyparams)216 bool KeyParser::ParseNightMode(const string &folderName, vector<KeyParam> &keyparams)
217 {
218     auto it = g_nightModeMap.find(folderName);
219     if (it == g_nightModeMap.end()) {
220         return false;
221     }
222 
223     PushValue(static_cast<uint32_t>(it->second), KeyType::NIGHTMODE, keyparams);
224     return true;
225 }
226 
ParseResolution(const string & folderName,vector<KeyParam> & keyparams)227 bool KeyParser::ParseResolution(const string &folderName, vector<KeyParam> &keyparams)
228 {
229     auto it = g_resolutionMap.find(folderName);
230     if (it == g_resolutionMap.end()) {
231         return false;
232     }
233 
234     PushValue(static_cast<uint32_t>(it->second), KeyType::RESOLUTION, keyparams);
235     return true;
236 }
237 
PushMccMnc(const string & folderName,KeyType type,vector<KeyParam> & keyparams)238 void KeyParser::PushMccMnc(const string &folderName, KeyType type, vector<KeyParam> &keyparams)
239 {
240     KeyParam keyParam;
241     keyParam.keyType = type;
242     keyParam.value = atoi(folderName.substr(MCC_MNC_KEY_LENGHT).c_str());
243     keyparams.push_back(keyParam);
244 }
245 
PushLSR(const string & folderName,KeyType type,vector<KeyParam> & keyparams)246 void KeyParser::PushLSR(const string &folderName, KeyType type, vector<KeyParam> &keyparams)
247 {
248     KeyParam keyParam;
249     keyParam.keyType = type;
250     keyParam.value = 0;
251     for (size_t i = 0; i < folderName.size(); i++) {
252         keyParam.value = (keyParam.value << 8) | (folderName[i] & 0xff);
253     }
254     keyparams.push_back(keyParam);
255 }
256 
PushValue(uint32_t value,KeyType type,vector<KeyParam> & keyparams)257 void KeyParser::PushValue(uint32_t value, KeyType type, vector<KeyParam> &keyparams)
258 {
259     KeyParam keyParam;
260     keyParam.keyType = type;
261     keyParam.value = value;
262     keyparams.push_back(keyParam);
263 }
264 }
265 }
266 }