• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "cmd_list.h"
17 #include "restool_errors.h"
18 
19 namespace OHOS {
20 namespace Global {
21 namespace Restool {
22 using namespace std;
Init(const string & filePath,function<uint32_t (int c,const string & argValue)> callback)23 uint32_t CmdList::Init(const string &filePath, function<uint32_t(int c, const string &argValue)> callback)
24 {
25     Json::Value root;
26     if (!ResourceUtil::OpenJsonFile(filePath, root)) {
27         return RESTOOL_ERROR;
28     }
29 
30     if (!callback) {
31         return RESTOOL_SUCCESS;
32     }
33 
34     if (GetString(root["configPath"], 'j', callback) != RESTOOL_SUCCESS ||
35         GetString(root["packageName"], 'p', callback) != RESTOOL_SUCCESS ||
36         GetString(root["output"], 'o', callback) != RESTOOL_SUCCESS ||
37         GetString(root["startId"], 'e', callback) != RESTOOL_SUCCESS ||
38         GetString(root["entryCompiledResource"], 'd', callback) != RESTOOL_SUCCESS) {
39         return RESTOOL_ERROR;
40     }
41 
42     if (GetModuleNames(root["moduleNames"], callback) != RESTOOL_SUCCESS) {
43         return RESTOOL_ERROR;
44     }
45 
46     if (GetArray(root["ResourceTable"], 'r', callback) != RESTOOL_SUCCESS ||
47         GetString(root["applicationResource"], 'i', callback) != RESTOOL_SUCCESS ||
48         GetArray(root["moduleResources"], 'i', callback) != RESTOOL_SUCCESS ||
49         GetArray(root["dependencies"], 'i', callback) != RESTOOL_SUCCESS) {
50         return RESTOOL_ERROR;
51     }
52 
53     callback('f', "");
54     return RESTOOL_SUCCESS;
55 }
56 
57 // below private
GetString(const Json::Value & node,int c,HandleBack callback)58 uint32_t CmdList::GetString(const Json::Value &node, int c, HandleBack callback)
59 {
60     if (!node.isString()) {
61         return RESTOOL_SUCCESS;
62     }
63 
64     if (callback(c, node.asString()) != RESTOOL_SUCCESS) {
65         return RESTOOL_ERROR;
66     }
67     return RESTOOL_SUCCESS;
68 }
69 
GetArray(const Json::Value & node,int c,HandleBack callback)70 uint32_t CmdList::GetArray(const Json::Value &node, int c, HandleBack callback)
71 {
72     if (!node.isArray()) {
73         return RESTOOL_SUCCESS;
74     }
75 
76     for (Json::ArrayIndex i = 0; i < node.size(); i++) {
77         if (!node[i].isString()) {
78             return RESTOOL_ERROR;
79         }
80         if (callback(c, node[i].asString()) != RESTOOL_SUCCESS) {
81             return RESTOOL_ERROR;
82         }
83     }
84     return RESTOOL_SUCCESS;
85 }
86 
GetModuleNames(const Json::Value & node,HandleBack callback)87 uint32_t CmdList::GetModuleNames(const Json::Value &node, HandleBack callback)
88 {
89     string moduleNames;
90     if (node.isString()) {
91         return GetString(node, 'm', callback);
92     }
93     if (GetArray(node, 'm', [&moduleNames](int c, const string &argValue) {
94         if (!moduleNames.empty()) {
95             moduleNames.append(",");
96         }
97         moduleNames.append(argValue);
98         return RESTOOL_SUCCESS;
99     }) != RESTOOL_SUCCESS) {
100         return RESTOOL_ERROR;
101     }
102 
103     if (!moduleNames.empty() && callback('m', moduleNames) != RESTOOL_SUCCESS) {
104         return RESTOOL_ERROR;
105     }
106     return RESTOOL_SUCCESS;
107 }
108 }
109 }
110 }
111