For this purpose, I'm using Boost Property Tree. According to the tutorial on Boost Property Tree, the code was very easy to handle. For that, I separate the piece away from the core object and put them into a brand new object, where the object will take the sole responsibility in handling the reading and writing of XML content. And for the sake of the whole execution, the object will have only one instance in the memory. And then I give a name for this object called Configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Configuration::Configuration() | |
{ | |
try { | |
boost::property_tree::read_xml(filesystem::current_path().string() + GENERIC_PATH_SEPARATOR + INDEX_FILENAME, backupConfig); | |
} | |
catch( const boost::property_tree::ptree_error &e ) { | |
cout << e.what() << endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Configuration | |
{ | |
private: | |
Configuration(); | |
public: | |
static Configuration* getInstance() | |
{ | |
if( !instance ) | |
instance = new Configuration(); | |
return instance; | |
} | |
inline void updateConfiguration(string configPath) | |
{ | |
boost::property_tree::write_xml(configPath, backupConfig); | |
} | |
boost::property_tree::ptree readConfigFile(); | |
boost::property_tree::ptree readConfigFile(string configPath); | |
public: | |
boost::property_tree::ptree getBackupConfig() const { return backupConfig; } | |
private: | |
static Configuration *instance; | |
boost::property_tree::ptree backupConfig; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<backup> | |
<file>/home/debug/FolderA/file_1.txt</file> | |
<file>/home/debug/FolderA/file_2.txt</file> | |
<file>/test/FolderA/test1.log</file> | |
<file>/test/FolderA/test2.log</file> | |
</backup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BOOST_AUTO_TEST_CASE(TL_Property_read, *boost::unit_test::precondition(skipTest(false))) | |
{ | |
Configuration::getInstance()->readConfigFile("/home/debug/backup.xml"); | |
cout << "** count backup: " << Configuration::getInstance()->getBackupConfig().count("backup") << endl; | |
cout << "** count file: " << Configuration::getInstance()->getBackupConfig().count("backup.file") << endl; | |
for (const std::pair<std::string, ptree> &p : Configuration::getInstance()->getBackupConfig().get_child("backup")) | |
std::cout << "pass 1: " << p.second.get_value<std::string>() << '\n'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BOOST_AUTO_TEST_CASE(TL_Property_read, *boost::unit_test::precondition(skipTest(false))) | |
{ | |
using boost::property_tree::ptree; | |
Configuration::getInstance()->readConfigFile("/home/debug/backup.xml"); | |
cout << "** count backup: " << Configuration::getInstance()->getBackupConfig().count("backup") << endl; | |
cout << "** count file: " << Configuration::getInstance()->getBackupConfig().count("backup.file") << endl; | |
ptree d = Configuration::getInstance()->getBackupConfig().get_child("backup"); | |
for (const std::pair<std::string, ptree> &p : d) | |
std::cout << "pass 1: " << p.second.get_value<std::string>() << '\n'; | |
} |
No comments:
Post a Comment