#define _POSIX_C_SOURCE 200809L #include "config.h" #include #include #ifdef _WIN32 # include # define MAKE_DIR(p) _mkdir(p) #else # include # define MAKE_DIR(p) mkdir(p, 0755) #endif void configSanitizeName(const char *in, char *out, int maxLen) { int i = 0; for (; in[i] && i < maxLen - 1; i++) { char c = in[i]; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-') out[i] = c; else out[i] = '_'; } out[i] = '\0'; } int configSaveLastDevice(const char *deviceName) { MAKE_DIR(CONFIG_DIR); FILE *f = fopen(CONFIG_DIR "/last_device.txt", "w"); if (!f) return 0; fprintf(f, "%s\n", deviceName); fclose(f); return 1; } int configLoadLastDevice(char *out, int maxLen) { FILE *f = fopen(CONFIG_DIR "/last_device.txt", "r"); if (!f) return 0; int ok = (fgets(out, maxLen, f) != NULL); fclose(f); if (!ok) return 0; int len = (int)strlen(out); if (len > 0 && out[len - 1] == '\n') out[len - 1] = '\0'; return out[0] != '\0'; } int configSaveCcMappings(const char *deviceName, ConfigCcEntry *entries, int count) { MAKE_DIR(CONFIG_DIR); char safe[CONFIG_NAME_LEN]; configSanitizeName(deviceName, safe, sizeof(safe)); char path[CONFIG_NAME_LEN + 32]; snprintf(path, sizeof(path), CONFIG_DIR "/cc_%s.cfg", safe); FILE *f = fopen(path, "w"); if (!f) return 0; for (int i = 0; i < count; i++) fprintf(f, "%d %d %f %f\n", entries[i].cc, entries[i].controlId, entries[i].min, entries[i].max); fclose(f); return 1; } int configLoadCcMappings(const char *deviceName, ConfigCcEntry *entries, int maxCount) { char safe[CONFIG_NAME_LEN]; configSanitizeName(deviceName, safe, sizeof(safe)); char path[CONFIG_NAME_LEN + 32]; snprintf(path, sizeof(path), CONFIG_DIR "/cc_%s.cfg", safe); FILE *f = fopen(path, "r"); if (!f) return 0; int count = 0; while (count < maxCount) { int cc, id; float mn, mx; if (fscanf(f, "%d %d %f %f\n", &cc, &id, &mn, &mx) != 4) break; if (cc >= 0 && cc < 128) { entries[count].cc = cc; entries[count].controlId = id; entries[count].min = mn; entries[count].max = mx; count++; } } fclose(f); return count; }