Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Set up sample CMake and yaml file parsing (#12)
Browse files Browse the repository at this point in the history
* 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
samdai01 authored Nov 25, 2023
1 parent 9fe5cea commit 2fa9709
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
diagnostics
15 changes: 15 additions & 0 deletions CMakeLists.txt
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)
14 changes: 14 additions & 0 deletions build.sh
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 .
4 changes: 4 additions & 0 deletions config/CMakeLists.txt
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"
)
11 changes: 11 additions & 0 deletions config/yaml/sampleTest.yaml
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
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
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)
17 changes: 17 additions & 0 deletions src/main/CMakeLists.txt
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}"
)
34 changes: 34 additions & 0 deletions src/main/main.cpp
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;
}
19 changes: 19 additions & 0 deletions src/yaml/CMakeLists.txt
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}
)
29 changes: 29 additions & 0 deletions src/yaml/parse_yaml.cpp
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();
}
10 changes: 10 additions & 0 deletions src/yaml/parse_yaml.h
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

0 comments on commit 2fa9709

Please sign in to comment.