Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ build/
# debug information files
*.dwo

lattice_files/expand.yaml
lattice_files/expand.pals.yaml
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.16)
project(YAMLWrapper)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand Down Expand Up @@ -85,7 +88,7 @@ file(COPY ${CMAKE_SOURCE_DIR}/lattice_files/
# endif()

add_library(yaml_c_wrapper SHARED src/yaml_c_wrapper.cpp)
target_link_libraries(yaml_c_wrapper yaml-cpp)
target_link_libraries(yaml_c_wrapper PUBLIC yaml-cpp)

add_executable(yaml_reader examples/yaml_reader.cpp)
target_link_libraries(yaml_reader yaml_c_wrapper)
18 changes: 16 additions & 2 deletions examples/yaml_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#include "../src/yaml_c_wrapper.h"
#include <iostream>

// See ex.pals.yaml for the example lattice file and expand.pals.yaml for the output of this file.

int main() {
// reading a lattice from a yaml file
YAMLNodeHandle handle = yaml_parse_file("../lattice_files/ex.pals.yaml");
// printing to terminal
std::cout << yaml_to_string(handle) << std::endl << std::endl;

// type checking
std::cout << (yaml_is_sequence(handle)) << "\n";
// prints "handle is of type sequence: 1", 1 meaning true
std::cout << "handle is of type sequence: " << (yaml_is_sequence(handle)) << "\n";

// accessing sequence
YAMLNodeHandle node = yaml_get_index(handle, 0);
/* prints
the first element is:
thingB:
kind: Sextupole
*/
std::cout << "the first element is: \n" << yaml_to_string(node) << "\n";

// accessing map
// prints "the value at key 'thingB' is: kind: Sextupole"
std::cout << "\nthe value at key 'thingB' is: " << yaml_to_string(yaml_get_key(node, "thingB")) << "\n";

// creating a new node that's a map
Expand All @@ -28,10 +37,15 @@ int main() {
YAMLNodeHandle scalar = yaml_create_scalar();
yaml_set_scalar_string(scalar, "magnet2");
yaml_set_at_index(sequence, 1, scalar);
// give sequence a name by putting it in a map:
YAMLNodeHandle magnets = yaml_create_map();
yaml_set_node(magnets, "magnets", sequence);

// adding new nodes to lattice
yaml_push_node(handle, map);
yaml_push_node(handle, sequence);
yaml_push_node(handle, magnets);

yaml_expand(handle);

// writing modified lattice file to expand.pals.yaml
yaml_write_file(handle, "../lattice_files/expand.pals.yaml");
Expand Down
3 changes: 2 additions & 1 deletion lattice_files/ex.pals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
length: 1.03
direction: -1
- a_subline: # Item a_subline is repeated three times
repeat: 3
repeat: 3
- include: "include.pals.yaml"
4 changes: 4 additions & 0 deletions lattice_files/include.pals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- line:
first: 1
second: 2
thrid: 3
Loading