Quick JSON bool lookup for early-init config reads (before the full JSON parser is available). Returns defaultVal if the key is absent.
| 81 | // Quick JSON bool lookup for early-init config reads (before the full JSON |
| 82 | // parser is available). Returns defaultVal if the key is absent. |
| 83 | static bool ReadConfigBool(const char* key, bool defaultVal) |
| 84 | { |
| 85 | std::string configPath = XdgConfigHome() + "/CloudRedirect/config.json"; |
| 86 | FILE* f = fopen(configPath.c_str(), "r"); |
| 87 | if (!f) return defaultVal; |
| 88 | |
| 89 | char buf[65536] = {}; |
| 90 | size_t n = fread(buf, 1, sizeof(buf) - 1, f); |
| 91 | fclose(f); |
| 92 | buf[n] = '\0'; |
| 93 | |
| 94 | std::string needle = std::string("\"") + key + "\""; |
| 95 | const char* pos = strstr(buf, needle.c_str()); |
| 96 | if (!pos) return defaultVal; |
| 97 | |
| 98 | const char* p = pos + needle.size(); |
| 99 | while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ':') ++p; |
| 100 | |
| 101 | if (strncmp(p, "false", 5) == 0) return false; |
| 102 | if (strncmp(p, "true", 4) == 0) return true; |
| 103 | return defaultVal; |
| 104 | } |
| 105 | |
| 106 | static bool NotificationsEnabled() { return ReadConfigBool("notifications_enabled", true); } |
| 107 | static bool StatsSyncEnabled() { return ReadConfigBool("stats_sync_enabled", true); } |
no test coverage detected