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 CCFGWriter class.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <sys/types.h>
31 #include <ns_cfg_parser.h>
32 #include <ns_cfg_writer.h>
33 #include <ns_config_parser_frameworkunifiedlog.h>
39 CCFGWriter::CCFGWriter(): m_cFilePath(""), m_pCFGParser(NULL) { // LCOV_EXCL_BR_LINE 11:except branch
40 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
43 CCFGWriter::CCFGWriter(const std::string &f_c_filepath): m_cFilePath(f_c_filepath), m_pCFGParser(NULL) {
44 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", m_cFilePath.c_str());
46 m_pCFGParser = new CCFGParser(m_cFilePath);
48 if (NULL == m_pCFGParser) {
49 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser");
53 CCFGWriter::~CCFGWriter() {
54 if (NULL != m_pCFGParser) {
55 delete m_pCFGParser; // LCOV_EXCL_BR_LINE 11:except branch
60 EFrameworkunifiedStatus CCFGWriter::ParseFile(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 m_cFilePath.assign(f_c_filepath);
68 m_pCFGParser = new(std::nothrow) CCFGParser(); // LCOV_EXCL_BR_LINE 11:except branch
70 if (NULL == m_pCFGParser) { // LCOV_EXCL_BR_LINE 5:m_pCFGParser cannot be NULL, because internal logic guarantee
71 // LCOV_EXCL_START 5:m_pCFGParser cannot be NULL, because internal logic guarantee
72 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
73 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser");
74 l_e_status = eFrameworkunifiedStatusNullPointer;
77 l_e_status = m_pCFGParser->CFGParseFile(f_c_filepath);
80 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
84 EFrameworkunifiedStatus CCFGWriter::SetPath(const std::string &f_c_path) {
85 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
87 if (!f_c_path.empty()) { // LCOV_EXCL_BR_LINE 11:except branch
89 m_cFilePath.assign(f_c_path);
91 l_e_status = eFrameworkunifiedStatusInvldParam;
97 VOID CCFGWriter::SetDataPtr(PVOID f_p_data) {
98 if (NULL != f_p_data) {
99 // set the doc pointer
100 m_pCFGParser = static_cast<CCFGParser *>(f_p_data);
103 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Data Pointer not set in cfg writer"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
107 EFrameworkunifiedStatus CCFGWriter::SetValue(const std::string &f_c_key, std::string f_c_value) {
108 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
110 if (NULL != m_pCFGParser && !f_c_key.empty()) {
111 // set the data in internal CFG structure
112 l_e_status = m_pCFGParser->CFGSetData(f_c_key, f_c_value); // LCOV_EXCL_BR_LINE 11:except branch
114 l_e_status = eFrameworkunifiedStatusInvldParam;
120 EFrameworkunifiedStatus CCFGWriter::SaveData() {
121 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
123 if (NULL != m_pCFGParser) {
124 if (0 == access(m_cFilePath.c_str(), W_OK)) {
125 // save the data permanently in config file
126 l_e_status = m_pCFGParser->CFGSaveData(m_cFilePath);
128 l_e_status = eFrameworkunifiedStatusFail;
131 l_e_status = eFrameworkunifiedStatusNullPointer;