From 4decb301ad1e0e413f6cdac069a2fe406b517f6a Mon Sep 17 00:00:00 2001 From: Adrian Del Grosso <10929341+ad3154@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:10:54 -0600 Subject: [PATCH] [Utility]: Added an IOP file reader Added a simple way to read IOP files to assist with testing. Feel free to use this in applications that consume this library to easily read an IOP file, but it may not be ideal for an embedded platform with very limted RAM, as it will store the entire IOP file content in RAM. If you need to read an object pool in segments due to RAM restrictions, you should instead use CAN data chunk callbacks in concert with your own custom driver for getting pool data. If there is demand for it, I may add an interface for that in the future. --- utility/CMakeLists.txt | 2 ++ utility/include/iop_file_interface.hpp | 36 ++++++++++++++++++++++++ utility/src/iop_file_interface.cpp | 39 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 utility/include/iop_file_interface.hpp create mode 100644 utility/src/iop_file_interface.cpp diff --git a/utility/CMakeLists.txt b/utility/CMakeLists.txt index 3c55fa10..d7181958 100644 --- a/utility/CMakeLists.txt +++ b/utility/CMakeLists.txt @@ -11,6 +11,7 @@ set(SYSTEM_TIMING_INCLUDE_DIR "include") set(SYSTEM_TIMING_SRC "system_timing.cpp" "processing_flags.cpp" + "iop_file_interface.cpp" ) # Prepend the source directory path to all the source files @@ -20,6 +21,7 @@ PREPEND(SYSTEM_TIMING_SRC ${SYSTEM_TIMING_SRC_DIR} ${SYSTEM_TIMING_SRC}) set(SYSTEM_TIMING_INCLUDE "system_timing.hpp" "processing_flags.hpp" + "iop_file_interface.hpp" ) # Prepend the include directory path to all the include files diff --git a/utility/include/iop_file_interface.hpp b/utility/include/iop_file_interface.hpp new file mode 100644 index 00000000..fd3586cf --- /dev/null +++ b/utility/include/iop_file_interface.hpp @@ -0,0 +1,36 @@ +//================================================================================================ +/// @file iop_file_interface.hpp +/// +/// @brief A class that manages reading IOP files +/// @author Adrian Del Grosso +/// +/// @copyright 2022 Adrian Del Grosso +//================================================================================================ + +#ifndef IOP_FILE_INTERFACE_HPP +#define IOP_FILE_INTERFACE_HPP + +#include +#include +#include + +namespace isobus +{ + //================================================================================================ + /// @class IOPFileInterface + /// + /// @brief A class that manages reading IOP files + /// @details IOP files are the standard format for ISOBUS object pools. This class will read + /// an IOP file, and return a vector that represents the object pool stored in the file. + //================================================================================================ + class IOPFileInterface + { + public: + /// @brief Reads an IOP file given a file name/path + /// @param[in] filename A string filepath for the IOP file to read + /// @returns A vector with an object pool in it, or an empty vector if reading failed + static std::vector read_iop_file(const std::string &filename); + }; +} + +#endif // IOP_FILE_INTERFACE_HPP diff --git a/utility/src/iop_file_interface.cpp b/utility/src/iop_file_interface.cpp new file mode 100644 index 00000000..43504ee0 --- /dev/null +++ b/utility/src/iop_file_interface.cpp @@ -0,0 +1,39 @@ +//================================================================================================ +/// @file iop_file_interface.cpp +/// +/// @brief Implementation of class that manages reading IOP files +/// @author Adrian Del Grosso +/// +/// @copyright 2022 Adrian Del Grosso +//================================================================================================ +#include "iop_file_interface.hpp" + +#include +#include + +namespace isobus +{ + std::vector IOPFileInterface::read_iop_file(const std::string &filename) + { + std::vector retVal; + std::streampos fileSize; + + std::ifstream file(filename, std::ios::binary); + + if (file.is_open()) + { + // Ignore newlines + file.unsetf(std::ios::skipws); + + file.seekg(0, std::ios::end); + fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + retVal.reserve(fileSize); + + // Read in the data + retVal.insert(retVal.begin(), std::istream_iterator(file), std::istream_iterator()); + } + return retVal; + } +}