|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 HR Agrartechnik GmbH |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * http://www.eclipse.org/legal/epl-2.0. |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Franz Höpfinger |
| 11 | + * - initial API and implementation and/or initial documentation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +#include "forte/arch/forte_fileio.h" |
| 15 | +#include "forte/arch/forte_ramdisk.h" |
| 16 | +#include <cstdio> |
| 17 | +#include <cstring> |
| 18 | +#include <fstream> |
| 19 | +#include <map> |
| 20 | +#include <mutex> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +struct RamdiskFileInfo { |
| 25 | + std::string filename; |
| 26 | + char mode; |
| 27 | +}; |
| 28 | + |
| 29 | +static std::map<std::string, std::vector<char>> g_ramdiskEntries; |
| 30 | +static std::map<void *, RamdiskFileInfo> g_ramdiskOpenFiles; |
| 31 | +static std::mutex g_ramdiskMutex; |
| 32 | + |
| 33 | +extern "C" { |
| 34 | + |
| 35 | +char *forte_getenv(const char *env_var) { |
| 36 | + return getenv(env_var); |
| 37 | +} |
| 38 | + |
| 39 | +size_t forte_strnlen_s(const char *str, size_t strsz) { |
| 40 | + if (nullptr == str) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + return strnlen(str, strsz); |
| 44 | +} |
| 45 | + |
| 46 | +int forte_ramdisk_load(const char *filename) { |
| 47 | + if (filename == nullptr) { |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + std::ifstream file(filename, std::ios::binary | std::ios::ate); |
| 52 | + if (!file) { |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + std::streamsize size = file.tellg(); |
| 57 | + if (size < 0) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + file.seekg(0, std::ios::beg); |
| 62 | + |
| 63 | + std::vector<char> buffer(static_cast<size_t>(size)); |
| 64 | + if (!file.read(buffer.data(), size)) { |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + std::lock_guard<std::mutex> lock(g_ramdiskMutex); |
| 69 | + g_ramdiskEntries[filename] = std::move(buffer); |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +void forte_ramdisk_unload(const char *filename) { |
| 74 | + if (filename == nullptr) { |
| 75 | + return; |
| 76 | + } |
| 77 | + std::lock_guard<std::mutex> lock(g_ramdiskMutex); |
| 78 | + g_ramdiskEntries.erase(filename); |
| 79 | +} |
| 80 | + |
| 81 | +void *forte_fopen(const char *filename, const char *mode) { |
| 82 | + if (filename != nullptr && mode != nullptr) { |
| 83 | + std::lock_guard<std::mutex> lock(g_ramdiskMutex); |
| 84 | + auto it = g_ramdiskEntries.find(filename); |
| 85 | + if (it != g_ramdiskEntries.end()) { |
| 86 | + // Only use RAMDISK for read modes; write modes fallback to real file |
| 87 | + if (mode[0] == 'r') { |
| 88 | + FILE *file = fmemopen(it->second.data(), it->second.size(), mode); |
| 89 | + if (file != nullptr) { |
| 90 | + g_ramdiskOpenFiles[file] = {filename, mode[0]}; |
| 91 | + } |
| 92 | + return file; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return fopen(filename, mode); |
| 97 | +} |
| 98 | + |
| 99 | +int forte_fclose(void *file) { |
| 100 | + FILE *f = static_cast<FILE *>(file); |
| 101 | + { |
| 102 | + std::lock_guard<std::mutex> lock(g_ramdiskMutex); |
| 103 | + auto it = g_ramdiskOpenFiles.find(f); |
| 104 | + if (it != g_ramdiskOpenFiles.end()) { |
| 105 | + if (it->second.mode == 'r') { |
| 106 | + // Remove from RAMDISK; vector memory freed automatically by RAII |
| 107 | + g_ramdiskEntries.erase(it->second.filename); |
| 108 | + } |
| 109 | + g_ramdiskOpenFiles.erase(it); |
| 110 | + } |
| 111 | + } |
| 112 | + return fclose(f); |
| 113 | +} |
| 114 | + |
| 115 | +char *forte_fgets(char *str, int count, void *file) { |
| 116 | + return fgets(str, count, static_cast<FILE *>(file)); |
| 117 | +} |
| 118 | + |
| 119 | +int forte_fseek(void *file, long offset, int whence) { |
| 120 | + return fseek(static_cast<FILE *>(file), offset, whence); |
| 121 | +} |
| 122 | + |
| 123 | +long forte_ftell(void *file) { |
| 124 | + return ftell(static_cast<FILE *>(file)); |
| 125 | +} |
| 126 | + |
| 127 | +int forte_feof(void *file) { |
| 128 | + return feof(static_cast<FILE *>(file)); |
| 129 | +} |
| 130 | + |
| 131 | +size_t forte_fread(void *ptr, size_t itemsize, size_t nitems, void *file) { |
| 132 | + return fread(ptr, itemsize, nitems, static_cast<FILE *>(file)); |
| 133 | +} |
| 134 | + |
| 135 | +size_t forte_fwrite(const void *ptr, size_t itemsize, size_t nitems, void *file) { |
| 136 | + return fwrite(ptr, itemsize, nitems, static_cast<FILE *>(file)); |
| 137 | +} |
| 138 | + |
| 139 | +} // extern "C" |
0 commit comments