织梦CMS - 轻松建站从此开始!

罗索

ACE_Configuration_Heap类

罗索客 发布于 2010-01-27 15:10 点击:次 
该类用语访问合操纵配置信息,它们的接口由ACE_Configuration类来定义的
TAG:

该类用语访问合操纵配置信息,它们的接口由ACE_Configuration类来定义的;

1、ACE_Configuration_Heap:它可以用在几乎所有的平台上,在内存中保存所有配置信息.可以对内存的分配进行定制,使用持久的后备存储,但是最常用的是动态分配的堆内存,所以这个类的名字里面含有一个Heap;

2、ACE_Configuration_Win32Registry:它只能用在Windows平台上,它实现了ACE_Configuration类的接口,可以访问合操纵注册表中的数据;

3、ACE_Registry_ImpExp:这个类使用了一种文本格式,其中的每个值都有类型信息.

4、ACE_Ini_ImpExp:这个类使用的是较老的Windows"INI"文件的格式,其中的值没有相关联的类型信息,因此,不管原来的类型是什么,使用ACE_Ini_ImpExp导出的配置数据总是作为字符串数据导入;

下面的例子是ACE_Configuration_Heap类的使用样例,用于从配置文件中读取配置信息;配置文件的格式如下:

[SECTION]

"Key1"="Value1"

"Key2"="Value2"

"Key2"="Value2"

例子代码:

  1. #include "ace/Get_Opt.h"  
  2. #include "ace/Log_Msg.h"  
  3. #include "ace/SString.h"  
  4. #include "ace/OS_String.h"  
  5. #include "ace/Configuration.h"  
  6. #include "ace/Configuration_Import_Export.h"  
  7.  
  8. int ACE_TMAIN(int argc, ACE_TCHAR** argv)  
  9. {  
  10.  int opt = 0;  
  11.  ACE_TCHAR strCfgFile[128] = "";  
  12.  ACE_TCHAR strSegName[128] = "";  
  13.  
  14.  ACE_TCHAR options[] = "f:F:s:S:vV";  
  15.  ACE_Get_Opt cmd_opts(argc, argv, options);  
  16.  
  17.  //set long options  
  18.  cmd_opts.long_option(ACE_TEXT("config"), 'f', ACE_Get_Opt::ARG_REQUIRED);  
  19.  cmd_opts.long_option(ACE_TEXT("segment"), 's', ACE_Get_Opt::ARG_REQUIRED);  
  20.  
  21.  cmd_opts.long_option(ACE_TEXT("CONFIG"), 'f', ACE_Get_Opt::ARG_REQUIRED);  
  22.  cmd_opts.long_option(ACE_TEXT("SEGMENT"), 's', ACE_Get_Opt::ARG_REQUIRED);  
  23.  
  24.  while((opt = cmd_opts()) != EOF)  
  25.  {  
  26.   switch(opt)  
  27.   {  
  28.    case 'f':  
  29.    case 'F':  
  30.     {  
  31.      ACE_OS_String::strcpy(strCfgFile, cmd_opts.opt_arg());  
  32.     }  
  33.     break;  
  34.  
  35.    case 's':  
  36.    case 'S':  
  37.     {  
  38.      ACE_OS_String::strcpy(strSegName, cmd_opts.opt_arg());  
  39.     }  
  40.     break;  
  41.  
  42.    case 'v':  
  43.    case 'V':  
  44.     {  
  45.      ACE_DEBUG((LM_INFO, ACE_TEXT("test the configuration with ace\n")));  
  46.     }  
  47.     break;  
  48.   }  
  49.  }  
  50.  
  51.  ACE_DEBUG( (LM_INFO, 
  52. ACE_TEXT("You entered ---> CfgFileName: %s;  SegName: %s\n")
  53. , strCfgFile, strSegName) );  
  54.  
  55.  //declare a configuration object  
  56.  ACE_Configuration_Heap config;  
  57.  
  58.  if(config.open() == -1)  
  59.  {  
  60.   ACE_ERROR_RETURN( (LM_ERROR, 
  61. ACE_TEXT("--->failed to open the configuration object[%p]"), 
  62. ACE_TEXT("open_config")), -1);  
  63.  }  
  64.  
  65.  //declare a importer of config, and import the configuration-file  
  66.  ACE_Registry_ImpExp config_importer(config);  
  67.  if(config_importer.import_config(ACE_TEXT(strCfgFile)) == -1)  
  68.  {  
  69.   ACE_ERROR_RETURN( (LM_ERROR, 
  70. ACE_TEXT("--->failed to import config-file %s[%p]\n"), 
  71. strCfgFile, ACE_TEXT("import_config()")), -2);  
  72.  }  
  73.  
  74.  //declare a section-key pair, and open this section with the config-object;  
  75.  ACE_Configuration_Section_Key status_section;  
  76.  if(config.open_section(config.root_section(), 
  77. ACE_TEXT(strSegName), 0, status_section) == -1)  
  78.  {  
  79.   ACE_ERROR_RETURN( (LM_ERROR, ACE_TEXT("--->failed to open section %s[%p]\n"),
  80.  strSegName, ACE_TEXT("open_section(LOG_IMPORT)")), -3 );  
  81.  }  
  82.  
  83.  //get port  
  84.  ACE_TString port = "";  
  85.  if(config.get_string_value(status_section, ACE_TEXT("Port"), port) == -1)  
  86.  {  
  87.   ACE_ERROR_RETURN( (LM_ERROR, ACE_TEXT("--->failed to get HOST PORT[%p]\n"),
  88.  ACE_TEXT("get_string_value(Port)")), -4 );  
  89.  }  
  90.  
  91.  //get host ip  
  92.  ACE_TString strHostIP = "";  
  93.  if(config.get_string_value(status_section, ACE_TEXT("HostIP"), strHostIP) == -1)  
  94.  {  
  95.   ACE_ERROR_RETURN( (LM_ERROR, ACE_TEXT("--->failed to get HOST IP[%p]\n"),
  96.  ACE_TEXT("get_string_value()")), -5 );  
  97.  }  
  98.  
  99.  ACE_DEBUG( (LM_INFO, ACE_TEXT("IP: %s\nPort: %d\n"), strHostIP.c_str(),
  100.  ACE_OS::atoi(port.c_str())) );  
  101.  return 0;  

 

(罗索客)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201001/8402.html]
本文出处:网络博客 作者:罗索客
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容