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 CNSConfigWriter class.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_config_parser_if.h>
27 #include <native_service/ns_writer.h>
32 #include "ns_cfg_writer.h"
33 #include "ns_config_parser_frameworkunifiedlog.h"
36 CNSConfigWriter::CNSConfigWriter(): m_pWriter(NULL) {
39 CNSConfigWriter::CNSConfigWriter(const std::string &f_c_filepath): m_pWriter(NULL) {
40 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
45 CNSConfigWriter::CNSConfigWriter(const std::string &f_c_filepath, BOOL f_b_noload): m_pWriter(NULL) {
46 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
51 CNSConfigWriter::~CNSConfigWriter() {
52 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
54 if (NULL != m_pWriter) {
55 delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch
58 // TODO(my_username): dlclose.
61 EFrameworkunifiedStatus CNSConfigWriter::Parse(const std::string &f_c_filepath) {
62 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
64 if (NULL != m_pWriter) {
65 delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch
69 // create parser object depending on file extension
70 if (std::strstr(f_c_filepath.c_str(), ".cfg") != NULL) {
71 m_pWriter = new(std::nothrow) CCFGWriter(); // LCOV_EXCL_BR_LINE 11:except branch
73 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
74 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG");
78 if (NULL != m_pWriter) {
79 l_e_status = m_pWriter->ParseFile(f_c_filepath);
81 l_e_status = eFrameworkunifiedStatusNullPointer;
87 EFrameworkunifiedStatus CNSConfigWriter::Create(const std::string &f_c_filepath) {
88 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
90 if (NULL != m_pWriter) {
91 delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch
95 // create parser object depending on file extension
96 if (std::strstr(f_c_filepath.c_str(), ".cfg") != NULL) {
97 m_pWriter = new(std::nothrow) CCFGWriter(); // LCOV_EXCL_BR_LINE 11:except branch
99 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
100 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG");
102 l_e_status = eFrameworkunifiedStatusFail;
105 if (NULL != m_pWriter) {
106 l_e_status = m_pWriter->SetPath(f_c_filepath);
108 l_e_status = eFrameworkunifiedStatusNullPointer;
114 VOID CNSConfigWriter::SetDataPtr(PVOID f_p_data) {
115 if (NULL != m_pWriter) {
116 m_pWriter->SetDataPtr(f_p_data);
118 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
119 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pWriter is NULL");
124 EFrameworkunifiedStatus CNSConfigWriter::Save() {
125 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
127 if (NULL != m_pWriter) {
128 // save the updated config file
129 l_e_status = m_pWriter->SaveData();
131 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
132 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");
134 l_e_status = eFrameworkunifiedStatusNullPointer;
140 EFrameworkunifiedStatus CNSConfigWriter::SetBool(const std::string &f_c_key, BOOL f_b_value) {
141 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
143 if (NULL != m_pWriter) {
144 std::string l_c_temp = "";
146 if (TRUE == f_b_value) {
148 l_e_status = m_pWriter->SetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
149 } else if (FALSE == f_b_value) { // LCOV_EXCL_BR_LINE 11:except branch
151 l_e_status = m_pWriter->SetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch
153 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "received bool value is not proper:: %d", f_b_value);
154 l_e_status = eFrameworkunifiedStatusInvldParam;
157 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
158 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");
160 l_e_status = eFrameworkunifiedStatusNullPointer;
166 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_64 f_f_value) {
167 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
169 if (NULL != m_pWriter) {
170 std::stringstream l_cStream;
171 l_cStream << f_f_value;
173 l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch
175 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
176 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");
178 l_e_status = eFrameworkunifiedStatusNullPointer;
184 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_32 f_fValue) {
185 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
187 if (NULL != m_pWriter) { // LCOV_EXCL_BR_LINE 11:except branch
188 std::stringstream l_cStream;
189 l_cStream << f_fValue;
191 l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch
193 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");
194 l_e_status = eFrameworkunifiedStatusNullPointer;
200 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, SI_32 f_si_value) {
201 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
203 if (NULL != m_pWriter) {
204 std::stringstream l_cStream;
205 l_cStream << f_si_value; // LCOV_EXCL_BR_LINE 11:except branch
207 l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch
209 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); // LCOV_EXCL_BR_LINE 11:except branch
210 l_e_status = eFrameworkunifiedStatusNullPointer;
216 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, const std::string &f_s_value) {
217 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
219 if (NULL != m_pWriter) {
220 l_e_status = m_pWriter->SetValue(f_cKey, f_s_value); // LCOV_EXCL_BR_LINE 11:except branch
222 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); // LCOV_EXCL_BR_LINE 11:except branch
223 l_e_status = eFrameworkunifiedStatusNullPointer;