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 #include <fstream>
16 #include <string>
17 #include <climits>
18 
19 #include "xml_helper.h"
20 
21 namespace OHOS {
22 namespace Memory {
23 namespace {
24     const std::string TAG = "XmlHelper";
25 } // namespace
26 
CheckNode(const xmlNodePtr & nodePtr)27 bool XmlHelper::CheckNode(const xmlNodePtr &nodePtr)
28 {
29     if (nodePtr != nullptr && nodePtr->name != nullptr &&
30         (nodePtr->type == XML_ELEMENT_NODE || nodePtr->type == XML_TEXT_NODE)) {
31         return true;
32     }
33     return false;
34 }
35 
CheckPathExist(const char * path)36 bool XmlHelper::CheckPathExist(const char *path)
37 {
38     std::ifstream profileStream(path);
39     return profileStream.good();
40 }
41 
HasChild(const xmlNodePtr & rootNodePtr)42 bool XmlHelper::HasChild(const xmlNodePtr &rootNodePtr)
43 {
44     return xmlChildElementCount(rootNodePtr) > 0;
45 }
46 
SetIntParam(std::map<std::string,std::string> & param,std::string key,int & dst,int defaultValue)47 void XmlHelper::SetIntParam(std::map<std::string, std::string> &param,
48                             std::string key, int &dst, int defaultValue)
49 {
50     dst = defaultValue;
51     std::map<std::string, std::string>::iterator iter = param.find(key);
52     if (iter != param.end() && (iter->second).size() > 0) {
53         try {
54             dst = std::stoi(iter->second);
55             return;
56         } catch (std::out_of_range&) {
57             HILOGW("stoi() failed: out_of_range");
58             return;
59         } catch (std::invalid_argument&) {
60             HILOGW("stoi() failed: invalid_argument");
61             return;
62         }
63     }
64     HILOGW("find param failed key:<%{public}s>", key.c_str());
65 }
66 
SetUnsignedIntParam(std::map<std::string,std::string> & param,std::string key,unsigned int & dst,unsigned int defaultValue)67 void XmlHelper::SetUnsignedIntParam(std::map<std::string, std::string> &param,
68                                     std::string key, unsigned int &dst, unsigned int defaultValue)
69 {
70     dst = defaultValue;
71     std::map<std::string, std::string>::iterator iter = param.find(key);
72     if (iter != param.end() && (iter->second).size() > 0) {
73         try {
74             unsigned long src = std::stoul(iter->second);
75             if (src > INT_MAX) {
76                 src = defaultValue;
77             }
78             dst = static_cast<unsigned int>(src);
79             return;
80         } catch (std::out_of_range&) {
81             HILOGW("stoul() failed: out_of_range");
82             return;
83         } catch (std::invalid_argument&) {
84             HILOGW("stoul() failed: invalid_argument");
85             return;
86         }
87     }
88     HILOGW("find param failed key:<%{public}s>", key.c_str());
89 }
90 
GetModuleParam(const xmlNodePtr & rootNodePtr,std::map<std::string,std::string> & param)91 bool XmlHelper::GetModuleParam(const xmlNodePtr &rootNodePtr, std::map<std::string, std::string> &param)
92 {
93     for (xmlNodePtr currNode = rootNodePtr->xmlChildrenNode; currNode != nullptr; currNode = currNode->next) {
94         if (!CheckNode(currNode)) {
95             return false;
96         }
97         std::string key = std::string(reinterpret_cast<const char *>(currNode->name));
98         auto contentPtr = xmlNodeGetContent(currNode);
99         std::string value;
100         if (contentPtr != nullptr) {
101             value = std::string(reinterpret_cast<char *>(contentPtr));
102             xmlFree(contentPtr);
103         }
104         param.insert(std::pair<std::string, std::string>(key, value));
105         HILOGI("key:<%{public}s>, value:<%{public}s>", key.c_str(), value.c_str());
106     }
107     return true;
108 }
109 
ParseUnsignedLongLongContent(const xmlNodePtr & rootNodePtr,unsigned long long & value)110 bool XmlHelper::ParseUnsignedLongLongContent(const xmlNodePtr &rootNodePtr, unsigned long long &value)
111 {
112     try {
113         auto contentPtr = xmlNodeGetContent(rootNodePtr);
114         std::string valueStr;
115         if (contentPtr != nullptr) {
116             valueStr = std::string(reinterpret_cast<char *>(contentPtr));
117             xmlFree(contentPtr);
118             size_t validPos = valueStr.find_first_not_of(" ");
119             if (validPos != std::string::npos && valueStr[validPos] == '-') {
120                 value = 0;
121             } else {
122                 value = std::strtoull(valueStr.c_str(), nullptr, 10); // 10:Decimal
123             }
124             return true;
125         }
126     } catch (...) {
127         return false;
128     }
129     return false;
130 }
131 
SetStringParam(std::map<std::string,std::string> & param,std::string key,std::string & dst,std::string defaultValue)132 void XmlHelper::SetStringParam(std::map<std::string, std::string> &param,
133                                std::string key, std::string &dst, std::string defaultValue)
134 {
135     HILOGI("called");
136     dst = defaultValue;
137     std::map<std::string, std::string>::iterator iter = param.find(key);
138     if (iter != param.end() && (iter->second).size() > 0) {
139         dst = iter->second;
140         return;
141     }
142     HILOGW("find param failed key:<%{public}s>", key.c_str());
143 }
144 } // namespace Memory
145 } // namespace OHOS
146