diff --git a/TactilityCore/CMakeLists.txt b/TactilityCore/CMakeLists.txt index a55e7dab..f8c64691 100644 --- a/TactilityCore/CMakeLists.txt +++ b/TactilityCore/CMakeLists.txt @@ -9,6 +9,7 @@ if (DEFINED ENV{ESP_IDF_VERSION}) idf_component_register( SRCS ${SOURCE_FILES} INCLUDE_DIRS "Include/" + PRIV_INCLUDE_DIRS "Private/" REQUIRES mbedtls nvs_flash esp_rom esp_timer ) @@ -24,6 +25,10 @@ else() PRIVATE ${SOURCES} ) + include_directories( + PRIVATE Private/ + ) + target_include_directories(TactilityCore SYSTEM PUBLIC Include/ ) diff --git a/TactilityCore/Private/Tactility/file/ObjectFilePrivate.h b/TactilityCore/Private/Tactility/file/ObjectFilePrivate.h new file mode 100644 index 00000000..8710ed4e --- /dev/null +++ b/TactilityCore/Private/Tactility/file/ObjectFilePrivate.h @@ -0,0 +1,19 @@ +#pragma once + +namespace tt::file { + +constexpr uint32_t OBJECT_FILE_IDENTIFIER = 0x13371337; +constexpr uint32_t OBJECT_FILE_VERSION = 1; + +struct FileHeader { + uint32_t identifier = OBJECT_FILE_IDENTIFIER; + uint32_t version = OBJECT_FILE_VERSION; +}; + +struct ContentHeader { + uint32_t recordVersion = 0; + uint32_t recordSize = 0; + uint32_t recordCount = 0; +}; + +} diff --git a/TactilityCore/Source/file/ObjectFileReader.cpp b/TactilityCore/Source/file/ObjectFileReader.cpp new file mode 100644 index 00000000..94012901 --- /dev/null +++ b/TactilityCore/Source/file/ObjectFileReader.cpp @@ -0,0 +1,78 @@ +#include "Tactility/file/ObjectFile.h" +#include "Tactility/file/ObjectFilePrivate.h" + +#include +#include + +namespace tt::file { + +constexpr const char* TAG = "ObjectFileReader"; + +bool ObjectFileReader::open() { + auto opening_file = std::unique_ptr(fopen(filePath.c_str(), "r")); + if (opening_file == nullptr) { + TT_LOG_E(TAG, "Failed to open file %s", filePath.c_str()); + return false; + } + + FileHeader file_header; + if (fread(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) { + TT_LOG_E(TAG, "Failed to read file header from %s", filePath.c_str()); + return false; + } + + if (file_header.identifier != OBJECT_FILE_IDENTIFIER) { + TT_LOG_E(TAG, "Invalid file type for %s", filePath.c_str()); + return false; + } + + if (file_header.version != OBJECT_FILE_VERSION) { + TT_LOG_E(TAG, "Unknown version for %s: %lu", filePath.c_str(), file_header.identifier); + return false; + } + + ContentHeader content_header; + if (fread(&content_header, sizeof(ContentHeader), 1, opening_file.get()) != 1) { + TT_LOG_E(TAG, "Failed to read content header from %s", filePath.c_str()); + return false; + } + + if (recordSize != content_header.recordSize) { + TT_LOG_E(TAG, "Record size mismatch for %s: expected %lu, got %lu", filePath.c_str(), recordSize, content_header.recordSize); + return false; + } + + recordCount = content_header.recordCount; + recordVersion = content_header.recordVersion; + + file = std::move(opening_file); + + TT_LOG_D(TAG, "File version: %lu", file_header.version); + TT_LOG_D(TAG, "Content: version = %lu, size = %lu bytes, count = %lu", content_header.recordVersion, content_header.recordSize, content_header.recordCount); + + return true; +} + +void ObjectFileReader::close() { + recordCount = 0; + recordVersion = 0; + recordsRead = 0; + + file = nullptr; +} + +bool ObjectFileReader::readNext(void* output) { + if (file == nullptr) { + TT_LOG_E(TAG, "File not open"); + return false; + } + + bool result = fread(output, recordSize, 1, file.get()) == 1; + if (result) { + recordsRead++; + } + + return result; +} + +} \ No newline at end of file diff --git a/TactilityCore/Source/file/ObjectFile.cpp b/TactilityCore/Source/file/ObjectFileWriter.cpp similarity index 56% rename from TactilityCore/Source/file/ObjectFile.cpp rename to TactilityCore/Source/file/ObjectFileWriter.cpp index 9ee024da..293ff350 100644 --- a/TactilityCore/Source/file/ObjectFile.cpp +++ b/TactilityCore/Source/file/ObjectFileWriter.cpp @@ -1,107 +1,25 @@ #include "Tactility/file/ObjectFile.h" +#include "Tactility/file/ObjectFilePrivate.h" +#include #include #include namespace tt::file { -constexpr const char* TAG = "ObjectFile"; - -constexpr uint32_t OBJECT_FILE_IDENTIFIER = 0x13371337; -constexpr uint32_t OBJECT_FILE_VERSION = 1; - -struct FileHeader { - uint32_t identifier = OBJECT_FILE_IDENTIFIER; - uint32_t version = OBJECT_FILE_VERSION; -}; - -struct ContentHeader { - uint32_t recordVersion = 0; - uint32_t recordSize = 0; - uint32_t recordCount = 0; -}; - -// region Reader - -bool ObjectFileReader::open() { - auto opening_file = std::unique_ptr(fopen(filePath.c_str(), "r")); - if (opening_file == nullptr) { - TT_LOG_E(TAG, "Failed to open file %s", filePath.c_str()); - return false; - } - - FileHeader file_header; - if (fread(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) { - TT_LOG_E(TAG, "Failed to read file header from %s", filePath.c_str()); - return false; - } - - if (file_header.identifier != OBJECT_FILE_IDENTIFIER) { - TT_LOG_E(TAG, "Invalid file type for %s", filePath.c_str()); - return false; - } - - if (file_header.version != OBJECT_FILE_VERSION) { - TT_LOG_E(TAG, "Unknown version for %s: %lu", filePath.c_str(), file_header.identifier); - return false; - } - - ContentHeader content_header; - if (fread(&content_header, sizeof(ContentHeader), 1, opening_file.get()) != 1) { - TT_LOG_E(TAG, "Failed to read content header from %s", filePath.c_str()); - return false; - } - - if (recordSize != content_header.recordSize) { - TT_LOG_E(TAG, "Record size mismatch for %s: expected %lu, got %lu", filePath.c_str(), recordSize, content_header.recordSize); - return false; - } - - recordCount = content_header.recordCount; - recordVersion = content_header.recordVersion; - - file = std::move(opening_file); - - TT_LOG_D(TAG, "File version: %lu", file_header.version); - TT_LOG_D(TAG, "Content: version = %lu, size = %lu bytes, count = %lu", content_header.recordVersion, content_header.recordSize, content_header.recordCount); - - return true; -} - -void ObjectFileReader::close() { - recordCount = 0; - recordVersion = 0; - recordsRead = 0; - - file = nullptr; -} - -bool ObjectFileReader::readNext(void* output) { - if (file == nullptr) { - TT_LOG_E(TAG, "File not open"); - return false; - } - - bool result = fread(output, recordSize, 1, file.get()) == 1; - if (result) { - recordsRead++; - } - - return result; -} - -// endregion Reader - -// region Writer +constexpr const char* TAG = "ObjectFileWriter"; bool ObjectFileWriter::open() { - // If file exists bool edit_existing = append && access(filePath.c_str(), F_OK) == 0; + if (append && !edit_existing) { + TT_LOG_W(TAG, "access() to %s failed: %s", filePath.c_str(), strerror(errno)); + } + // Edit existing or create a new file auto* mode = edit_existing ? "r+" : "w"; - auto opening_file = std::unique_ptr(fopen(filePath.c_str(), mode)); + auto opening_file = std::unique_ptr(std::fopen(filePath.c_str(), mode)); if (opening_file == nullptr) { - TT_LOG_E(TAG, "Failed to open file %s", filePath.c_str()); + TT_LOG_E(TAG, "Failed to open file %s in %s mode", filePath.c_str(), mode); return false; }