Add gitlab issue/merge request templates
[staging/basesystem.git] / service / native / framework_unified / client / NS_ConfigParser / src / ns_config_writer.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 CNSConfigWriter class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_config_parser_if.h>
27 #include <native_service/ns_writer.h>
28 #include <dlfcn.h>
29 #include <sstream>
30 #include <cstring>
31 #include <string>
32 #include "ns_cfg_writer.h"
33 #include "ns_config_parser_frameworkunifiedlog.h"
34
35
36 CNSConfigWriter::CNSConfigWriter(): m_pWriter(NULL) {
37 }
38
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());
41
42   Parse(f_c_filepath);
43 }
44
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());
47
48   Create(f_c_filepath);
49 }
50
51 CNSConfigWriter::~CNSConfigWriter() {
52   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
53
54   if (NULL != m_pWriter) {
55     delete m_pWriter;  // LCOV_EXCL_BR_LINE 11:except branch
56     m_pWriter = NULL;
57   }
58   // TODO(my_username): dlclose.
59 }
60
61 EFrameworkunifiedStatus CNSConfigWriter::Parse(const std::string &f_c_filepath) {
62   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
63
64   if (NULL != m_pWriter) {
65     delete m_pWriter;  // LCOV_EXCL_BR_LINE 11:except branch
66     m_pWriter = NULL;
67   }
68
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
72   } else {
73     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
74     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG");
75     // LCOV_EXCL_BR_STOP
76   }
77
78   if (NULL != m_pWriter) {
79     l_e_status = m_pWriter->ParseFile(f_c_filepath);
80   } else {
81     l_e_status = eFrameworkunifiedStatusNullPointer;
82   }
83
84   return l_e_status;
85 }
86
87 EFrameworkunifiedStatus CNSConfigWriter::Create(const std::string &f_c_filepath) {
88   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
89
90   if (NULL != m_pWriter) {
91     delete m_pWriter;  // LCOV_EXCL_BR_LINE 11:except branch
92     m_pWriter = NULL;
93   }
94
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
98   } else {
99     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
100     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG");
101     // LCOV_EXCL_BR_STOP
102     l_e_status = eFrameworkunifiedStatusFail;
103   }
104
105   if (NULL != m_pWriter) {
106     l_e_status = m_pWriter->SetPath(f_c_filepath);
107   } else {
108     l_e_status = eFrameworkunifiedStatusNullPointer;
109   }
110
111   return l_e_status;
112 }
113
114 VOID CNSConfigWriter::SetDataPtr(PVOID f_p_data) {
115   if (NULL != m_pWriter) {
116     m_pWriter->SetDataPtr(f_p_data);
117   } else {
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");
120     // LCOV_EXCL_BR_STOP
121   }
122 }
123
124 EFrameworkunifiedStatus CNSConfigWriter::Save() {
125   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
126
127   if (NULL != m_pWriter) {
128     // save the updated config file
129     l_e_status = m_pWriter->SaveData();
130   } else {
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");
133     // LCOV_EXCL_BR_STOP
134     l_e_status = eFrameworkunifiedStatusNullPointer;
135   }
136
137   return l_e_status;
138 }
139
140 EFrameworkunifiedStatus CNSConfigWriter::SetBool(const std::string &f_c_key, BOOL f_b_value) {
141   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
142
143   if (NULL != m_pWriter) {
144     std::string l_c_temp = "";
145
146     if (TRUE == f_b_value) {
147       l_c_temp = "TRUE";
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
150       l_c_temp = "FALSE";
151       l_e_status = m_pWriter->SetValue(f_c_key, l_c_temp);  // LCOV_EXCL_BR_LINE 11:except branch
152     } else {
153       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "received bool value is not proper:: %d", f_b_value);
154       l_e_status = eFrameworkunifiedStatusInvldParam;
155     }
156   } else {
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");
159     // LCOV_EXCL_BR_STOP
160     l_e_status = eFrameworkunifiedStatusNullPointer;
161   }
162
163   return l_e_status;
164 }
165
166 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_64 f_f_value) {
167   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
168
169   if (NULL != m_pWriter) {
170     std::stringstream l_cStream;
171     l_cStream << f_f_value;
172
173     l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str());  // LCOV_EXCL_BR_LINE 11:except branch
174   } else {
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");
177     // LCOV_EXCL_BR_STOP
178     l_e_status = eFrameworkunifiedStatusNullPointer;
179   }
180
181   return l_e_status;
182 }
183
184 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_32 f_fValue) {
185   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
186
187   if (NULL != m_pWriter) {  // LCOV_EXCL_BR_LINE 11:except branch
188     std::stringstream l_cStream;
189     l_cStream << f_fValue;
190
191     l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str());  // LCOV_EXCL_BR_LINE 11:except branch
192   } else {
193     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");
194     l_e_status = eFrameworkunifiedStatusNullPointer;
195   }
196
197   return l_e_status;
198 }
199
200 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, SI_32 f_si_value) {
201   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
202
203   if (NULL != m_pWriter) {
204     std::stringstream l_cStream;
205     l_cStream << f_si_value;  // LCOV_EXCL_BR_LINE 11:except branch
206
207     l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str());  // LCOV_EXCL_BR_LINE 11:except branch
208   } else {
209     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");  // LCOV_EXCL_BR_LINE 11:except branch
210     l_e_status = eFrameworkunifiedStatusNullPointer;
211   }
212
213   return l_e_status;
214 }
215
216 EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, const std::string &f_s_value) {
217   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
218
219   if (NULL != m_pWriter) {
220     l_e_status = m_pWriter->SetValue(f_cKey, f_s_value);  // LCOV_EXCL_BR_LINE 11:except branch
221   } else {
222     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL");  // LCOV_EXCL_BR_LINE 11:except branch
223     l_e_status = eFrameworkunifiedStatusNullPointer;
224   }
225
226   return l_e_status;
227 }