Ace reads the configuration file

Source: Internet
Author: User

ACE reads the configuration file.
2008-01-22
Category:

Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://wanttocry.blogbus.com/logs/14298003.html

 

Post Code first

# Include "ace/OS. h"
# Include "ace/OS _Main.h"
# Include "ace/Configuration. h"
# Include "ace/Configuration_Import_Export.h"
# Include <iostream>
# Include <string>

# Ifdef _ DEBUG
# Pragma comment (lib, "aced. lib ")
# Else
# Pragma comment (lib, "ace. lib ")
# Endif

Struct Config_Info
{
ACE_TString person_name;
ACE_TString person_sex;
ACE_TString dog_name;
ACE_TString dog_sex;

Int person_age;
Int dog_age;
};
Int ACE_TMAIN (int argc, ACE_TCHAR * argv [])
{

Config_Info config_info;
 

ACE_Configuration_Heap config _;

Config _. open ();
 
ACE_Ini_ImpExp ini_impexp (config _);

Ini_impexp.import_config (argv [1]);
 
ACE_Configuration_Section_Key section_person;
Int ret = config _. open_section (config _. root_section (),
ACE_TEXT ("PERSON"), 0, section_person );
Ret = config _. get_string_value (section_person, ACE_TEXT ("name "),
Config_info.person_name );
Ret = config _. get_string_value (section_person, ACE_TEXT ("sex "),
Config_info.person_sex );

ACE_TString person_age;
Ret = config _. get_string_value (section_person,
ACE_TEXT ("age"), person_age );
Config_info.person_age = ACE_ OS: atoi (person_age.c_str ());

ACE_Configuration_Section_Key section_dog;
Config _. open_section (config _. root_section (), ACE_TEXT ("DOG"), 0, section_dog );
Config _. get_string_value (section_dog, ACE_TEXT ("name"), config_info.dog_name );
Config _. get_string_value (section_dog, ACE_TEXT ("sex"), config_info.dog_sex );

ACE_TString dog_age;
Config _. get_string_value (section_dog, ACE_TEXT ("age"), dog_age );
Config_info.dog_age = ACE_ OS: atoi (dog_age.c_str ());

Std: cout <config_info.person_name <"/n" <config_info.person_age <"/n"
<Config_info.person_sex <"/n" <config_info.dog_name <"/n"
<Config_info.dog_age <"/n" <config_info.dog_sex <std: endl;

System ("pause ");
Return 0;

}

Three classes are used here;

ACE_Configuration_Heap

ACE_Configuration_Section_Key

ACE_Ini_ImpExp

First, initialize an ACE_Configuration_Heap to store the information obtained from reading the configuration file.

Ace_configuration_heap.open () I have read the source code of Ace. Based on my judgment, it is used to allocate space for heap. I have not fully understood how to do it.

Create an ace_ini_impexp object. The basic function of ace_ini_impexp is an adapter that initializes the section_map member of ace_configuration_heap through its import_config.

The ini configuration file divides configuration items into multiple sections.

Use the open_section () method of ace_configuration_heap to initialize an ace_configuration_section_key object to save a specific section.

Finally, the value of the configuration item is obtained through the get_string_value () get_integer_value () and get_binary_value () Methods of ace_configuration_heap. In my first understanding of string, integer is to use get_string if the configured value is string. If it is int, get_integer_value is used. Later I found that I was wrong. The three methods correspond to the three storage methods, which are stored by character, by integer, or by binary. The configuration items on one side are stored in string, so most of them use get_string_value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.