Add gitlab issue/merge request templates
[staging/basesystem.git] / service / native / framework_unified / client / NS_ConfigParser / src / ns_cfg_reader.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NS_ConfigParser
19 /// \brief    This file contains implementation of CCFGReader class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <ns_cfg_reader.h>
27 #include <ns_cfg_parser.h>
28 #include <ns_config_parser_frameworkunifiedlog.h>
29 #include <string>
30
31
32
33 CCFGReader::CCFGReader(): m_cFilePath(""), m_pCFGParser(NULL) {  // LCOV_EXCL_BR_LINE 11:except branch
34 }
35
36 // LCOV_EXCL_START 200:CCFGReader is not external API class, this constructed function isn't used by other function
37 CCFGReader::CCFGReader(const std::string &f_c_filepath): m_cFilePath(f_c_filepath), m_pCFGParser(NULL) {
38   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
39   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", m_cFilePath.c_str());
40
41   m_pCFGParser = new CCFGParser(m_cFilePath);
42
43   if (NULL == m_pCFGParser) {
44     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser");
45   }
46 }
47 // LCOV_EXCL_STOP
48
49 EFrameworkunifiedStatus CCFGReader::ParseFile(const std::string &f_c_filepath) {
50   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
51   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
52
53   // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
54
55   m_cFilePath.assign(f_c_filepath);
56
57   m_pCFGParser = new(std::nothrow) CCFGParser();  // LCOV_EXCL_BR_LINE 11:except branch
58
59   if (NULL == m_pCFGParser) {  // LCOV_EXCL_BR_LINE 5:new's error case
60     // LCOV_EXCL_START 5:new's error case
61     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
62     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser");
63     l_e_status = eFrameworkunifiedStatusNullPointer;
64     // LCOV_EXCL_STOP
65   } else {
66     l_e_status = m_pCFGParser->CFGParseFile(f_c_filepath);
67   }
68
69   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
70   return l_e_status;
71 }
72
73 CCFGReader::~CCFGReader() {
74   if (NULL != m_pCFGParser) {
75     delete m_pCFGParser;
76     m_pCFGParser = NULL;
77   }
78 }
79
80 PVOID CCFGReader::GetDataPtr() {
81   return m_pCFGParser;
82 }
83
84 std::string CCFGReader::GetValue(const std::string &f_c_key) {
85   std::string l_cValue = "";
86
87   if (NULL != m_pCFGParser && !f_c_key.empty()) {  // LCOV_EXCL_BR_LINE 11:except branch
88     // read the data from internal CFG structure
89     l_cValue = m_pCFGParser->CFGGetData(f_c_key);  // LCOV_EXCL_BR_LINE 11:except branch
90   }
91
92   return l_cValue;
93 }
94
95 EFrameworkunifiedStatus CCFGReader::GetValue(const std::string &f_c_key, std::string &f_c_value) {
96   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
97   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
98
99   if (NULL == m_pCFGParser) {  // LCOV_EXCL_BR_LINE 5:m_pCFGParser cannot be NULL, because internal logic guarantee
100     // LCOV_EXCL_START 5:m_pCFGParser cannot be NULL, because internal logic guarantee
101     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
102     l_e_status = eFrameworkunifiedStatusNullPointer;
103     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Parser object(m_pCFGParser) is NULL");
104     // LCOV_EXCL_STOP
105   } else if (f_c_key.empty()) {
106     l_e_status = eFrameworkunifiedStatusInvldParam;
107     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
108     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid Key");
109     // LCOV_EXCL_BR_STOP
110   } else {
111     // read the data from internal CFG structure
112     l_e_status = m_pCFGParser->CFGGetData(f_c_key, f_c_value);
113   }
114
115   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
116   return l_e_status;
117 }