Skip to content

Instantly share code, notes, and snippets.

@mikolasan
Created October 13, 2021 08:24
Show Gist options
  • Save mikolasan/d9306a03b23a9a85e4166e69810414b1 to your computer and use it in GitHub Desktop.
Save mikolasan/d9306a03b23a9a85e4166e69810414b1 to your computer and use it in GitHub Desktop.
Getting started with rapidyaml

Getting started with rapidyaml

  1. Add the lib into your repository as a submodule git submodule add https://github.com/biojppm/rapidyaml rapidyaml
  2. Recursively initialize this submodule git submodule update --init --recursive rapidyaml
  3. Alternatively (instead of 1. and 2.)
    • clone repository git clone --recursive https://github.com/biojppm/rapidyaml,
    • remove .git,
    • then cut and paste into your source tree
  4. Update your CMakeLists.txt
    • build the library add_subdirectory(rapidyaml)
    • find headers target_include_directories(test PRIVATE rapidyaml/src)
    • link the lib target_link_libraries(test ryml)
cmake_minimum_required(VERSION 3.14)
project(rapidyaml_getting_started VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(rapidyaml)
add_executable(test main.cpp)
target_include_directories(test PRIVATE rapidyaml/src)
target_link_libraries(test ryml)
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <ryml_std.hpp>
#include <ryml.hpp>
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (!in) {
std::cerr << "could not open " << filename << std::endl;
exit(1);
}
std::ostringstream contents;
contents << in.rdbuf();
return contents.str();
}
int main(int argc, char const *argv[])
{
std::string contents = get_file_contents("config.yml");
ryml::Tree tree = ryml::parse(ryml::to_csubstr(contents));
ryml::NodeRef show_mapping = tree["foo"];
for (ryml::NodeRef const& child : show_mapping.children()) {
std::cout << "key: " << child.key() << " val: " << child.val() << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment