This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up sample CMake and yaml file parsing (#12)
* Set up barebones diagnostic project with CMake * Refactored CMake implementation, imported rapidYaml, and reimplemented test yaml parsing * More changes -> Last commit part 2
- Loading branch information
Showing
11 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out/ | ||
diagnostics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Top level CMake file for entire Diagnostics app | ||
|
||
# Set CMake version and create project | ||
cmake_minimum_required(VERSION 3.22) | ||
project(diagnostics) | ||
|
||
# Set C++ Standard | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
# Import rapidYaml | ||
find_package(ryml REQUIRED) | ||
|
||
# Add source files | ||
add_subdirectory(config) | ||
add_subdirectory(src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
# Remove previously built files | ||
rm -rf out/ | ||
rm -rf diagnostics | ||
|
||
# Run CMake with source directory '.' and build directory out/build | ||
cmake -S . -B out/build | ||
|
||
# Compile and link the project with generated MakeFile from CMake | ||
make -C out/build | ||
|
||
# Copy the results to output file | ||
cp -r out/build/src/main/diagnostics . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Create constants and export it to the rest of the CMAKE project | ||
set( | ||
YAML_TEST_PATH ${CMAKE_CURRENT_LIST_DIR}/yaml/sampleTest.yaml CACHE INTERNAL "Path to test yaml config file" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
foo: | ||
bar: "a" | ||
barbar: "b" | ||
barbarbar: "c" | ||
|
||
matrix: | ||
array: | ||
- 0.5 | ||
- 0.67 | ||
- 0.78 | ||
- 0.89 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Add modules for project as required | ||
add_subdirectory(main) | ||
add_subdirectory(yaml) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add main.exe | ||
add_executable(diagnostics main.cpp) | ||
|
||
# List of required header files | ||
target_include_directories(diagnostics PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
# Modules that main depends on | ||
# ex: target_link_libraries(SailbotDiagnostics_main PRIVATE module1 module2) | ||
target_link_libraries(diagnostics PRIVATE | ||
yaml | ||
ryml::ryml | ||
) | ||
|
||
# Import configuration file | ||
target_compile_definitions( | ||
diagnostics PUBLIC YAML_TEST_PATH="${YAML_TEST_PATH}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* Include files */ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include <ryml_std.hpp> | ||
#include <ryml.hpp> | ||
#include "parse_yaml.h" | ||
|
||
/* Functions */ | ||
|
||
int main() | ||
{ | ||
std::cout << "UBC Sailbot Diagnostics App" << std::endl; | ||
std::cout << "Instructions for use can be found on https://ubcsailbot.atlassian.net/l/cp/mRm7BF1k" << std::endl; | ||
std::string contents = readFile(YAML_TEST_PATH); | ||
std::cout << contents << std::endl; | ||
std::cout << "------------------------------------" << std::endl; | ||
ryml::Tree tree = ryml::parse_in_place(ryml::to_substr(contents)); | ||
ryml::NodeRef foo = tree["foo"]; | ||
for (ryml::NodeRef const& child : foo.children()) { | ||
std::cout << "key: " << child.key() << " val: " << child.val() << std::endl; | ||
} | ||
|
||
ryml::NodeRef array = tree["matrix"]["array"]; | ||
for (ryml::NodeRef const& child : array.children()) { | ||
double val; | ||
child >> val; | ||
std::cout << "float val: " << std::setprecision (18) << val << std::endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# src/yaml/CMakeLists.txt | ||
|
||
# Collect header files | ||
set(HEADER_FILES | ||
parse_yaml.h | ||
) | ||
|
||
# Collect source files | ||
set(SOURCE_FILES | ||
parse_yaml.cpp | ||
) | ||
|
||
# Create a library from the source files | ||
add_library(yaml ${SOURCE_FILES} ${HEADER_FILES}) | ||
|
||
# Include directories | ||
target_include_directories(${PROJECT_NAME} PUBLIC | ||
${CMAKE_CURRENT_SOURCE_DIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Include files */ | ||
#include <string> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <sstream> | ||
|
||
/* Functions */ | ||
|
||
/** | ||
* @brief Parses a file and returns all its contents in a string | ||
* | ||
* @param file_path | ||
* @return std::string contains everything inside the defined file | ||
*/ | ||
std::string readFile( const char *file_path ) { | ||
// open the file as input and read as binary | ||
std::ifstream in_file( file_path, std::ios::in | std::ios::binary ); | ||
if (!in_file) { | ||
std::cerr << "PARSE_YAML: ifstream could not open file " << file_path << std::endl; | ||
throw std::ifstream::failure("ERROR: ifstream could not open file"); | ||
} | ||
|
||
// put the contents of the file into a stringstream | ||
std::stringstream file_buffer; | ||
file_buffer << in_file.rdbuf(); | ||
|
||
// return all contents of stringstream as a single string | ||
return file_buffer.str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef PARSE_YAML_H_ | ||
#define PARSE_YAML_H_ | ||
|
||
/* Include Files */ | ||
#include <string> | ||
|
||
/* Function Declarations */ | ||
std::string readFile( const char *file_path ); | ||
|
||
#endif |