diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-23 17:06:02 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-23 17:06:02 +0200 |
| commit | ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21 (patch) | |
| tree | 3946a99938c10b61f7822c715771c3529400cebc /web/storage.cpp | |
| parent | c6a77f30f64be73a5e55e06336975f2ecfbb2324 (diff) | |
| download | nissy-core-ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21.tar.gz nissy-core-ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21.zip | |
Web version (work in progress)
Diffstat (limited to 'web/storage.cpp')
| -rw-r--r-- | web/storage.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/web/storage.cpp b/web/storage.cpp new file mode 100644 index 0000000..4be3066 --- /dev/null +++ b/web/storage.cpp | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #include "storage.h" | ||
| 2 | |||
| 3 | #include "emscripten.h" | ||
| 4 | #include <filesystem> | ||
| 5 | #include <fstream> | ||
| 6 | |||
| 7 | EM_JS(int, inbrowser, (), { return typeof window !== 'undefined'; }); | ||
| 8 | EM_JS(int, inworker, (), { return typeof WorkerGlobalScope !== 'undefined' && | ||
| 9 | self instanceof WorkerGlobalScope; }); | ||
| 10 | |||
| 11 | std::string getprefix() { | ||
| 12 | return inbrowser() || inworker() ? "/tables/" : "./tables/"; | ||
| 13 | } | ||
| 14 | |||
| 15 | EM_ASYNC_JS(int, loadfs, (), { | ||
| 16 | const dir = '/tables'; | ||
| 17 | const inBrowser = typeof window !== 'undefined'; | ||
| 18 | const inWorker = typeof WorkerGlobalScope !== 'undefined' && | ||
| 19 | self instanceof WorkerGlobalScope; | ||
| 20 | |||
| 21 | if (!(inBrowser || inWorker)) return; | ||
| 22 | |||
| 23 | if (!FS.analyzePath(dir).exists) | ||
| 24 | FS.mkdir(dir); | ||
| 25 | |||
| 26 | if (FS.analyzePath(dir).object.mount.mountpoint != dir) { | ||
| 27 | FS.mount(IDBFS, { autoPersist: true }, dir); | ||
| 28 | |||
| 29 | await new Promise((resolve, reject) => { | ||
| 30 | FS.syncfs(true, function (err) { | ||
| 31 | if (err) { | ||
| 32 | reject(err); | ||
| 33 | } else { | ||
| 34 | resolve(true); | ||
| 35 | } | ||
| 36 | }); | ||
| 37 | }); | ||
| 38 | } | ||
| 39 | }); | ||
| 40 | |||
| 41 | bool storage::read(std::string key, size_t data_size, char *data) | ||
| 42 | { | ||
| 43 | loadfs(); | ||
| 44 | |||
| 45 | std::filesystem::path path(getprefix() + key); | ||
| 46 | if (!std::filesystem::exists(path)) | ||
| 47 | return false; | ||
| 48 | |||
| 49 | std::ifstream ifs(path, std::ios::binary); | ||
| 50 | ifs.read(data, data_size); | ||
| 51 | ifs.close(); | ||
| 52 | |||
| 53 | return !ifs.fail(); | ||
| 54 | } | ||
| 55 | |||
| 56 | bool storage::write(std::string key, size_t data_size, const char *data) | ||
| 57 | { | ||
| 58 | loadfs(); | ||
| 59 | |||
| 60 | std::filesystem::path path(getprefix() + key); | ||
| 61 | |||
| 62 | std::ofstream ofs(path, std::ios::binary); | ||
| 63 | ofs.write(data, data_size); | ||
| 64 | ofs.close(); | ||
| 65 | |||
| 66 | return !ofs.fail(); | ||
| 67 | } | ||
