summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_parser.cpp
blob: a2abd785eed53409e3cba02b2abb8f164cf7d016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NS_ConfigParser
/// \brief    This file contains implementation of CCFGParser class.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////

#include <ns_cfg_internal.h>
#include <ns_cfg_parser.h>
#include <ns_config_parser_frameworkunifiedlog.h>

#include <fstream>
#include <string>

CCFGParser::CCFGParser()
  : m_pmCFGData(NULL) {
}

// LCOV_EXCL_START 200:CCFGParser is not external API class, this constructed function isn't used by other function
CCFGParser::CCFGParser(const std::string &f_cfilepath)
  : m_pmCFGData(NULL) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  CFGParseFile(f_cfilepath);
}
// LCOV_EXCL_STOP

CCFGParser::~CCFGParser() {
  CFGData_Type::iterator l_itCfgStructure;

  if (NULL != m_pmCFGData) {  // LCOV_EXCL_BR_LINE 5:m_pmCFGData cannot be NULL, because internal logic guarantee
    // release the structure
    for (l_itCfgStructure = m_pmCFGData->begin();
         l_itCfgStructure != m_pmCFGData->end();
         l_itCfgStructure++) {
      CBlock *l_pBlock = (*l_itCfgStructure).second;
      if (NULL != l_pBlock) {  // LCOV_EXCL_BR_LINE 5:l_pBlock cannot be NULL, because map insert data success.
        // clear the vector
        l_pBlock->m_vElementPair.clear();

        delete l_pBlock;  // LCOV_EXCL_BR_LINE 11:except branch
        l_pBlock = NULL;
      }
    }

    delete m_pmCFGData;  // LCOV_EXCL_BR_LINE 11:except branch
    m_pmCFGData = NULL;
  }
}

EFrameworkunifiedStatus CCFGParser::CFGParseFile(const std::string &f_cfilepath) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  size_t l_uiPos = 0;
  std::string l_cLine("");
  std::string l_cTag("");  // LCOV_EXCL_BR_LINE 11:except branch
  std::string l_cComment("");  // LCOV_EXCL_BR_LINE 11:except branch

  m_pmCFGData = new(std::nothrow) CFGData_Type();  // LCOV_EXCL_BR_LINE 11:except branch

  if (NULL != m_pmCFGData) {  // LCOV_EXCL_BR_LINE 5:new's error case
    // open the file
    std::ifstream l_objFile(f_cfilepath.c_str());  // LCOV_EXCL_BR_LINE 11:except branch

    // check if file is opened
    if (l_objFile.is_open()) {
      CBlock *l_pBlock = NULL;

      // read until end of file is not reached or any other stream error occurred
      while (!l_objFile.eof()) {
        // read the line from the file
        getline(l_objFile, l_cLine);  // LCOV_EXCL_BR_LINE 11:except branch

        // if line is a comment
        if ('#' == l_cLine[0] || ';' == l_cLine[0]) {
          if (!l_cComment.empty()) {
            l_cComment.append("\n");  // LCOV_EXCL_BR_LINE 11:except branch
          }
          l_cComment.append(l_cLine);
        } else if ('[' == l_cLine[0]) {  // if line is main tag
          // creates new CBlock object,
          // each block contains information of single like all its key-value pair and
          // all the comments associated with it.
          l_pBlock = new CBlock();  // LCOV_EXCL_BR_LINE 11:except branch
          if (NULL != l_pBlock) {  // LCOV_EXCL_BR_LINE 5:new's error case
            // set the tag comment
            l_pBlock->m_cComment = l_cComment;
            l_cComment.clear();

            if (std::string::npos != (l_uiPos = l_cLine.find(']'))) {
              // set the tag
              l_cTag = l_cLine.substr(1, l_uiPos - 1);  // LCOV_EXCL_BR_LINE 11:except branch

              // insert the block with its tag name in structure(map)
              m_pmCFGData->insert(make_pair(l_cTag, l_pBlock));  // NOLINT (readability/nolint)
            } else {
              delete l_pBlock;  // LCOV_EXCL_BR_LINE 11:except branch
              l_pBlock = NULL;

              // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
              FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "End Tag missing");
              // LCOV_EXCL_BR_STOP
              l_eStatus = eFrameworkunifiedStatusErrOther;
              break;
            }
          } else {
            // LCOV_EXCL_START 5:new's error case
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error allocating memory for l_pBlock");
            l_eStatus = eFrameworkunifiedStatusNullPointer;
            break;
            // LCOV_EXCL_STOP
          }
        } else if (std::string::npos != (l_uiPos = l_cLine.find('='))) {  // LCOV_EXCL_BR_LINE 11:except branch
          if (NULL != l_pBlock) {  // LCOV_EXCL_BR_LINE 5:new's error case
            // create new node object,
            // each node object contains the information of singlr key-value pair and
            /// it's comment(if any)
            // LCOV_EXCL_BR_START 11:except branch
            CNode l_objNode;
            l_objNode.m_cComment.append(l_cComment);
            l_objNode.m_cTag = l_cLine.substr(0, l_uiPos);
            l_objNode.m_cValue = l_cLine.substr(l_uiPos + 1);

            // insert the node object in block
            l_pBlock->m_vElementPair.push_back(l_objNode);
            // LCOV_EXCL_BR_STOP

            l_cComment.clear();
          } else {
            // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
            FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "l_pBlock is NULL");
            // LCOV_EXCL_BR_STOP
            l_eStatus = eFrameworkunifiedStatusNullPointer;
            break;
          }
        }
      }

      // last comment
      if (!l_cComment.empty()) {
        m_cLastComment = l_cComment;
      } else {
        m_cLastComment.clear();
      }

      // close the file
      l_objFile.close();  // LCOV_EXCL_BR_LINE 11:except branch
    } else {
      l_eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error Opening File");
      // LCOV_EXCL_BR_STOP
    }
  } else {
    // LCOV_EXCL_START 5:new's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL");
    l_eStatus = eFrameworkunifiedStatusNullPointer;
    // LCOV_EXCL_STOP
  }

  return l_eStatus;
}

