我一直觉得ACE_Service_Config是非常高层次,非常麻烦的咚咚,今天仅仅在里面抽取了一点内容用来读取配置文件,格式类似于windows的ini,但不同的是这里面区分大小写。ACE里面支持对Win32注册表的读取,我个人对此不感兴趣,因为这丧失了跨平台的特性。
#include
"ace/Configuration.h"
#include
"ace/Configuration_Import_Export.h"
class
CConfigTool
{
public
:
int
GetString
(
const
char
*
szSection
,
const
char
*
szKey
,
ACE_TString
&
strValue
);
int
GetInteger
(
const
char
*
szSection
,
const
char
*
szKey
,
int
&
nValue
);
int
open
(
const
ACE_TCHAR
*
filename
);
int
close
();
CConfigTool
();
virtual
~
CConfigTool
();
private
:
ACE_Configuration_Section_Key
root_key_
;
ACE_Ini_ImpExp
*
impExp_
;
ACE_Configuration_Heap
config
;
};
CConfigTool
::
CConfigTool
():
impExp_
(
NULL
)
{
}
CConfigTool
::~
CConfigTool
()
{
if
(
impExp_
)
delete
impExp_
;
impExp_
=
NULL
;
}
int
CConfigTool
::
open
(
const
ACE_TCHAR
*
filename
)
{
if
(
this
->
config
.
open
() == -1)
return
-1;
this
->
impExp_
=
new
ACE_Ini_ImpExp
(
config
);
return
this
->
impExp_
->
import_config
(
filename
);
}
int
CConfigTool
::
GetString
(
const
char
*
szSection
,
const
char
*
szKey
,
ACE_TString
&
strValue
)
{
if
(
config
.
open_section
(
config
.
root_section
(),
ACE_TEXT
(
szSection
),0,
this
->
root_key_
)==-1)
return
-1;
return
config
.
get_string_value
(
this
->
root_key_
,
szKey
,
strValue
);
}
int
CConfigTool
::
GetInteger
(
const
char
*
szSection
,
const
char
*
szKey
,
int
&
nValue
)
{
ACE_TString
strValue
;
if
(
config
.
open_section
(
config
.
root_section
(),
ACE_TEXT
(
szSection
),0,
this
->
root_key_
)==-1)
return
-1;
if
(
config
.
get_string_value
(
this
->
root_key_
,
szKey
,
strValue
) == -1)
return
-1;
nValue
=
ACE_OS
::
atoi
(
strValue
.
c_str
());
if (nValue == 0 && strValue != "0")
return
-1;
return
nValue
;
}
int
CConfigTool
::
close
()
{
if
(
impExp_
)
{
delete
impExp_
;
impExp_
=
NULL
;
}
return
0;
}