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
65 changes: 5 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,66 +29,11 @@ add_subdirectory(tests)
file(COPY ${CMAKE_SOURCE_DIR}/lattice_files/
DESTINATION ${CMAKE_BINARY_DIR}/lattice_files/)

# if(APPLE)
# # Check for package managers
# execute_process(COMMAND which brew OUTPUT_VARIABLE HOMEBREW_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)
# execute_process(COMMAND which port OUTPUT_VARIABLE MACPORTS_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)
# execute_process(COMMAND which conda OUTPUT_VARIABLE CONDA_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)

# # Error if both Homebrew and MacPorts exist
# if(HOMEBREW_EXISTS AND MACPORTS_EXISTS)
# message(FATAL_ERROR
# "Both Homebrew and MacPorts detected. This can cause conflicts.\n"
# "Please use only one package manager:\n"
# " - Homebrew found at: ${HOMEBREW_EXISTS}\n"
# " - MacPorts found at: ${MACPORTS_EXISTS}\n"
# "Consider uninstalling one to avoid library conflicts."
# )
# endif()

# # Set RPATH based on which package manager is found
# set(BASE_RPATH "@executable_path/../lib;@loader_path")

# if(HOMEBREW_EXISTS)
# message(STATUS "Using Homebrew package manager")
# # Check if Apple Silicon or Intel
# execute_process(
# COMMAND uname -m
# OUTPUT_VARIABLE ARCH
# OUTPUT_STRIP_TRAILING_WHITESPACE
# )
# if(ARCH STREQUAL "arm64")
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/opt/homebrew/lib")
# list(APPEND CMAKE_PREFIX_PATH "/opt/homebrew")
# else()
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/usr/local/lib")
# list(APPEND CMAKE_PREFIX_PATH "/usr/local")
# endif()
# elseif(MACPORTS_EXISTS)
# message(STATUS "Using MacPorts package manager")
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/opt/local/lib")
# list(APPEND CMAKE_PREFIX_PATH "/opt/local")
# elseif(CONDA_EXISTS)
# message(STATUS "Using Conda package manager")
# # Get conda prefix
# execute_process(
# COMMAND conda info --base
# OUTPUT_VARIABLE CONDA_PREFIX
# OUTPUT_STRIP_TRAILING_WHITESPACE
# )
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};${CONDA_PREFIX}/lib")
# list(APPEND CMAKE_PREFIX_PATH "${CONDA_PREFIX}")
# else()
# message(STATUS "No package manager detected, using default paths")
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/usr/local/lib")
# endif()

# set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
# message(STATUS "RPATH set to: ${CMAKE_INSTALL_RPATH}")
# endif()

add_library(yaml_c_wrapper SHARED src/yaml_c_wrapper.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)
add_executable(example_read_write examples/example_read_write.cpp)
target_link_libraries(example_read_write yaml_c_wrapper yaml-cpp)

add_executable(get_lattices src/get_lattices.cpp)
target_link_libraries(get_lattices yaml_c_wrapper yaml-cpp)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ include
inherits
repeat

YAML::Nodes are values that act like pointers, so editing a node will cause the tree the node is
contained in to reflect the changes.

## Usage
In pals-cpp, run

Expand Down
57 changes: 57 additions & 0 deletions examples/example_read_write.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include "../src/yaml_c_wrapper.h"
#include <yaml-cpp/yaml.h>

int main(int argc, char* argv[]) {
// reading a lattice from a yaml file
YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml");
std::cout << "Output of example_read_write.cpp" << std::endl;
// printing to terminal
std::cout << yaml_to_string(handle) << std::endl << std::endl;

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

// accessing sequence
YAMLNodeHandle node = 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(get_key(node, "thingB")) << "\n";

// creating a new node that's a map
YAMLNodeHandle map = create_map();
set_value_int(map, "apples", 5);

// creating a new node that's a sequence
YAMLNodeHandle sequence = create_sequence();
push_string(sequence, "magnet1");
push_string(sequence, "");
YAMLNodeHandle scalar = create_scalar();
set_scalar_string(scalar, "magnet2");
set_at_index(sequence, 1, scalar);
// give sequence a name by putting it in a map:
YAMLNodeHandle magnets = create_map();
set_value_node(magnets, "magnets", sequence);

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

// getting expanded lattice
struct lattices lat = get_lattices("ex.pals.yaml", "lat1");
YAMLNodeHandle expanded = lat.expanded;

// writing modified lattice file to expand.pals.yaml
write_file(handle, "../lattice_files/expand.pals.yaml");
return 0;
}
12 changes: 11 additions & 1 deletion lattice_files/ex.pals.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
- thingB:
kind: Sextupole
- lat1:
kind: Lattice
branches:
- inj_line
- lat2:
kind: Lattice
branches:
- inj_line
- inj_line:
kind: BeamLine
multipass: true
Expand All @@ -20,4 +28,6 @@
line:
- a
- b
- c
- c
- use: "lat2"
- use: "lat1"
3 changes: 2 additions & 1 deletion lattice_files/include.pals.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- line:
- first: 1
- second: 2
- third: 3
- third: 3
- include: "include2.pals.yaml"
4 changes: 4 additions & 0 deletions lattice_files/include2.pals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- line:
- fourth: 4
- fifth: 5
- sixth: 6
30 changes: 30 additions & 0 deletions src/get_lattices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include "../src/yaml_c_wrapper.h"
#include <cstring>

int main(int argc, char* argv[]) {
std::string file_name = "ex.pals.yaml";
const char* lattice_name = "";
if (argc >= 1) {
file_name = argv[0];
}
if (argc >= 2) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-lat") == 0) {
lattice_name = argv[i+1];
}
}
}

struct lattices lat = get_lattices("ex.pals.yaml", lattice_name);
std::cout << "Printing original lattice information: " << std::endl;
std::cout << yaml_to_string(lat.original) << std::endl << "\n\n";

// put separating lines here
std::cout << "Printing included lattice information: " << std::endl;
std::cout << yaml_to_string(lat.included) << std::endl << "\n\n";

std::cout << "Printing expanded lattice information: " << std::endl;
std::cout << yaml_to_string(lat.expanded) << std::endl;
return 0;
}
Loading