-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcfg_reader.cpp
More file actions
131 lines (112 loc) · 3.4 KB
/
Copy pathcfg_reader.cpp
File metadata and controls
131 lines (112 loc) · 3.4 KB
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
/*----------------------------------------------------------------
// Copyright (C) 2013 广州,爱游
//
// 模块名:cfg_reader
// 创建者:Steven Yang
// 修改者列表:
// 创建日期:2013.1.5
// 模块描述:读配置类
//----------------------------------------------------------------*/
#include "cfg_reader.h"
#include "util.h"
#include "logger.h"
namespace mogo
{
CCfgReader::CCfgReader(const string& strFile)
:m_strFile(strFile), m_bScan(false)
{
}
CCfgReader::~CCfgReader()
{
map<string, map<string, string>*>::iterator iter = m_CfgDict.begin();
for(; iter != m_CfgDict.end(); ++iter)
{
delete iter->second;
iter->second = NULL;
}
}
string CCfgReader::GetValue(const char* szSection, const char* szName)
{
if(!m_bScan)
{
ScanCfgFile();
}
map<string, map<string, string>*>::const_iterator iter = m_CfgDict.find(szSection);
if(iter != m_CfgDict.end())
{
map<string, string>& tmp = *(iter->second);
map<string, string>::const_iterator iter2 = tmp.find(szName);
if(iter2 != tmp.end())
{
printf("get_value, section=[%s], %s=%s\n", szSection, szName, iter2->second.c_str());
return iter2->second;
}
}
ThrowException(-1, "Empty item value: [%s] %s!", szSection, szName);
static string strEmpty("");
return strEmpty;
}
string CCfgReader::GetOptValue(const char* szSection, const char* szName,
const string& strDefault)
{
try
{
return GetValue(szSection, szName);
}
catch (const CException& ex)
{
return strDefault;
}
}
void CCfgReader::ScanCfgFile()
{
ifstream file(m_strFile.c_str(), ios::in);
if(!file.is_open())
{
ThrowException(-1, "open config file failed: %s", m_strFile.c_str());
}
string strSection;
while(!file.eof())
{
char szLine[128];
file.getline(szLine, sizeof(szLine), 0x0A);
//window文件格式换行是0x0D0A,UNIX/LINUX是0x0A
string sLine = Trim(szLine);
if(sLine.empty() || sLine[0] == '#')
{
continue;
}
if(sLine[0] == '[' && sLine[sLine.size() - 1] == ']')
{
strSection = sLine.substr(1, sLine.size()-2);
continue;
}
string::size_type pos = sLine.find("=");
if(pos==string::npos)
{
continue;
}
if(strSection.empty())
{
ThrowException(-1, "item '%s' has not section", sLine.c_str());
}
string strTemp = sLine.substr(0, pos);
string s1 = Trim(strTemp);
strTemp = sLine.substr(pos+1, sLine.size() - pos - 1);
string s2 = Trim(strTemp);
map<string, map<string, string>*>::iterator iter = m_CfgDict.lower_bound(strSection);
if(iter != m_CfgDict.end() && strSection == iter->first)
{
iter->second->insert(make_pair(s1, s2));
//LogDebug("insert one item:", "%s %s %s", strSection, s1, s2);
}
else
{
map<string, string>* tmp = new map<string, string>;
tmp->insert(make_pair(s1, s2));
m_CfgDict.insert(iter, make_pair(strSection, tmp) );
}
}
m_bScan = true;
}
};