Skip to content

Commit

Permalink
[Utility]: Added an IOP file reader
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ad3154 committed Oct 6, 2022
1 parent a192599 commit 4decb30
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions utility/include/iop_file_interface.hpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <cstdint>
#include <string>

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<std::uint8_t> read_iop_file(const std::string &filename);
};
}

#endif // IOP_FILE_INTERFACE_HPP
39 changes: 39 additions & 0 deletions utility/src/iop_file_interface.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <iterator>

namespace isobus
{
std::vector<std::uint8_t> IOPFileInterface::read_iop_file(const std::string &filename)
{
std::vector<std::uint8_t> 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<std::uint8_t>(file), std::istream_iterator<std::uint8_t>());
}
return retVal;
}
}

0 comments on commit 4decb30

Please sign in to comment.