commit 7defa608e1cbc4b8dcdf26a25181ddc9d3e9d3da
parent 25cc5608f7c0807f53c3e46cf454a451edebc83b
Author: nibo <nibo@relim.de>
Date: Tue, 10 Sep 2024 21:23:10 +0200
Improve --print-default-config
Diffstat:
| M | chordpro.c | | | 2 | +- |
| M | config.c | | | 99 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
| M | todo | | | 3 | +-- |
3 files changed, 62 insertions(+), 42 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -535,7 +535,7 @@ const char *cho_font_weight_to_config_string(enum FontWeight weight)
void cho_font_print_as_toml(struct Font *font, const char *section)
{
- printf("[styles.%s.font]\n", section);
+ printf("[output.styles.%s.font]\n", section);
printf("\n");
printf("name = \"%s\"\n", font->name);
printf("family = \"%s\"\n", cho_font_family_to_config_string(font->family));
diff --git a/config.c b/config.c
@@ -114,6 +114,43 @@ struct PrintableItem *config_printable_item_get(struct PrintableItem **items, co
return NULL;
}
+static enum NamingSystem config_naming_system_parse(const char *str)
+{
+ if (strcmp(str, "common") == 0 || strcmp(str, "dutch") == 0) {
+ return NS_COMMON;
+ } else if (strcmp(str, "german") == 0) {
+ return NS_GERMAN;
+ } else if (strcmp(str, "scandinavian") == 0) {
+ return NS_SCANDINAVIAN;
+ } else if (strcmp(str, "latin") == 0) {
+ return NS_LATIN;
+ } else if (strcmp(str, "roman") == 0) {
+ return NS_ROMAN;
+ } else if (strcmp(str, "nashville") == 0) {
+ return NS_NASHVILLE;
+ } else {
+ return NS_CUSTOM;
+ }
+}
+
+static const char *config_naming_system_to_config_string(enum NamingSystem system)
+{
+ switch (system) {
+ case NS_GERMAN:
+ return "german";
+ case NS_SCANDINAVIAN:
+ return "scandinavian";
+ case NS_LATIN:
+ return "latin";
+ case NS_ROMAN:
+ return "roman";
+ case NS_NASHVILLE:
+ return "nashville";
+ default:
+ return "common";
+ }
+}
+
static struct Note *config_note_new(void)
{
struct Note *note = malloc(sizeof(struct Note));
@@ -231,49 +268,30 @@ static struct Note **config_notes_load(toml_table_t *notes, const char *system)
return NULL;
}
-static const char *config_parse_mode_to_config_string(enum ParseMode mode)
+static void config_notes_print_as_toml(enum NamingSystem system, struct Note **notes)
{
- if (mode == PM_RELAXED) {
- return "relaxed";
- }
- return "strict";
-}
-
-static enum NamingSystem config_naming_system_parse(const char *str)
-{
- if (strcmp(str, "common") == 0 || strcmp(str, "dutch") == 0) {
- return NS_COMMON;
- } else if (strcmp(str, "german") == 0) {
- return NS_GERMAN;
- } else if (strcmp(str, "scandinavian") == 0) {
- return NS_SCANDINAVIAN;
- } else if (strcmp(str, "latin") == 0) {
- return NS_LATIN;
- } else if (strcmp(str, "roman") == 0) {
- return NS_ROMAN;
- } else if (strcmp(str, "nashville") == 0) {
- return NS_NASHVILLE;
- } else {
- return NS_CUSTOM;
+ printf("%s = [\n", config_naming_system_to_config_string(system));
+ int i;
+ // TODO: handle NULL cases
+ for (i = 0; notes[i]; i++) {
+ printf("\t{ note = \"%s\",", notes[i]->note);
+ if (notes[i]->sharp) {
+ printf(" sharp = \"%s\",", notes[i]->sharp);
+ }
+ if (notes[i]->flat) {
+ printf(" flat = \"%s\"", notes[i]->flat);
+ }
+ printf(" },\n");
}
+ printf("]\n\n");
}
-static const char *config_naming_system_to_config_string(enum NamingSystem system)
+static const char *config_parse_mode_to_config_string(enum ParseMode mode)
{
- switch (system) {
- case NS_GERMAN:
- return "german";
- case NS_SCANDINAVIAN:
- return "scandinavian";
- case NS_LATIN:
- return "latin";
- case NS_ROMAN:
- return "roman";
- case NS_NASHVILLE:
- return "nashville";
- default:
- return "common";
+ if (mode == PM_RELAXED) {
+ return "relaxed";
}
+ return "strict";
}
static struct Config *config_load_default(void)
@@ -333,6 +351,9 @@ void config_print_default(void)
struct Config *config = config_load_default();
int i = 0;
printf("[output]\n\n");
+ printf("system = \"%s\"\n\n", config_naming_system_to_config_string(config->output->system));
+ printf("[output.notes]\n\n");
+ config_notes_print_as_toml(config->output->system, config->output->notes);
printf("[output.styles]\n\n");
while (config->output->printable_items[i] != NULL) {
config_printable_item_print_as_toml(config->output->printable_items[i]);
@@ -563,9 +584,9 @@ struct Config *config_load(const char *filepath)
if (value.ok) {
system = config_naming_system_parse(value.u.s);
if (system == NS_CUSTOM) {
- notes = toml_table_table(table, "notes");
+ notes = toml_table_table(output, "notes");
if (!notes) {
- fprintf(stderr, "ERR: Custom notes '%s' has no corresponding definition in [notes].\n", value.u.s);
+ fprintf(stderr, "ERR: Custom notes '%s' has no corresponding definition in [output.notes].\n", value.u.s);
return NULL;
}
custom_notes = config_notes_load(notes, value.u.s);
diff --git a/todo b/todo
@@ -11,7 +11,6 @@ chords
chord diagrams
strict and relaxed parsing makes no difference!?
make parser bulletproof
- parse \r\n also as linebreak
should it just parse valid input correctly and crash on invalid input?
parse environment directive value when: label="Verse 1"
@@ -19,5 +18,5 @@ parse environment directive value when: label="Verse 1"
break lines when too long
render in two or more columns
-find better name fore PrintableItem, TextAbove
+find better name for PrintableItem, TextAbove
consider freeing memory in case of errors