Re-organized sub-directory by category
[staging/basesystem.git] / service / native / framework_unified / client / NS_ConfigParser / src / ns_config_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 CNSConfigReader class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <dlfcn.h>
29 #include <errno.h>
30
31 #include <native_service/ns_config_parser_if.h>
32 #include <native_service/ns_reader.h>
33
34 #include <cstring>
35 #include <string>
36
37 #include "ns_config_parser_frameworkunifiedlog.h"
38 #include "ns_cfg_reader.h"
39 CNSConfigReader::CNSConfigReader() {
40   m_pReader = NULL;
41 }
42
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());
45
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"
48   }
49 }
50
51 CNSConfigReader::~CNSConfigReader() {
52   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
53
54   if (NULL != m_pReader) {
55     delete m_pReader;  // LCOV_EXCL_BR_LINE 11:except branch
56     m_pReader = NULL;
57   }
58 }
59
60 EFrameworkunifiedStatus CNSConfigReader::Parse(const std::string &f_c_filepath) {
61   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
62   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
63
64   // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str());
65
66   if (NULL != m_pReader) {
67     delete m_pReader;  // LCOV_EXCL_BR_LINE 11:except branch
68     m_pReader = NULL;
69   }
70
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
74   } else {
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;
77   }
78
79   if (NULL != m_pReader) {
80     l_e_status = m_pReader->ParseFile(f_c_filepath);
81   } else {
82     l_e_status = eFrameworkunifiedStatusNullPointer;
83   }
84
85   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
86   return l_e_status;
87 }
88
89 PVOID CNSConfigReader::GetDataPtr() {
90   if (NULL != m_pReader) {
91     // get the data pointer
92     return m_pReader->GetDataPtr();
93   } else {
94     return NULL;
95   }
96 }
97
98 BOOL CNSConfigReader::GetBool(const std::string &f_c_key) {
99   BOOL l_bResult = FALSE;
100
101   if (NULL != m_pReader) {
102     std::string l_c_temp = m_pReader->GetValue(f_c_key);
103
104     l_c_temp = FormatValue(l_c_temp);  // LCOV_EXCL_BR_LINE 11:except branch
105
106     if (!strcasecmp(l_c_temp.c_str(), "true")) {
107       l_bResult = TRUE;
108     }
109   } else {
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"
111   }
112
113   return l_bResult;
114 }
115
116 F_64 CNSConfigReader::GetDouble(const std::string &f_c_key) {
117   F_64 l_fValue = 0;
118
119   if (NULL != m_pReader) {
120     std::string l_c_temp = m_pReader->GetValue(f_c_key);
121
122     errno = EOK;
123
124     // convert string to double
125     l_fValue = strtod(l_c_temp.c_str(), NULL);
126
127     if (EOK != errno) {
128       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to double, errno=%d", l_c_temp.c_str(), errno);
129     }
130   } else {
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"
132   }
133
134   return l_fValue;
135 }
136
137 F_32 CNSConfigReader::GetFloat(const std::string &f_c_key) {
138   F_32 l_fValue = 0;
139
140   if (NULL != m_pReader) {
141     std::string l_c_temp = m_pReader->GetValue(f_c_key);
142     errno = EOK;
143     l_fValue = strtof(l_c_temp.c_str(), NULL);
144
145     if (EOK != errno) {
146       l_fValue = 0;
147       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to float, errno=%d", l_c_temp.c_str(), errno);
148     }
149   } else {
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"
151   }
152
153   return l_fValue;
154 }
155
156 SI_32 CNSConfigReader::GetInt(const std::string &f_c_key) {
157   UI_64 l_ui_value = 0;
158
159   if (NULL != m_pReader) {
160     std::string l_c_temp = m_pReader->GetValue(f_c_key);
161
162     errno = EOK;
163
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);
166     } else {
167       l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10);
168     }
169
170     if (EOK != errno) {
171       l_ui_value = 0;
172       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to int, errno=%d", l_c_temp.c_str(), errno);
173     }
174   } else {
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"
176   }
177
178   return static_cast<SI_32>(l_ui_value);
179 }
180
181 std::string CNSConfigReader::GetString(const std::string &f_c_key) {
182   std::string l_c_temp = "";
183
184   if (NULL != m_pReader) {
185     l_c_temp = m_pReader->GetValue(f_c_key);  // LCOV_EXCL_BR_LINE 11:except branch
186
187     l_c_temp = FormatValue(l_c_temp);  // LCOV_EXCL_BR_LINE 11:except branch
188   } else {
189     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
190   }
191
192   return l_c_temp;
193 }
194
195 EFrameworkunifiedStatus CNSConfigReader::GetBool(const std::string &f_c_key, BOOL &f_b_value) {
196   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
197
198   std::string l_c_temp = "";
199
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
202
203     if (eFrameworkunifiedStatusOK == l_e_status) {
204       l_c_temp = FormatValue(l_c_temp);  // LCOV_EXCL_BR_LINE 11:except branch
205
206       if (!strcasecmp(l_c_temp.c_str(), "true")) {
207         f_b_value = TRUE;
208       } else if (!strcasecmp(l_c_temp.c_str(), "false")) {
209         f_b_value = FALSE;
210       } else {
211         l_e_status = eFrameworkunifiedStatusErrOther;
212       }
213     }
214   } else {
215     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
216     l_e_status = eFrameworkunifiedStatusNullPointer;
217   }
218
219   return l_e_status;
220 }
221
222 EFrameworkunifiedStatus CNSConfigReader::GetDouble(const std::string &f_c_key, F_64 &f_d_value) {
223   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
224
225   std::string l_c_temp = "";
226
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
229
230     if (eFrameworkunifiedStatusOK == l_e_status) {
231       errno = EOK;
232       f_d_value = strtod(l_c_temp.c_str(), NULL);
233
234       if (EOK != errno) {
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;
237       }
238     }
239   } else {
240     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
241     l_e_status = eFrameworkunifiedStatusNullPointer;
242   }
243
244   return l_e_status;
245 }
246
247 EFrameworkunifiedStatus CNSConfigReader::GetFloat(const std::string &f_c_key, F_32 &f_f_value) {
248   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
249
250   std::string l_c_temp = "";
251
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
254
255     if (eFrameworkunifiedStatusOK == l_e_status) {
256       errno = EOK;
257       f_f_value = strtof(l_c_temp.c_str(), NULL);
258
259       if (EOK != errno) {
260         f_f_value = 0;
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;
263       }
264     }
265   } else {
266     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
267     l_e_status = eFrameworkunifiedStatusNullPointer;
268   }
269
270   return l_e_status;
271 }
272
273 EFrameworkunifiedStatus CNSConfigReader::GetInt(const std::string &f_c_key, SI_32 &f_i_value) {
274   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
275
276   std::string l_c_temp = "";
277
278   UI_64 l_ui_value = 0;
279
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
282
283     if (eFrameworkunifiedStatusOK == l_e_status) {
284       errno = EOK;
285
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);
288       } else {
289         l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10);
290       }
291
292       f_i_value = static_cast<SI_32>(l_ui_value);
293
294       if (EOK != errno) {
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;
297       }
298     }
299   } else {
300     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
301     l_e_status = eFrameworkunifiedStatusNullPointer;
302   }
303
304   return l_e_status;
305 }
306
307 EFrameworkunifiedStatus CNSConfigReader::GetString(const std::string &f_c_key, std::string &f_c_value) {
308   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
309
310   std::string l_c_temp = "";
311
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
314
315     if (eFrameworkunifiedStatusOK == l_e_status) {
316       l_c_temp = FormatValue(l_c_temp);  // LCOV_EXCL_BR_LINE 11:except branch
317
318       f_c_value.assign(l_c_temp);
319     }
320   } else {
321     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL");
322     l_e_status = eFrameworkunifiedStatusNullPointer;
323   }
324
325   return l_e_status;
326 }
327
328 std::string CNSConfigReader::FormatValue(const std::string &f_c_value) {
329   std::string l_c_newvalue = f_c_value;
330
331   if (0 < f_c_value.length()) {
332     UI_32 l_uiStrPos = static_cast<UI_32>(l_c_newvalue.length() - 1);
333
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);
336
337       if (0 == l_uiStrPos) {
338         break;
339       }
340
341       l_uiStrPos--;
342     }
343   }
344
345   return l_c_newvalue;
346 }