std::string CCFGParser::CFGGetData(const std::string &f_ckey) {
  CFGData_Type::iterator l_itCfgStructure;
  std::string l_cHeadTag = "";
  std::string l_cTag = "";  // LCOV_EXCL_BR_LINE 11:except branch
  std::string l_cValue = "";  // LCOV_EXCL_BR_LINE 11:except branch

  size_t l_uiPos = 0;

  if (NULL != m_pmCFGData && !f_ckey.empty()) {  // LCOV_EXCL_BR_LINE 5:new's error case(in function CFGParseFile)
    if (std::string::npos != (l_uiPos = f_ckey.find('.'))) {  // LCOV_EXCL_BR_LINE 11:except branch
      l_cHeadTag = f_ckey.substr(0, l_uiPos);  // LCOV_EXCL_BR_LINE 11:except branch
      l_cTag = f_ckey.substr(l_uiPos + 1);  // LCOV_EXCL_BR_LINE 11:except branch
    }

    // search for the key and its associated value in internal structure
    for (l_itCfgStructure = m_pmCFGData->begin();
         l_itCfgStructure != m_pmCFGData->end();
         l_itCfgStructure++) {
      CBlock *l_pBlock = (*l_itCfgStructure).second;

      if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) {
        for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) {
          if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) {
            // LCOV_EXCL_BR_START 200:After l_cValue assignment is empty,there is no other place to assignment the value
            if (!l_cValue.empty()) {
            // LCOV_EXCL_BR_STOP
              // LCOV_EXCL_START 200:After l_cValue assignment is empty,there is no other place to assignment the value
              AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
              l_cValue.append("\n");
              // LCOV_EXCL_STOP 200
            }
            l_cValue.append(l_pBlock->m_vElementPair[l_uiCount].m_cValue);
          }
        }
      }
    }
  }

  return l_cValue;
}

EFrameworkunifiedStatus CCFGParser::CFGGetData(const std::string &f_ckey, std::string &f_cvalue) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  CFGData_Type::iterator l_itCfgStructure;
  std::string l_cHeadTag = "";
  std::string l_cTag = "";  // LCOV_EXCL_BR_LINE 11:except branch
  std::string l_cValue = "";  // LCOV_EXCL_BR_LINE 11:except branch

  size_t l_uiPos = 0;

  if (NULL != m_pmCFGData && !f_ckey.empty()) {  // LCOV_EXCL_BR_LINE 5:new's error case(in function CFGParseFile)
    if (std::string::npos != (l_uiPos = f_ckey.find('.'))) {  // LCOV_EXCL_BR_LINE 11:except branch
      l_cHeadTag = f_ckey.substr(0, l_uiPos);  // LCOV_EXCL_BR_LINE 11:except branch
      l_cTag = f_ckey.substr(l_uiPos + 1);  // LCOV_EXCL_BR_LINE 11:except branch
    }

    // search for the key and its associated value in internal structure
    for (l_itCfgStructure = m_pmCFGData->begin();
         l_itCfgStructure != m_pmCFGData->end();
         l_itCfgStructure++) {
      CBlock *l_pBlock = (*l_itCfgStructure).second;

      if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) {
        for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) {
          if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) {
            if (!f_cvalue.empty()) {
              f_cvalue.append("\n");
            }
            f_cvalue.append(l_pBlock->m_vElementPair[l_uiCount].m_cValue);

            l_eStatus = eFrameworkunifiedStatusOK;
          }
        }
      }
    }
  }

  return l_eStatus;
}

