2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NS_ConfigParser
19 /// \brief This file contains implementation of CNSConfigReader class.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
31 #include <native_service/ns_config_parser_if.h>
32 #include <native_service/ns_reader.h>
37 #include "ns_config_parser_frameworkunifiedlog.h"
38 #include "ns_cfg_reader.h"
39 CNSConfigReader::CNSConfigReader() {
43 CNSConfigReader::CNSConfigReader(const std::string &f_c_filepath): m_pReader(NULL) {
44 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
46 if (eFrameworkunifiedStatusOK != Parse(f_c_filepath)) {
47 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Parsing file %s", f_c_filepath.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
51 CNSConfigReader::~CNSConfigReader() {
52 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
54 if (NULL != m_pReader) {
55 delete m_pReader; // LCOV_EXCL_BR_LINE 11:except branch
60 EFrameworkunifiedStatus CNSConfigReader::Parse(const std::string &f_c_filepath) {
61 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
62 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
64 // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
66 if (NULL != m_pReader) {
67 delete m_pReader; // LCOV_EXCL_BR_LINE 11:except branch
71 // create parser object depending on file extension
72 if (NULL != std::strstr(f_c_filepath.c_str(), ".cfg")) {
73 m_pReader = new(std::nothrow) CCFGReader(); // LCOV_EXCL_BR_LINE 11:except branch
75 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
76 l_e_status = eFrameworkunifiedStatusFail;
79 if (NULL != m_pReader) {
80 l_e_status = m_pReader->ParseFile(f_c_filepath);
82 l_e_status = eFrameworkunifiedStatusNullPointer;
85 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
89 PVOID CNSConfigReader::GetDataPtr() {
90 if (NULL != m_pReader) {
91 // get the data pointer
92 return m_pReader->GetDataPtr();
98 BOOL CNSConfigReader::GetBool(const std::string &f_c_key) {
99 BOOL l_bResult = FALSE;
101 if (NULL != m_pReader) {
102 std::string l_c_temp = m_pReader->GetValue(f_c_key);
104 l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
106 if (!strcasecmp(l_c_temp.c_str(), "true")) {
110 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
116 F_64 CNSConfigReader::GetDouble(const std::string &f_c_key) {
119 if (NULL != m_pReader) {
120 std::string l_c_temp = m_pReader->GetValue(f_c_key);
124 // convert string to double
125 l_fValue = strtod(l_c_temp.c_str(), NULL);
128 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to double, errno=%d", l_c_temp.c_str(), errno);
131 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
137 F_32 CNSConfigReader::GetFloat(const std::string &f_c_key) {
140 if (NULL != m_pReader) {
141 std::string l_c_temp = m_pReader->GetValue(f_c_key);
143 l_fValue = strtof(l_c_temp.c_str(), NULL);
147 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to float, errno=%d", l_c_temp.c_str(), errno);
150 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
156 SI_32 CNSConfigReader::GetInt(const std::string &f_c_key) {
157 UI_64 l_ui_value = 0;
159 if (NULL != m_pReader) {
160 std::string l_c_temp = m_pReader->GetValue(f_c_key);
164 if ((l_c_temp.size() >= 3) && ('0' == l_c_temp[0]) && ('X' == toupper(l_c_temp[1]))) {
165 l_ui_value = strtoul(l_c_temp.c_str(), NULL, 16);
167 l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10);
172 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to int, errno=%d", l_c_temp.c_str(), errno);
175 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
178 return static_cast<SI_32>(l_ui_value);
181 std::string CNSConfigReader::GetString(const std::string &f_c_key) {
182 std::string l_c_temp = "";
184 if (NULL != m_pReader) {
185 l_c_temp = m_pReader->GetValue(f_c_key); // LCOV_EXCL_BR_LINE 11:except branch
187 l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
189 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
195 EFrameworkunifiedStatus CNSConfigReader::GetBool(const std::string &f_c_key, BOOL &f_b_value) {
196 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
198 std::string l_c_temp = "";
200 if (NULL != m_pReader) {
201 l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
203 if (eFrameworkunifiedStatusOK == l_e_status) {
204 l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
206 if (!strcasecmp(l_c_temp.c_str(), "true")) {
208 } else if (!strcasecmp(l_c_temp.c_str(), "false")) {
211 l_e_status = eFrameworkunifiedStatusErrOther;
215 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
216 l_e_status = eFrameworkunifiedStatusNullPointer;
222 EFrameworkunifiedStatus CNSConfigReader::GetDouble(const std::string &f_c_key, F_64 &f_d_value) {
223 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
225 std::string l_c_temp = "";
227 if (NULL != m_pReader) {
228 l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
230 if (eFrameworkunifiedStatusOK == l_e_status) {
232 f_d_value = strtod(l_c_temp.c_str(), NULL);
235 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to double, errno=%d", l_c_temp.c_str(), errno);
236 l_e_status = eFrameworkunifiedStatusInvldBuf;
240 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
241 l_e_status = eFrameworkunifiedStatusNullPointer;
247 EFrameworkunifiedStatus CNSConfigReader::GetFloat(const std::string &f_c_key, F_32 &f_f_value) {
248 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
250 std::string l_c_temp = "";
252 if (NULL != m_pReader) {
253 l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
255 if (eFrameworkunifiedStatusOK == l_e_status) {
257 f_f_value = strtof(l_c_temp.c_str(), NULL);
261 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to float, errno=%d", l_c_temp.c_str(), errno);
262 l_e_status = eFrameworkunifiedStatusInvldBuf;
266 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
267 l_e_status = eFrameworkunifiedStatusNullPointer;
273 EFrameworkunifiedStatus CNSConfigReader::GetInt(const std::string &f_c_key, SI_32 &f_i_value) {
274 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
276 std::string l_c_temp = "";
278 UI_64 l_ui_value = 0;
280 if (NULL != m_pReader) {
281 l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
283 if (eFrameworkunifiedStatusOK == l_e_status) {
286 if ((l_c_temp.size() >= 3) && ('0' == l_c_temp[0]) && ('X' == toupper(l_c_temp[1]))) {
287 l_ui_value = strtoul(l_c_temp.c_str(), NULL, 16);
289 l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10);
292 f_i_value = static_cast<SI_32>(l_ui_value);
295 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to int, errno=%d", l_c_temp.c_str(), errno);
296 l_e_status = eFrameworkunifiedStatusInvldBuf;
300 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
301 l_e_status = eFrameworkunifiedStatusNullPointer;
307 EFrameworkunifiedStatus CNSConfigReader::GetString(const std::string &f_c_key, std::string &f_c_value) {
308 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
310 std::string l_c_temp = "";
312 if (NULL != m_pReader) {
313 l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
315 if (eFrameworkunifiedStatusOK == l_e_status) {
316 l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
318 f_c_value.assign(l_c_temp);
321 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
322 l_e_status = eFrameworkunifiedStatusNullPointer;
328 std::string CNSConfigReader::FormatValue(const std::string &f_c_value) {
329 std::string l_c_newvalue = f_c_value;
331 if (0 < f_c_value.length()) {
332 UI_32 l_uiStrPos = static_cast<UI_32>(l_c_newvalue.length() - 1);
334 while ('\n' == l_c_newvalue[l_uiStrPos] || '\r' == l_c_newvalue[l_uiStrPos]) {
335 l_c_newvalue = l_c_newvalue.substr(0, l_uiStrPos);
337 if (0 == l_uiStrPos) {