From 5e88e7c2c3b62a762eaf1a10222b4750e44c70f3 Mon Sep 17 00:00:00 2001 From: 20xd6 <20xd6@airmail.cc> Date: Fri, 4 Oct 2024 00:07:06 -0400 Subject: [PATCH] Reading a config file is now working. --- get_artist_art.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 get_artist_art.c diff --git a/get_artist_art.c b/get_artist_art.c new file mode 100644 index 0000000..fc5a0c1 --- /dev/null +++ b/get_artist_art.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#define conf_file "config.ini" + +const char *get_conf_str(char set_key[10]) { + const char *prog_conf = malloc(10 * sizeof(char)); + config_t cfg; + config_setting_t *setting; + + config_init(&cfg); + + if (!config_read_file(&cfg, conf_file)) { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return ""; + } + + setting = config_lookup(&cfg, set_key); + + prog_conf = config_setting_get_string(setting); + + config_destroy(&cfg); + return prog_conf; +} + +int get_conf_int(char set_key[10]) { + int set_int; + config_t cfg; + config_setting_t *setting; + + config_init(&cfg); + + if (!config_read_file(&cfg, conf_file)) { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return 1; + } + +setting = config_lookup(&cfg, set_key); + set_int = config_setting_get_int(setting); + + config_destroy(&cfg); + + return set_int; +} + +void print_conf(const char m_dir[20], const int mb_conf, + const char ftv_api_key[32]) { + if (m_dir != NULL) { + printf("The music directory is: %s\n", m_dir); + } + if (mb_conf != 1) { + printf("The lower limit for MB confidence is: %d\n", mb_conf); + } + if (ftv_api_key != NULL) { + printf("Your API key is: %s\n", ftv_api_key); + } +} + +int main() { + int mb_conf; + const char *m_dir, *ftv_api_key; + + m_dir = get_conf_str("dir"); + mb_conf = get_conf_int("confidence"); + ftv_api_key = get_conf_str("api_key"); + + print_conf(m_dir, mb_conf, ftv_api_key); + + return EXIT_SUCCESS; +} \ No newline at end of file