|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + SettingsFileManagerClass.h |
| 5 | + Created: 29 Aug 2024 10:46:18pm |
| 6 | + Author: anders |
| 7 | +
|
| 8 | + We need to be able to save settings. This class is meant to handle this. |
| 9 | + The idea is to keep the file open consistently, to avoid having to open/close |
| 10 | + at every write - this will optimize, since we are aiming to write changes |
| 11 | + to settings as they happen. |
| 12 | + TODO: Maybe the class should occationally write to a backup file? |
| 13 | +
|
| 14 | + ============================================================================== |
| 15 | +*/ |
| 16 | + |
| 17 | +THIS IS AIMING AT HANDLING ALL SETTINGS, WRITING ALL EVERY TIME. |
| 18 | +INEFFECIENT. |
| 19 | +LETS MAKE IT SO WE CAN EFFECIENTLY CHANGE A SINGLE SETTING. |
| 20 | +BUT HOW OFTEN DO WE WRITE? |
| 21 | +DO WE REALLY WANT TO WRITE ON EVERYTHING, OR INSTEAD ON MAIN CHANGES? |
| 22 | +NO - THERE IS A SAVE OPTION - SO WE ONLY SAVE TO FILE WHEN THAT IT USED. |
| 23 | +SO WE MUST KEEP A "STANDARD SETTINGS" FILE.... |
| 24 | + |
| 25 | +from manual: |
| 26 | +New Session (Ctrl+N) |
| 27 | +• Press Ctrl+N or click the File menu, drag down to |
| 28 | +New Session, and release. |
| 29 | +• Select this to—wait, let me guess—start a new |
| 30 | +session. |
| 31 | +• The OS will ask you to save changes to the |
| 32 | +current session, if necessary. |
| 33 | +• Next, you get the chance to name the new session, |
| 34 | +select which folder it ends up in, create a new |
| 35 | +folder to put it in, or cancel. |
| 36 | +• The session can be saved to the internal hard |
| 37 | +drive or to a 3.5" floppy disk. |
| 38 | +• A session consists of all snapshots, automation |
| 39 | +data, and—if you choose—custom EQ, effects, |
| 40 | +and dynamics patches. |
| 41 | +• A new session uses the Template as its initial |
| 42 | +status" |
| 43 | + |
| 44 | + |
| 45 | +autosave feature..... |
| 46 | + |
| 47 | +a new session is using a template, but we can also save as template to update the ... tadaaa... template. |
| 48 | + |
| 49 | +the following are stored in the Template: |
| 50 | +• Channel Name |
| 51 | +• Noise Filter setting (in Channel List) |
| 52 | +• Channel Layout (rearrangement) |
| 53 | +• MDS Network settings |
| 54 | +• Stereo/Surround setting |
| 55 | +• Current Tempo (default to 120 bpm) |
| 56 | +• Time View (SMPTE/BBT/Milliseconds) |
| 57 | +• Mix Editor – Grid Setting (resolution—called |
| 58 | +Snap Grid in the pull-down), Enable Snapping, |
| 59 | +Snap to Cues, Snap to Grid |
| 60 | + |
| 61 | +#pragma once |
| 62 | + |
| 63 | +#include <cstdio> |
| 64 | +#include <mutex> |
| 65 | + |
| 66 | +class SettingsFileManager |
| 67 | +{ |
| 68 | + private: |
| 69 | + FILE *file; |
| 70 | + std::mutex fileMutex; |
| 71 | + const char *filename; |
| 72 | + |
| 73 | + public: |
| 74 | + SettingsFileManager(const char *filename) : filename(filename), file(nullptr) |
| 75 | + { |
| 76 | + openFile(); |
| 77 | + } |
| 78 | + |
| 79 | + ~SettingsFileManager() |
| 80 | + { |
| 81 | + closeFile(); |
| 82 | + } |
| 83 | + |
| 84 | + void openFile() |
| 85 | + { |
| 86 | + std::lock_guard<std::mutex> lock(fileMutex); |
| 87 | + if (!file) |
| 88 | + { |
| 89 | + file = fopen(filename, "r+b"); // Open for reading and writing in binary mode |
| 90 | + if (!file) |
| 91 | + { // File does not exist, create it |
| 92 | + file = fopen(filename, "w+b"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + void closeFile() |
| 98 | + { |
| 99 | + std::lock_guard<std::mutex> lock(fileMutex); |
| 100 | + if (file) |
| 101 | + { |
| 102 | + fclose(file); |
| 103 | + file = nullptr; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + bool writeSettings(const void *settings, size_t size) |
| 108 | + { |
| 109 | + std::lock_guard<std::mutex> lock(fileMutex); |
| 110 | + if (file && fseek(file, 0, SEEK_SET) == 0) |
| 111 | + { |
| 112 | + size_t written = fwrite(settings, size, 1, file); |
| 113 | + fflush(file); // Flush the file buffer to ensure data is written to disk |
| 114 | + return written == 1; |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + bool readSettings(void *settings, size_t size) |
| 120 | + { |
| 121 | + std::lock_guard<std::mutex> lock(fileMutex); |
| 122 | + if (file && fseek(file, 0, SEEK_SET) == 0) |
| 123 | + { |
| 124 | + size_t read = fread(settings, size, 1, file); |
| 125 | + return read == 1; |
| 126 | + } |
| 127 | + return false; |
| 128 | + } |
| 129 | +}; |
0 commit comments