C++ source code to get yaml informations by yaml-cpp 0.3 series.
/* | |
* Getting yaml informations by yaml-cpp 0.3 series. | |
*/ | |
#include <fstream> | |
#include <string> | |
#include "yaml-cpp/yaml.h" | |
using namespace std; | |
struct Db { | |
string hostname; | |
string database; | |
string username; | |
string password; | |
}; | |
void operator >> (const YAML::Node& node, Db& db) { | |
node["hostname"] >> db.hostname; | |
node["database"] >> db.database; | |
node["username"] >> db.username; | |
node["password"] >> db.password; | |
} | |
int main(int argc, char* argv[]){ | |
try{ | |
ifstream fin("config2.yml"); | |
YAML::Parser parser(fin); | |
YAML::Node doc; | |
parser.GetNextDocument(doc); | |
Db db; | |
doc[0] >> db; | |
cout << "* DB Setting:\n" | |
<< " - HOSTNAME: " << db.hostname << "\n" | |
<< " - DATABASE: " << db.database << "\n" | |
<< " - USERNAME: " << db.username << "\n" | |
<< " - PASSWORD: " << db.password << endl; | |
} catch(YAML::ParserException& e) { | |
cerr << e.what() << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment