Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_ConfigParser / src / ns_cfg_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 CCFGWriter class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include <ns_cfg_parser.h>
32 #include <ns_cfg_writer.h>
33 #include <ns_config_parser_frameworkunifiedlog.h>
34
35 #include <fstream>
36 #include <string>
37
38
39 CCFGWriter::CCFGWriter(): m_cFilePath(""), m_pCFGParser(NULL) {  // LCOV_EXCL_BR_LINE 11:except branch
40   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
41 }
42
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());
45
46   m_pCFGParser = new CCFGParser(m_cFilePath);
47
48   if (NULL == m_pCFGParser) {
49     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser");
50   }
51 }
52
53 CCFGWriter::~CCFGWriter() {
54   if (NULL != m_pCFGParser) {
55     delete m_pCFGParser;  // LCOV_EXCL_BR_LINE 11:except branch
56     m_pCFGParser = NULL;
57   }
58 }
59
60 EFrameworkunifiedStatus CCFGWriter::ParseFile(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   m_cFilePath.assign(f_c_filepath);
67
68   m_pCFGParser = new(std::nothrow) CCFGParser();  // LCOV_EXCL_BR_LINE 11:except branch
69
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;
75     // LCOV_EXCL_STOP
76   } else {
77     l_e_status = m_pCFGParser->CFGParseFile(f_c_filepath);
78   }
79
80   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
81   return l_e_status;
82 }
83
84 EFrameworkunifiedStatus CCFGWriter::SetPath(const std::string &f_c_path) {
85   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
86
87   if (!f_c_path.empty()) {  // LCOV_EXCL_BR_LINE 11:except branch
88     // set the file path
89     m_cFilePath.assign(f_c_path);
90   } else {
91     l_e_status = eFrameworkunifiedStatusInvldParam;
92   }
93
94   return l_e_status;
95 }
96
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);
101   } else {
102     m_pCFGParser = NULL;
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"
104   }
105 }
106
107 EFrameworkunifiedStatus CCFGWriter::SetValue(const std::string &f_c_key, std::string f_c_value) {
108   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
109
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
113   } else {
114     l_e_status = eFrameworkunifiedStatusInvldParam;
115   }
116
117   return l_e_status;
118 }
119
120 EFrameworkunifiedStatus CCFGWriter::SaveData() {
121   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
122
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);
127     } else {
128       l_e_status = eFrameworkunifiedStatusFail;
129     }
130   } else {
131     l_e_status = eFrameworkunifiedStatusNullPointer;
132   }
133
134   return l_e_status;
135 }