1 /*!
2 * \copy
3 * Copyright (c) 2008-2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * read_config.h
32 *
33 * Abstract
34 * Class for reading parameter settings in a configure file.
35 *
36 * History
37 * 08/18/2008 Created
38 *
39 *****************************************************************************/
40
41 #define _CRT_SECURE_NO_WARNINGS
42 #include <stdio.h>
43 #include <string.h>
44 #include "read_config.h"
45
CReadConfig()46 CReadConfig::CReadConfig()
47 : m_pCfgFile (NULL)
48 , m_strCfgFileName ("")
49 , m_iLines (0) {
50 }
51
CReadConfig(const char * kpConfigFileName)52 CReadConfig::CReadConfig (const char* kpConfigFileName)
53 : m_pCfgFile (0)
54 , m_strCfgFileName (kpConfigFileName)
55 , m_iLines (0) {
56 if (strlen (kpConfigFileName) > 0) { // confirmed_safe_unsafe_usage
57 m_pCfgFile = fopen (kpConfigFileName, "r");
58 }
59 }
60
CReadConfig(const std::string & kpConfigFileName)61 CReadConfig::CReadConfig (const std::string& kpConfigFileName)
62 : m_pCfgFile (0)
63 , m_strCfgFileName (kpConfigFileName)
64 , m_iLines (0) {
65 if (kpConfigFileName.length() > 0) {
66 m_pCfgFile = fopen (kpConfigFileName.c_str(), "r");
67 }
68 }
69
~CReadConfig()70 CReadConfig::~CReadConfig() {
71 if (m_pCfgFile) {
72 fclose (m_pCfgFile);
73 m_pCfgFile = NULL;
74 }
75 }
76
Openf(const char * kpStrFile)77 void CReadConfig::Openf (const char* kpStrFile) {
78 if (kpStrFile != NULL && strlen (kpStrFile) > 0) { // confirmed_safe_unsafe_usage
79 m_strCfgFileName = kpStrFile;
80 m_pCfgFile = fopen (kpStrFile, "r");
81 }
82 }
83
ReadLine(std::string * pVal,const int kiValSize)84 long CReadConfig::ReadLine (std::string* pVal, const int kiValSize/* = 4*/) {
85 if (m_pCfgFile == NULL || pVal == NULL || kiValSize <= 1)
86 return 0;
87
88 std::string* strTags = &pVal[0];
89 int nTagNum = 0, n = 0;
90 bool bCommentFlag = false;
91
92 while (n < kiValSize) {
93 pVal[n] = "";
94 ++ n;
95 }
96
97 do {
98 const char kCh = (char)fgetc (m_pCfgFile);
99
100 if (kCh == '\n' || feof (m_pCfgFile)) {
101 ++ m_iLines;
102 break;
103 }
104 if (kCh == '#')
105 bCommentFlag = true;
106 if (!bCommentFlag) {
107 if (kCh == '\t' || kCh == ' ') {
108 if (nTagNum >= kiValSize - 1)
109 break;
110 if (! (*strTags).empty()) {
111 ++ nTagNum;
112 strTags = &pVal[nTagNum];
113 }
114 } else
115 *strTags += kCh;
116 }
117
118 } while (true);
119
120 return 1 + nTagNum;
121 }
122
EndOfFile()123 const bool CReadConfig::EndOfFile() {
124 if (m_pCfgFile == NULL)
125 return true;
126 return feof (m_pCfgFile) ? true : false;
127 }
128
GetLines()129 const int CReadConfig::GetLines() {
130 return m_iLines;
131 }
132
ExistFile()133 const bool CReadConfig::ExistFile() {
134 return (m_pCfgFile != NULL);
135 }
136
GetFileName()137 const std::string& CReadConfig::GetFileName() {
138 return m_strCfgFileName;
139 }
140