mirror of
https://github.com/Goldenkrew3000/OSSP_OpenSource.git
synced 2025-12-20 00:34:44 +10:00
Adding base source
This commit is contained in:
356
src/configHandler.c
Normal file
356
src/configHandler.c
Normal file
@@ -0,0 +1,356 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include "configHandler.h"
|
||||
#include "external/cJSON.h"
|
||||
#include "libopensubsonic/logger.h"
|
||||
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#include "OSSP_Bridge.h"
|
||||
#endif // defined(__APPLE__) && defined(__MACH__)
|
||||
|
||||
static int rc = 0;
|
||||
|
||||
/*
|
||||
* Read a predefined config file into the configuration struct
|
||||
* Returns 0 on success, 1 on failure
|
||||
*/
|
||||
int configHandler_Read(configHandler_config_t** configObj) {
|
||||
// Allocate config object on heap
|
||||
*configObj = malloc(sizeof(configHandler_config_t));
|
||||
|
||||
// Initialize struct variables
|
||||
(*configObj)->opensubsonic_protocol = NULL;
|
||||
(*configObj)->opensubsonic_server = NULL;
|
||||
(*configObj)->opensubsonic_username = NULL;
|
||||
(*configObj)->opensubsonic_password = NULL;
|
||||
(*configObj)->internal_opensubsonic_version = NULL;
|
||||
(*configObj)->internal_opensubsonic_clientName = NULL;
|
||||
(*configObj)->internal_opensubsonic_loginSalt = NULL;
|
||||
(*configObj)->internal_opensubsonic_loginToken = NULL;
|
||||
(*configObj)->listenbrainz_enable = false;
|
||||
(*configObj)->listenbrainz_token = NULL;
|
||||
(*configObj)->lastfm_enable = false;
|
||||
(*configObj)->lastfm_username = NULL;
|
||||
(*configObj)->lastfm_password = NULL;
|
||||
(*configObj)->lastfm_api_key = NULL;
|
||||
(*configObj)->lastfm_api_secret = NULL;
|
||||
(*configObj)->lastfm_api_session_key = NULL;
|
||||
(*configObj)->audio_equalizer_enable = false;
|
||||
(*configObj)->audio_equalizer_followPitch = false;
|
||||
(*configObj)->audio_equalizer_graphCount = 0;
|
||||
(*configObj)->audio_equalizer_graph = NULL;
|
||||
(*configObj)->audio_pitch_enable = false;
|
||||
(*configObj)->audio_pitch_cents = 0.00;
|
||||
(*configObj)->audio_pitch_rate = 0.00;
|
||||
(*configObj)->audio_reverb_enable = false;
|
||||
(*configObj)->audio_reverb_wetDryMix = 0.00;
|
||||
|
||||
// Set internal configuration values
|
||||
(*configObj)->internal_opensubsonic_version = strdup("1.8.0");
|
||||
(*configObj)->internal_opensubsonic_clientName = strdup("Hojuix_OSSP");
|
||||
|
||||
// Form the path to the config JSON
|
||||
char* config_path = NULL;
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
// NOTE: This is a relatively hacky way of fetching the iOS container path without diving into the hell that is ObjC
|
||||
char* root_path = getenv("HOME");
|
||||
rc = asprintf(&config_path, "%s/Documents/config.json", root_path);
|
||||
#if DEBUG
|
||||
printf("iOS Container Path: %s\n", config_path);
|
||||
#endif // DEBUG
|
||||
#else
|
||||
rc = asprintf(&config_path, "config.json");
|
||||
#endif // defined(__APPLE__) && defined(__MACH__)
|
||||
if (rc == -1) {
|
||||
logger_log_error(__func__, "asprintf() failed (Could not generate config path).");
|
||||
free(config_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read config file
|
||||
FILE* config_fd = NULL;
|
||||
char* config_buf = NULL;
|
||||
long config_fsize = 0;
|
||||
long config_fread_rc = 0; // Needs to be separate from 'rc' as fread() returns bytes read
|
||||
|
||||
// Check if the config file exists, and fetch it's size
|
||||
struct stat st;
|
||||
if (stat(config_path, &st) == 0) {
|
||||
config_fsize = st.st_size;
|
||||
} else {
|
||||
logger_log_error(__func__, "stat() failed (Config file does not exist).");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Actually open and read in the contents of the config file
|
||||
config_fd = fopen(config_path, "rb");
|
||||
if (config_fd == NULL) {
|
||||
logger_log_error(__func__, "fopen() failed (Could not open config file).");
|
||||
free(config_path);
|
||||
return 1;
|
||||
}
|
||||
free(config_path);
|
||||
|
||||
config_buf = (char*)malloc(config_fsize + 1);
|
||||
if (config_buf == NULL) {
|
||||
logger_log_error(__func__, "malloc() failed (Could not allocate enough memory for the config file).");
|
||||
fclose(config_fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
config_fread_rc = fread(config_buf, 1, config_fsize, config_fd);
|
||||
if (config_fread_rc != config_fsize) {
|
||||
logger_log_error(__func__, "fread() failed (Could not read the config file).");
|
||||
fclose(config_fd);
|
||||
free(config_buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Null terminate the buffer
|
||||
config_buf[config_fsize] = '\0';
|
||||
fclose(config_fd);
|
||||
|
||||
// Parse config JSON
|
||||
cJSON* root = cJSON_Parse(config_buf);
|
||||
free(config_buf);
|
||||
if (root == NULL) {
|
||||
logger_log_error(__func__, "cJSON_Parse() failed (Could not parse the configuration JSON).");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Make an object from opensubsonic_server
|
||||
// TODO - Use the new OSS_P*oj functions?
|
||||
cJSON* opensubsonic_server_root = cJSON_GetObjectItemCaseSensitive(root, "opensubsonic_server");
|
||||
if (opensubsonic_server_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - opensubsonic_server does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* opensubsonic_server_protocol = cJSON_GetObjectItemCaseSensitive(opensubsonic_server_root, "protocol");
|
||||
if (cJSON_IsString(opensubsonic_server_protocol) && opensubsonic_server_protocol->valuestring != NULL) {
|
||||
(*configObj)->opensubsonic_protocol = strdup(opensubsonic_server_protocol->valuestring);
|
||||
}
|
||||
|
||||
cJSON* opensubsonic_server_server = cJSON_GetObjectItemCaseSensitive(opensubsonic_server_root, "server");
|
||||
if (cJSON_IsString(opensubsonic_server_server) && opensubsonic_server_server->valuestring != NULL) {
|
||||
(*configObj)->opensubsonic_server = strdup(opensubsonic_server_server->valuestring);
|
||||
}
|
||||
|
||||
cJSON* opensubsonic_server_username = cJSON_GetObjectItemCaseSensitive(opensubsonic_server_root, "username");
|
||||
if (cJSON_IsString(opensubsonic_server_username) && opensubsonic_server_username->valuestring != NULL) {
|
||||
(*configObj)->opensubsonic_username = strdup(opensubsonic_server_username->valuestring);
|
||||
}
|
||||
|
||||
cJSON* opensubsonic_server_password = cJSON_GetObjectItemCaseSensitive(opensubsonic_server_root, "password");
|
||||
if (cJSON_IsString(opensubsonic_server_password) && opensubsonic_server_password->valuestring != NULL) {
|
||||
(*configObj)->opensubsonic_password = strdup(opensubsonic_server_password->valuestring);
|
||||
}
|
||||
|
||||
// Make an object from scrobbler
|
||||
cJSON* scrobbler_root = cJSON_GetObjectItemCaseSensitive(root, "scrobbler");
|
||||
if (scrobbler_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - scrobbler does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* scrobbler_listenbrainz_enable = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "listenbrainz_enable");
|
||||
if (cJSON_IsBool(scrobbler_listenbrainz_enable)) {
|
||||
if (cJSON_IsTrue(scrobbler_listenbrainz_enable)) {
|
||||
(*configObj)->listenbrainz_enable = true;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* scrobbler_listenbrainz_token = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "listenbrainz_token");
|
||||
if (cJSON_IsString(scrobbler_listenbrainz_token) && scrobbler_listenbrainz_token->valuestring != NULL) {
|
||||
(*configObj)->listenbrainz_token = strdup(scrobbler_listenbrainz_token->valuestring);
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_enable = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_enable");
|
||||
if (cJSON_IsBool(scrobbler_lastfm_enable)) {
|
||||
if (cJSON_IsTrue(scrobbler_lastfm_enable)) {
|
||||
(*configObj)->lastfm_enable = true;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_username = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_username");
|
||||
if (cJSON_IsString(scrobbler_lastfm_username) && scrobbler_lastfm_username->valuestring != NULL) {
|
||||
(*configObj)->lastfm_username = strdup(scrobbler_lastfm_username->valuestring);
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_password = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_password");
|
||||
if (cJSON_IsString(scrobbler_lastfm_password) && scrobbler_lastfm_password->valuestring != NULL) {
|
||||
(*configObj)->lastfm_password = strdup(scrobbler_lastfm_password->valuestring);
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_api_key = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_api_key");
|
||||
if (cJSON_IsString(scrobbler_lastfm_api_key) && scrobbler_lastfm_api_key->valuestring != NULL) {
|
||||
(*configObj)->lastfm_api_key = strdup(scrobbler_lastfm_api_key->valuestring);
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_api_secret = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_api_secret");
|
||||
if (cJSON_IsString(scrobbler_lastfm_api_secret) && scrobbler_lastfm_api_secret->valuestring != NULL) {
|
||||
(*configObj)->lastfm_api_secret = strdup(scrobbler_lastfm_api_secret->valuestring);
|
||||
}
|
||||
|
||||
cJSON* scrobbler_lastfm_api_session_key = cJSON_GetObjectItemCaseSensitive(scrobbler_root, "lastfm_session_key");
|
||||
if (cJSON_IsString(scrobbler_lastfm_api_session_key) && scrobbler_lastfm_api_session_key->valuestring != NULL) {
|
||||
(*configObj)->lastfm_api_session_key = strdup(scrobbler_lastfm_api_session_key->valuestring);
|
||||
}
|
||||
|
||||
// Make an object from audio
|
||||
cJSON* audio_root = cJSON_GetObjectItemCaseSensitive(root, "audio");
|
||||
if (audio_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - audio does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Make an object from equalizer
|
||||
cJSON* equalizer_root = cJSON_GetObjectItemCaseSensitive(audio_root, "equalizer");
|
||||
if (equalizer_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - equalizer does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_enable = cJSON_GetObjectItemCaseSensitive(equalizer_root, "enable");
|
||||
if (cJSON_IsBool(audio_equalizer_enable)) {
|
||||
if (cJSON_IsTrue(audio_equalizer_enable)) {
|
||||
(*configObj)->audio_equalizer_enable = true;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_followPitch = cJSON_GetObjectItemCaseSensitive(equalizer_root, "followPitch");
|
||||
if (cJSON_IsBool(audio_equalizer_followPitch)) {
|
||||
if (cJSON_IsTrue(audio_equalizer_followPitch)) {
|
||||
(*configObj)->audio_equalizer_followPitch = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch the equalizer graph array, and allocate memory for it
|
||||
cJSON* equalizer_graph_array = cJSON_GetObjectItemCaseSensitive(equalizer_root, "graph");
|
||||
if (equalizer_graph_array == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - graph does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
(*configObj)->audio_equalizer_graphCount = cJSON_GetArraySize(equalizer_graph_array);
|
||||
(*configObj)->audio_equalizer_graph = (configHandler_eqGraph_t*)malloc((*configObj)->audio_equalizer_graphCount * sizeof(configHandler_eqGraph_t));
|
||||
if ((*configObj)->audio_equalizer_graph == NULL) {
|
||||
logger_log_error(__func__, "malloc() failed.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize more struct variables
|
||||
for (size_t i = 0; i < (*configObj)->audio_equalizer_graphCount; i++) {
|
||||
(*configObj)->audio_equalizer_graph[i].bandwidth = 0.00;
|
||||
(*configObj)->audio_equalizer_graph[i].frequency = 0;
|
||||
(*configObj)->audio_equalizer_graph[i].gain = 0.00;
|
||||
(*configObj)->audio_equalizer_graph[i].bypass = false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < (*configObj)->audio_equalizer_graphCount; i++) {
|
||||
cJSON* array_equalizer_graph_root = cJSON_GetArrayItem(equalizer_graph_array, (int)i);
|
||||
if (array_equalizer_graph_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - Could not fetch graph index %d.", i);
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_graph_bandwidth = cJSON_GetObjectItemCaseSensitive(array_equalizer_graph_root, "bandwidth");
|
||||
if (cJSON_IsNumber(audio_equalizer_graph_bandwidth)) {
|
||||
(*configObj)->audio_equalizer_graph[i].bandwidth = audio_equalizer_graph_bandwidth->valuedouble;
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_graph_frequency = cJSON_GetObjectItemCaseSensitive(array_equalizer_graph_root, "frequency");
|
||||
if (cJSON_IsNumber(audio_equalizer_graph_frequency)) {
|
||||
(*configObj)->audio_equalizer_graph[i].frequency = audio_equalizer_graph_frequency->valueint;
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_graph_gain = cJSON_GetObjectItemCaseSensitive(array_equalizer_graph_root, "gain");
|
||||
if (cJSON_IsNumber(audio_equalizer_graph_gain)) {
|
||||
(*configObj)->audio_equalizer_graph[i].gain = audio_equalizer_graph_gain->valuedouble;
|
||||
}
|
||||
|
||||
cJSON* audio_equalizer_graph_bypass = cJSON_GetObjectItemCaseSensitive(array_equalizer_graph_root, "bypass");
|
||||
if (cJSON_IsBool(audio_equalizer_graph_bypass)) {
|
||||
if (cJSON_IsTrue(audio_equalizer_graph_bypass)) {
|
||||
(*configObj)->audio_equalizer_graph[i].bypass = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make an object from pitch
|
||||
cJSON* pitch_root = cJSON_GetObjectItemCaseSensitive(audio_root, "pitch");
|
||||
if (pitch_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - pitch does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* audio_pitch_enable = cJSON_GetObjectItemCaseSensitive(pitch_root, "enable");
|
||||
if (cJSON_IsBool(audio_pitch_enable)) {
|
||||
if (cJSON_IsTrue(audio_pitch_enable)) {
|
||||
(*configObj)->audio_pitch_enable = true;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* audio_pitch_cents = cJSON_GetObjectItemCaseSensitive(pitch_root, "cents");
|
||||
if (cJSON_IsNumber(audio_pitch_cents)) {
|
||||
(*configObj)->audio_pitch_cents = audio_pitch_cents->valuedouble;
|
||||
}
|
||||
|
||||
cJSON* audio_pitch_rate = cJSON_GetObjectItemCaseSensitive(pitch_root, "rate");
|
||||
if (cJSON_IsNumber(audio_pitch_rate)) {
|
||||
(*configObj)->audio_pitch_rate = audio_pitch_rate->valuedouble;
|
||||
}
|
||||
|
||||
// Make an object from reverb
|
||||
cJSON* reverb_root = cJSON_GetObjectItemCaseSensitive(audio_root, "reverb");
|
||||
if (reverb_root == NULL) {
|
||||
logger_log_error(__func__, "Error parsing JSON - reverb does not exist.");
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cJSON* audio_reverb_enable = cJSON_GetObjectItemCaseSensitive(reverb_root, "enable");
|
||||
if (cJSON_IsBool(audio_reverb_enable)) {
|
||||
if (cJSON_IsTrue(audio_reverb_enable)) {
|
||||
(*configObj)->audio_reverb_enable = true;
|
||||
}
|
||||
}
|
||||
|
||||
cJSON* audio_reverb_wetDryMix = cJSON_GetObjectItemCaseSensitive(reverb_root, "wetDryMix");
|
||||
if (cJSON_IsNumber(audio_reverb_wetDryMix)) {
|
||||
(*configObj)->audio_reverb_wetDryMix = audio_reverb_wetDryMix->valuedouble;
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
logger_log_general(__func__, "Successfully read configuration file.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void configHandler_Free(configHandler_config_t** configObj) {
|
||||
if ((*configObj)->opensubsonic_protocol != NULL) { free((*configObj)->opensubsonic_protocol); }
|
||||
if ((*configObj)->opensubsonic_server != NULL) { free((*configObj)->opensubsonic_server); }
|
||||
if ((*configObj)->opensubsonic_username != NULL) { free((*configObj)->opensubsonic_username); }
|
||||
if ((*configObj)->opensubsonic_password != NULL) { free((*configObj)->opensubsonic_password); }
|
||||
if ((*configObj)->internal_opensubsonic_version != NULL) { free((*configObj)->internal_opensubsonic_version); }
|
||||
if ((*configObj)->internal_opensubsonic_clientName != NULL) { free((*configObj)->internal_opensubsonic_clientName); }
|
||||
if ((*configObj)->internal_opensubsonic_loginSalt != NULL) { free((*configObj)->internal_opensubsonic_loginSalt); }
|
||||
if ((*configObj)->internal_opensubsonic_loginToken != NULL) { free((*configObj)->internal_opensubsonic_loginToken); }
|
||||
if ((*configObj)->listenbrainz_token != NULL) { free((*configObj)->listenbrainz_token); }
|
||||
if ((*configObj)->lastfm_username != NULL) { free((*configObj)->lastfm_username); }
|
||||
if ((*configObj)->lastfm_password != NULL) { free((*configObj)->lastfm_password); }
|
||||
if ((*configObj)->lastfm_api_key != NULL) { free((*configObj)->lastfm_api_key); }
|
||||
if ((*configObj)->lastfm_api_secret != NULL) { free((*configObj)->lastfm_api_secret); }
|
||||
if ((*configObj)->lastfm_api_session_key != NULL) { free((*configObj)->lastfm_api_session_key); }
|
||||
if ((*configObj)->audio_equalizer_graph != NULL) { free((*configObj)->audio_equalizer_graph); }
|
||||
if (*configObj != NULL) { free(*configObj); }
|
||||
}
|
||||
Reference in New Issue
Block a user