lorid

convert chordpro to pdf
git clone git://git.relim.de/lorid.git
Log | Files | Refs | README | LICENSE

commit 0cab9c7e8c41b00b43878ae538f04349fd89a39f
parent 276ed2be439625121085aa8d4b445bb16fbab441
Author: nibo <nibo@relim.de>
Date:   Thu, 27 Mar 2025 22:03:42 +0100

Eliminate global variable in config.c

Diffstat:
Msrc/config.c | 43+++++++++++++++++++++++++------------------
Msrc/config.h | 5+++++
2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/src/config.c b/src/config.c @@ -3,7 +3,6 @@ #include <stdarg.h> #include <string.h> #include <toml.h> -#include <linux/limits.h> #include "core.h" #include "config.h" #include "chordpro.h" @@ -96,10 +95,9 @@ static struct Note notes_nashville[] = { { .note = "7", .sharp = NULL, .flat = "7b" }, }; -static char g_config_filepath[PATH_MAX]; - static void config_log( + struct ConfigContext *ctx, enum LogLevel level, const char *toml_section, const char *msg, @@ -108,9 +106,9 @@ config_log( { va_list va; va_start(va, msg); - char str[64]; + char str[10+strlen(toml_section)+strlen(msg)+1]; sprintf((char *)&str, "section %s: %s", toml_section, msg); - util_vlog(g_config_filepath, 0, level, str, va); + util_vlog(ctx->config_filepath, 0, level, str, va); } static enum TextType @@ -302,13 +300,20 @@ config_notes_new_default(enum NotationSystem system) } static struct Note ** -config_notes_load(toml_table_t *notes, const char *system) +config_notes_load( + struct ConfigContext *ctx, + toml_table_t *notes, + const char *system +) { struct Note **custom_notes = emalloc(8 * sizeof(struct Note *)); toml_array_t *arr = toml_table_array(notes, system); + if (!arr) { + return NULL; + } int arr_len = toml_array_len(arr); if (arr_len != 7) { - config_log(LOG_ERR, "[notation_systems]", "Custom notation system '%s' has to have exactly 7 items. For an example see `lorid --print-default-config`.", system); + config_log(ctx, LOG_ERR, "[notation_systems]", "Custom notation system '%s' has to have exactly 7 items. For an example see `lorid --print-default-config`.", system); free(notes); return NULL; } @@ -327,7 +332,7 @@ config_notes_load(toml_table_t *notes, const char *system) if (value.ok) { custom_notes[i]->sharp = value.u.s; if (i == 2 || i == 6) { - config_log(LOG_ERR, "[notation_systems]", "Custom notation system '%s' can't have sharp value at array index '%d'.", system, i); + config_log(ctx, LOG_ERR, "[notation_systems]", "Custom notation system '%s' can't have sharp value at array index '%d'.", system, i); goto CLEAN; } } @@ -335,7 +340,7 @@ config_notes_load(toml_table_t *notes, const char *system) if (value.ok) { custom_notes[i]->flat = value.u.s; if (i == 0 || i == 3) { - config_log(LOG_ERR, "[notation_systems]", "Custom notation system '%s' can't have flat value at array index '%d'.", system, i); + config_log(ctx, LOG_ERR, "[notation_systems]", "Custom notation system '%s' can't have flat value at array index '%d'.", system, i); goto CLEAN; } } @@ -881,6 +886,7 @@ lyrics_set_text_style_as_default( struct Config * config_load(const char *filepath) { + struct ConfigContext ctx; struct Config *config = config_load_default(); char *home = getenv("HOME"); char path[26+strlen(home)+1]; @@ -888,7 +894,8 @@ config_load(const char *filepath) sprintf(path, "%s/.config/lorid/config.toml", home); filepath = path; } - strcpy(g_config_filepath, filepath); + strcpy(ctx.config_filepath, filepath); + FILE *fp = fopen(filepath, "r"); if (!fp) { util_log(NULL, 0, LOG_INFO, "Couldn't open config file '%s'. Using default configuration.", filepath); @@ -950,7 +957,7 @@ config_load(const char *filepath) instrument = config_instrument_parse(value.u.s, &error); if (error) { LOG_DEBUG("config_instrument_parse failed."); - config_log(LOG_ERR, "[output.chord_diagram]", "Unknown instrument '%s'.", value.u.s); + config_log(&ctx, LOG_ERR, "[output.chord_diagram]", "Unknown instrument '%s'.", value.u.s); return NULL; } config->output->diagram->instrument = instrument; @@ -963,16 +970,16 @@ config_load(const char *filepath) if (notation_system == NS_CUSTOM) { notes = toml_table_table(table, "notation_systems"); if (!notes) { - config_log(LOG_ERR, "[output]", "Custom notation system '%s' has no corresponding definition in [notation_systems]"); + config_log(&ctx, LOG_ERR, "[output]", "Custom notation system '%s' has no corresponding definition in [notation_systems]"); return NULL; } - custom_notes = config_notes_load(notes, value.u.s); + custom_notes = config_notes_load(&ctx, notes, value.u.s); if (custom_notes) { config_notes_free(config->output->notes); config->output->notes = custom_notes; } else { LOG_DEBUG("config_notes_load failed."); - config_log(LOG_ERR, "[output]", "Couldn't load custom notation system '%s' from [notation_systems] section.", value.u.s); + config_log(&ctx, LOG_ERR, "[output]", "Couldn't load custom notation system '%s' from [notation_systems] section.", value.u.s); return NULL; } } else { @@ -1022,7 +1029,7 @@ config_load(const char *filepath) sprintf((char *)&toml_section_name, "[output.styles.%s]", key_name); if (!config_load_style(style, key, &presences[ttype], &err)) { LOG_DEBUG("config_load_style failed."); - config_log(LOG_ERR, toml_section_name, err); + config_log(&ctx, LOG_ERR, toml_section_name, err); return NULL; } } @@ -1044,16 +1051,16 @@ config_load(const char *filepath) if (notation_system == NS_CUSTOM) { notes = toml_table_table(table, "notation_systems"); if (!notes) { - config_log(LOG_ERR, "[parser.chords]", "Custom notation system '%s' has no corresponding definition in [notation_systems].", value.u.s); + config_log(&ctx, LOG_ERR, "[parser.chords]", "Custom notation system '%s' has no corresponding definition in [notation_systems].", value.u.s); return NULL; } - custom_notes = config_notes_load(notes, value.u.s); + custom_notes = config_notes_load(&ctx, notes, value.u.s); if (custom_notes) { config_notes_free(config->parser->notes); config->parser->notes = custom_notes; } else { LOG_DEBUG("config_notes_load failed."); - config_log(LOG_ERR, "[parser.chords]", "Couldn't load custom notation system '%s' from [notation_systems] section.", value.u.s); + config_log(&ctx, LOG_ERR, "[parser.chords]", "Couldn't load custom notation system '%s' from [notation_systems] section.", value.u.s); return NULL; } } else { diff --git a/src/config.h b/src/config.h @@ -1,3 +1,4 @@ +#include <linux/limits.h> #include "core.h" #ifndef _CONFIG_H_ @@ -9,6 +10,10 @@ #define SYMBOLS_FILEPATH PREFIX"/share/lorid/ChordProSymbols.ttf" #endif /* DEBUG */ +struct ConfigContext { + char config_filepath[PATH_MAX]; +}; + struct Config *config_load(const char *filepath); void config_free(struct Config *config); void config_print_default(void);