EFrameworkunifiedStatus CCFGParser::CFGSetData(const std::string &f_cKey, std::string f_cvalue) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  CFGData_Type::iterator l_itCfgStructure;
  std::string l_cHeadTag = "";
  std::string l_cTag = "";  // LCOV_EXCL_BR_LINE 11:except branch
  std::string l_cValue = "";  // LCOV_EXCL_BR_LINE 11:except branch
  size_t l_uiPos = 0;

  // LCOV_EXCL_BR_START 6:f_cKey is not NULL, because checked in CCFGWriter::SetValue
  if (NULL != m_pmCFGData && !f_cKey.empty()) {
  // LCOV_EXCL_BR_STOP
    if (std::string::npos != (l_uiPos = f_cKey.find('.'))) {  // LCOV_EXCL_BR_LINE 11:except branch
      l_cHeadTag = f_cKey.substr(0, l_uiPos);  // LCOV_EXCL_BR_LINE 11:except branch
      l_cTag = f_cKey.substr(l_uiPos + 1);  // LCOV_EXCL_BR_LINE 11:except branch
    }

    // search for the key in internal structure and set the data for it
    for (l_itCfgStructure = m_pmCFGData->begin();
         l_itCfgStructure != m_pmCFGData->end();
         l_itCfgStructure++) {
      CBlock *l_pBlock = (*l_itCfgStructure).second;

      if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) {
        for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) {
          if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) {
            l_pBlock->m_vElementPair[l_uiCount].m_cValue.assign(f_cvalue);
            l_eStatus = eFrameworkunifiedStatusOK;
          }
        }
      }
    }
  } else {
    // LCOV_EXCL_START 6:f_cKey is not NULL, because checked in CCFGWriter::SetValue
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    l_eStatus = eFrameworkunifiedStatusNullPointer;
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL");
    //  LCOV_EXCL_STOP
  }

  return l_eStatus;
}

EFrameworkunifiedStatus CCFGParser::CFGSaveData(const std::string &f_cfilepath) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  CFGData_Type::iterator l_itCfgStructure;

  if (NULL != m_pmCFGData) {  // LCOV_EXCL_BR_LINE 5:m_pmCFGData cannot be NULL, because internal logic guarantee
    std::ofstream l_objFile(f_cfilepath.c_str());

    // check if file is open
    if (l_objFile.is_open()) {
      // write the updated CFG structure back to the file
      for (l_itCfgStructure = m_pmCFGData->begin();
           l_itCfgStructure != m_pmCFGData->end();
           l_itCfgStructure++) {
        CBlock *l_pBlock = (*l_itCfgStructure).second;
        if (NULL != l_pBlock) {  // LCOV_EXCL_BR_LINE 11:except branch
          // write the main tag comment in file
          if (!l_pBlock->m_cComment.empty()) {
            l_objFile << l_pBlock->m_cComment << std::endl;
          }

          // write main tag to file
          l_objFile << "[" << (*l_itCfgStructure).first << "]" << std::endl;

          for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) {
            // comment over the key-value pair
            if (!l_pBlock->m_vElementPair[l_uiCount].m_cComment.empty()) {
              l_objFile << l_pBlock->m_vElementPair[l_uiCount].m_cComment << std::endl;;
            }

            // key-value
            l_objFile << l_pBlock->m_vElementPair[l_uiCount].m_cTag << "="
                      << l_pBlock->m_vElementPair[l_uiCount].m_cValue << std::endl;
          }
        }
      }

      // last comment
      if (!m_cLastComment.empty()) {
        l_objFile << m_cLastComment << std::endl;
      }

      // close the file
      l_objFile.close();  // LCOV_EXCL_BR_LINE 11:except branch
    } else {
      l_eStatus = eFrameworkunifiedStatusFail;
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Cannot Open File");
    }
  } else {
    // LCOV_EXCL_START 5:m_pmCFGData cannot be NULL, because internal logic guarantee
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    l_eStatus = eFrameworkunifiedStatusNullPointer;
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL");
    // LCOV_EXCL_STOP
  }

  return l_eStatus;
}