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