1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ConfigReaderUtil.h"
18 
19 #include <android-base/logging.h>
20 #include <tinyxml2.h>
21 #include <utility>
22 
23 #include "core_lib.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace sv {
29 namespace V1_0 {
30 namespace implementation {
31 
32 using tinyxml2::XML_SUCCESS;
33 using tinyxml2::XMLElement;
34 
ElementHasText(const XMLElement * element)35 bool ElementHasText(const XMLElement* element) {
36     if (element->GetText() == "") {
37         LOG(ERROR) << "Expected element to have text: " << element->Name();
38         return false;
39     }
40     return true;
41 }
42 
GetElement(const XMLElement * parent,const char * elementName,XMLElement const ** element)43 bool GetElement(const XMLElement* parent, const char* elementName, XMLElement const** element) {
44     *element = parent->FirstChildElement(elementName);
45     if (*element == nullptr) {
46         LOG(ERROR) << "Expected element '" << elementName << "' in parent '" << parent->Name()
47                    << "' not found";
48         return false;
49     }
50     return true;
51 }
52 
ReadValue(const XMLElement * parent,const char * elementName,bool * value)53 bool ReadValue(const XMLElement* parent, const char* elementName, bool* value) {
54     const XMLElement* element = nullptr;
55     RETURN_IF_FALSE(GetElement(parent, elementName, &element));
56     if (element->QueryBoolText(value) != XML_SUCCESS) {
57         LOG(ERROR) << "Failed to read valid boolean value from: " << element->Name();
58         return false;
59     }
60     return true;
61 }
62 
ReadValue(const XMLElement * parent,const char * elementName,std::string * value)63 bool ReadValue(const XMLElement* parent, const char* elementName, std::string* value) {
64     const XMLElement* element = nullptr;
65     RETURN_IF_FALSE(GetElement(parent, elementName, &element));
66     RETURN_IF_FALSE(ElementHasText(element));
67     *value = std::string(element->GetText());
68     return true;
69 }
70 
ReadValue(const XMLElement * parent,const char * elementName,float * value)71 bool ReadValue(const XMLElement* parent, const char* elementName, float* value) {
72     const XMLElement* element = nullptr;
73     RETURN_IF_FALSE(GetElement(parent, elementName, &element));
74     if (element->QueryFloatText(value) != XML_SUCCESS) {
75         LOG(ERROR) << "Failed to read valid float value from: " << element->Name();
76         return false;
77     }
78     return true;
79 }
80 
ReadValue(const XMLElement * parent,const char * elementName,int * value)81 bool ReadValue(const XMLElement* parent, const char* elementName, int* value) {
82     const XMLElement* element = nullptr;
83     RETURN_IF_FALSE(GetElement(parent, elementName, &element));
84     if (element->QueryIntText(value) != XML_SUCCESS) {
85         LOG(ERROR) << "Failed to read valid int value from: " << element->Name();
86         return false;
87     }
88     return true;
89 }
90 
91 }  // namespace implementation
92 }  // namespace V1_0
93 }  // namespace sv
94 }  // namespace automotive
95 }  // namespace hardware
96 }  // namespace android
97