use of C++23 code
Signed-off-by: Peter Siegmund <mars3142@noreply.mars3142.dev>
This commit is contained in:
26
components/storage/CMakeLists.txt
Normal file
26
components/storage/CMakeLists.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
set(STORAGE_VERSION_MAJOR 1)
|
||||
set(STORAGE_VERSION_MINOR 0)
|
||||
set(STORAGE_VERSION_PATCH 0)
|
||||
set(STORAGE_VERSION "${STORAGE_VERSION_MAJOR}.${STORAGE_VERSION_MINOR}.${STORAGE_VERSION_PATCH}")
|
||||
|
||||
configure_file(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/version.h.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/include/storage/version.h
|
||||
@ONLY
|
||||
)
|
||||
|
||||
set(STORAGE_SRC
|
||||
src/storage.cpp
|
||||
)
|
||||
|
||||
add_library(storage SHARED ${STORAGE_SRC})
|
||||
set_target_properties(storage PROPERTIES
|
||||
VERSION ${STORAGE_VERSION}
|
||||
SOVERSION ${STORAGE_VERSION_MAJOR}
|
||||
)
|
||||
|
||||
target_include_directories(storage
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${CMAKE_CURRENT_BINARY_DIR}/include
|
||||
)
|
||||
50
components/storage/include/storage/storage.h
Normal file
50
components/storage/include/storage/storage.h
Normal file
@@ -0,0 +1,50 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// 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 "storage/storage_error.h"
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace storage {
|
||||
|
||||
class Storage {
|
||||
public:
|
||||
/**
|
||||
* Liest eine Datei von einem Pfad.
|
||||
* @param path Der Dateipfad.
|
||||
* @return Ein expected mit den Dateidaten oder einem StorageError.
|
||||
*/
|
||||
[[nodiscard]] std::expected<std::vector<uint8_t>, StorageError> readFile(
|
||||
const std::string &path) const;
|
||||
|
||||
/**
|
||||
* Schreibt Daten in eine Datei.
|
||||
* @param path Der Dateipfad.
|
||||
* @param data Die zu schreibenden Daten.
|
||||
* @return Ein expected<void> oder einem StorageError.
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, StorageError> writeFile(
|
||||
const std::string &path,
|
||||
const std::vector<uint8_t> &data) const;
|
||||
};
|
||||
|
||||
} // namespace storage
|
||||
29
components/storage/include/storage/storage_error.h
Normal file
29
components/storage/include/storage/storage_error.h
Normal file
@@ -0,0 +1,29 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// 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
|
||||
|
||||
namespace storage {
|
||||
|
||||
enum class StorageError {
|
||||
FileNotFound,
|
||||
ReadError,
|
||||
WriteError,
|
||||
};
|
||||
|
||||
} // namespace storage
|
||||
59
components/storage/src/storage.cpp
Normal file
59
components/storage/src/storage.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// 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 "storage/storage.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace storage {
|
||||
|
||||
std::expected<std::vector<uint8_t>, StorageError> Storage::readFile(
|
||||
const std::string &path) const {
|
||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
return std::unexpected(StorageError::FileNotFound);
|
||||
}
|
||||
|
||||
const std::streamsize size = file.tellg();
|
||||
if (size < 0) {
|
||||
return std::unexpected(StorageError::ReadError);
|
||||
}
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> data(static_cast<size_t>(size));
|
||||
if (!file.read(reinterpret_cast<char *>(data.data()), size)) {
|
||||
return std::unexpected(StorageError::ReadError);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
std::expected<void, StorageError> Storage::writeFile(
|
||||
const std::string &path,
|
||||
const std::vector<uint8_t> &data) const {
|
||||
std::ofstream file(path, std::ios::binary);
|
||||
if (!file) {
|
||||
return std::unexpected(StorageError::WriteError);
|
||||
}
|
||||
file.write(reinterpret_cast<const char *>(data.data()), data.size());
|
||||
if (!file.good()) {
|
||||
return std::unexpected(StorageError::WriteError);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace storage
|
||||
7
components/storage/version.h.in
Normal file
7
components/storage/version.h.in
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define STORAGE_VERSION_MAJOR @STORAGE_VERSION_MAJOR@
|
||||
#define STORAGE_VERSION_MINOR @STORAGE_VERSION_MINOR@
|
||||
#define STORAGE_VERSION_PATCH @STORAGE_VERSION_PATCH@
|
||||
#define STORAGE_VERSION "@STORAGE_VERSION@"
|
||||
|
||||
Reference in New Issue
Block a user