Line data Source code
1 : #include "presets/preset_components.h"
2 :
3 : #include <filesystem>
4 : #include <fstream>
5 :
6 : #include "preset_json.h"
7 : #include "preset_manager.h"
8 :
9 : namespace Amplitron {
10 :
11 60 : std::string PresetSerializer::serialize(const PresetData& preset) { return to_json_ext(preset); }
12 :
13 60 : bool PresetSerializer::deserialize(const std::string& json_str, PresetData& preset) {
14 60 : return from_json_ext(json_str, preset);
15 : }
16 :
17 60 : bool PresetStorage::save(const std::string& filepath, const std::string& data) {
18 60 : std::ofstream file(filepath);
19 60 : if (!file.is_open()) return false;
20 51 : file << data;
21 51 : file.close();
22 34 : return true;
23 60 : }
24 :
25 66 : std::string PresetStorage::load(const std::string& filepath) {
26 66 : std::ifstream file(filepath);
27 70 : if (!file.is_open()) return "";
28 80 : std::string data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
29 60 : file.close();
30 60 : return data;
31 86 : }
32 :
33 0 : std::vector<std::string> PresetStorage::list() {
34 0 : std::vector<std::string> result;
35 :
36 0 : auto append_files = [](const std::string& dir, std::vector<std::string>& res) {
37 0 : try {
38 0 : if (!dir.empty() && std::filesystem::exists(dir) &&
39 0 : std::filesystem::is_directory(dir)) {
40 0 : for (const auto& entry : std::filesystem::directory_iterator(dir)) {
41 0 : if (entry.is_regular_file() && entry.path().extension() == ".json") {
42 0 : res.push_back(entry.path().string());
43 0 : }
44 0 : }
45 0 : }
46 0 : } catch (...) {
47 : // Ignore errors
48 0 : }
49 0 : };
50 :
51 0 : std::string user_dir = PresetManager::get_presets_dir();
52 0 : append_files(user_dir, result);
53 :
54 0 : std::string sys_dir = PresetManager::get_system_presets_dir();
55 0 : if (!sys_dir.empty() && sys_dir != user_dir) {
56 0 : append_files(sys_dir, result);
57 0 : }
58 :
59 0 : return result;
60 0 : }
61 :
62 3 : bool PresetStorage::remove(const std::string& filepath) {
63 1 : try {
64 4 : if (std::filesystem::exists(filepath)) {
65 4 : return std::filesystem::remove(filepath);
66 : }
67 0 : } catch (...) {
68 : // Ignore errors
69 0 : }
70 0 : return false;
71 1 : }
72 :
73 60 : std::string PresetMigrator::migrate(const std::string& raw_json) {
74 60 : return PresetManager::apply_migrations(raw_json);
75 : }
76 :
77 : } // namespace Amplitron
|