From cf79e6d2d86645ec5cc4510fed88b5ec2be2adb7 Mon Sep 17 00:00:00 2001
From: Davide Mor <davide.mor@skywarder.eu>
Date: Thu, 9 May 2024 17:32:21 +0200
Subject: [PATCH] [Registry] Added File backed backend

---
 cmake/boardcore.cmake                         |  1 +
 .../utils/Registry/Backend/FileBackend.cpp    | 58 +++++++++++++++++++
 .../utils/Registry/Backend/FileBackend.h      | 45 ++++++++++++++
 3 files changed, 104 insertions(+)
 create mode 100644 src/shared/utils/Registry/Backend/FileBackend.cpp
 create mode 100644 src/shared/utils/Registry/Backend/FileBackend.h

diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake
index fb1d8df05..f36cdf935 100644
--- a/cmake/boardcore.cmake
+++ b/cmake/boardcore.cmake
@@ -135,6 +135,7 @@ set(BOARDCORE_SRC
     ${BOARDCORE_PATH}/src/shared/utils/TestUtils/TestHelper.cpp
     ${BOARDCORE_PATH}/src/shared/utils/Registry/RegistryFrontend.cpp
     ${BOARDCORE_PATH}/src/shared/utils/Registry/RegistrySerializer.cpp
+    ${BOARDCORE_PATH}/src/shared/utils/Registry/Backend/FileBackend.cpp
 )
 
 # Creates the Skyward::Boardcore::${BOARD_NAME} library
diff --git a/src/shared/utils/Registry/Backend/FileBackend.cpp b/src/shared/utils/Registry/Backend/FileBackend.cpp
new file mode 100644
index 000000000..1df854656
--- /dev/null
+++ b/src/shared/utils/Registry/Backend/FileBackend.cpp
@@ -0,0 +1,58 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Davide Mor
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "FileBackend.h"
+
+#include <fstream>
+#include <vector>
+
+namespace Boardcore
+{
+
+bool FileBackend::start() { return true; }
+
+bool FileBackend::load(std::vector<uint8_t>& buf)
+{
+    std::ifstream is(path, std::ifstream::ate | std::ifstream::binary);
+    if (!is.good())
+        return false;
+
+    size_t size = is.tellg();
+    is.seekg(0);
+
+    buf.resize(size);
+    is.read(reinterpret_cast<char*>(buf.data()), size);
+
+    return is.good();
+}
+
+bool FileBackend::save(std::vector<uint8_t>& buf)
+{
+    std::ofstream os(path, std::ifstream::binary);
+    if (!os.good())
+        return false;
+
+    os.write(reinterpret_cast<char*>(buf.data()), buf.size());
+    return os.good();
+}
+
+}  // namespace Boardcore
diff --git a/src/shared/utils/Registry/Backend/FileBackend.h b/src/shared/utils/Registry/Backend/FileBackend.h
new file mode 100644
index 000000000..71d39f338
--- /dev/null
+++ b/src/shared/utils/Registry/Backend/FileBackend.h
@@ -0,0 +1,45 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Davide Mor
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <utils/Registry/RegistryBackend.h>
+
+#include <string>
+
+namespace Boardcore
+{
+
+class FileBackend : public RegistryBackend
+{
+public:
+    explicit FileBackend(std::string path) : path{std::move(path)} {}
+
+    [[nodiscard]] bool start() override;
+    bool load(std::vector<uint8_t>& buf) override;
+    bool save(std::vector<uint8_t>& buf) override;
+
+private:
+    std::string path;
+};
+
+}  // namespace Boardcore
\ No newline at end of file
-- 
GitLab