Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created November 11, 2014 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/65f24a68914709a6042c to your computer and use it in GitHub Desktop.
Save komasaru/65f24a68914709a6042c to your computer and use it in GitHub Desktop.
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