I have a configuration file which contains a list of endpoint entries. Each entry is labeled with a heading of [endpt/n] (for the nth endpoint), and consists of a MAC and IP address. I'd like to use boost::program_options to read the addresses as strings, and push_back the results onto two vectors. I've looked through the program_options documentation, but I haven't been able to find what I'm looking for... Here's an example of an endpoint entry:
[endpt/2]
mac=ff-22-b6-33-91-3E
ip=133.22.32.222
Here's the code I'm currently using to add each endpoint's MAC and IP options to the boost::options_description:
std::vector<std::string> mac(NUM_ENDPTS);
std::vector<std::string> ip(NUM_ENDPTS);
for(int e = 0; e < NUM_ENDPTS; e++)
{
//convert endpoint 'e' to a string representing endpoint heading
std::stringstream tmp; tmp.clear(); tmp.str(""); tmp << e;
std::string strEndpt = tmp.str();
std::string heading = "endpt/"+strEndpt;
cfig_file_options.add_options()
((heading+".mac").c_str(), po::value<std::string>(&mac[e]), ("ENDPT MAC")
((heading+".ip").c_str(), po::value<std::string>( &ip[e]), ("ENDPT IP")
;
}
po::variables_map vm;
po::store(po::parse_config_file(config_stream, cfig_file_options), vm);
po::notify(vm);
This code works fine, but for a few reasons, I'd like to declare empty vectors for the MAC and IP addresses, and push_back the options onto them as boost reads them. I'm new to Boost, so any suggestions on better ways to read a list, or any other help would be greatly appreciated. Thanks!
It is straightforward to do exactly as you want. First, use
po::value< vector< std::string > >
instead ofpo::value< std::string >
as program-options provides special support of vectors. Then, reference your two vectors directly asThe key here is that all mac and ip addresses use common vectors for storage. I should point out that this won't necessarily associate the endpt number in the ini file with the correct index in the vector, unless a strict order is maintained in the file.