-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
156 lines (127 loc) · 3.95 KB
/
Config.cpp
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
#include "stdafx.h"
#include "Config.h"
#include <string>
#include "Poco\Exception.h"
#include "Common.h"
using namespace std;
using Poco::NotFoundException;
CConfig::CConfig (void)
: m_pXmlConfigure (new XMLConfiguration())
, m_eLoadCongfileStatus (UNLOAD)
, m_strConfigurePath ("")
{
}
CConfig::~CConfig (void)
{
}
TypeLoadStatus CConfig ::LoadConfigFile( const char *pXmlFileName )
{
if ( UNLOAD != m_eLoadCongfileStatus)
{
return m_eLoadCongfileStatus ;
}
m_eLoadCongfileStatus = LOADED_FAILED;
using std ::string;
char chErrMsg [512] = {0};
//加载配置文件
CString strIniPath;
if ( NULL == pXmlFileName)
{
strIniPath = GetMoudlePath() + _T("VoiceChat.xml");
}
else
{
strIniPath = pXmlFileName;
}
m_strConfigurePath = CString2String(strIniPath);
using Poco ::NotFoundException;
using Poco ::NullPointerException;
try
{
m_pXmlConfigure->load (m_strConfigurePath);
m_eLoadCongfileStatus = LOADED_SUCCESS;
}
catch ( NullPointerException & e)
{
OutputDebugStringA(e .what());
sprintf(chErrMsg , "解析配置文件:[%s]失败,原因:[%s]" , m_strConfigurePath.c_str(), e .what());
m_strErrMsg = chErrMsg;
}
catch ( std::exception &e )
{
OutputDebugStringA(e .what());
sprintf(chErrMsg , "解析配置文件:[%s]失败,原因:[%s]" , m_strConfigurePath.c_str(), e .what());
m_strErrMsg = chErrMsg;
}
return m_eLoadCongfileStatus ;
}
bool CConfig ::SetString( const std ::string &strKey, const std::string &strValue )
{
if ( LOADED_SUCCESS == m_eLoadCongfileStatus && m_pXmlConfigure )
{
m_pXmlConfigure->setString (strKey, strValue);
m_pXmlConfigure->save (m_strConfigurePath);
return true ;
}
return false ;
}
bool CConfig ::SetInt( const std ::string &strKey, int nValue)
{
if ( LOADED_SUCCESS == m_eLoadCongfileStatus && m_pXmlConfigure )
{
m_pXmlConfigure->setInt (strKey, nValue);
m_pXmlConfigure->save (m_strConfigurePath);
return true ;
}
return false ;
}
std::string CConfig:: GetString(const std:: string & strKey)
{
string strValue = "";
if ( LOADED_SUCCESS == m_eLoadCongfileStatus && m_pXmlConfigure )
{
m_strErrMsg = "";
try
{
strValue = m_pXmlConfigure-> getString(strKey );
}
catch ( NotFoundException & e)
{
char chErrMsg [512] = {0};
sprintf(chErrMsg , "参数[%s]未设置:%s" , strKey .c_str(), e.what ());
m_strErrMsg = chErrMsg;
}
catch (std::exception &e)
{
char chErrMsg [512] = {0};
sprintf(chErrMsg , "参数[%s]未设置:%s" , strKey .c_str(), e.what ());
m_strErrMsg = chErrMsg;
}
}
return strValue ;
}
int CConfig ::GetInt( const std ::string &strKey)
{
int nValue = 0;
if ( LOADED_SUCCESS == m_eLoadCongfileStatus && m_pXmlConfigure )
{
m_strErrMsg = "";
try
{
nValue = m_pXmlConfigure-> getInt(strKey );
}
catch ( Poco::NotFoundException & e)
{
char chErrMsg [512] = {0};
sprintf(chErrMsg , "参数[%s]未设置:%s" , strKey .c_str(), e.what ());
m_strErrMsg = chErrMsg;
}
catch (std::exception &e)
{
char chErrMsg [512] = {0};
sprintf(chErrMsg , "参数[%s]未设置:%s" , strKey .c_str(), e.what ());
m_strErrMsg = chErrMsg;
}
}
return nValue ;
}