ref: db385d7a06f4b0c34489b7b8d1c7a75fea959222
parent: a32586de20453d725ca0f952555882728013b3db
author: cancel <cancel@cancel.fm>
date: Tue Jan 14 13:12:46 EST 2020
Add first-pass attempt at reducing conf file sprawl
--- a/sysmisc.c
+++ b/sysmisc.c
@@ -172,6 +172,37 @@
return Conf_read_left_and_right;
}
+bool conf_read_match(FILE **pfile, char const *const *names, Usz nameslen,
+ char *buf, Usz bufsize, Usz *out_index, char **out_value) {
+ FILE *file = *pfile;
+ if (!file)
+ return false;
+ char *left;
+ Usz leftsz, rightsz;
+next_line:;
+ Conf_read_result res =
+ conf_read_line(file, buf, bufsize, &left, &leftsz, out_value, &rightsz);
+ switch (res) {
+ case Conf_read_left_and_right:
+ for (Usz i = 0; i < nameslen; i++) {
+ if (strcmp(names[i], left) == 0) {
+ *out_index = i;
+ return true;
+ }
+ }
+ goto next_line;
+ case Conf_read_irrelevant:
+ goto next_line;
+ case Conf_read_buffer_too_small:
+ case Conf_read_eof:
+ case Conf_read_io_error:
+ break;
+ }
+ fclose(file);
+ *pfile = NULL;
+ return false;
+}
+
typedef enum {
Conf_dir_ok = 0,
Conf_dir_no_home,
@@ -341,4 +372,16 @@
break;
}
return "Unknown";
+}
+
+void ezconf_read_start(Ezconf_read *ezcr) {
+ ezcr->file = conf_file_open_for_reading();
+ ezcr->index = 0;
+ ezcr->value = NULL;
+}
+
+bool ezconf_read_step(Ezconf_read *ezcr, char const *const *names,
+ Usz nameslen) {
+ return conf_read_match(&ezcr->file, names, nameslen, ezcr->buffer,
+ sizeof ezcr->buffer, &ezcr->index, &ezcr->value);
}
--- a/sysmisc.h
+++ b/sysmisc.h
@@ -28,6 +28,9 @@
char **out_left, Usz *out_leftlen,
char **out_right, Usz *out_rightlen);
+bool conf_read_match(FILE **pfile, char const *const *names, Usz nameslen,
+ char *buf, Usz bufsize, Usz *out_index, char **out_value);
+
FILE *conf_file_open_for_reading(void);
typedef struct {
@@ -92,3 +95,14 @@
} Prefs_save_error;
char const *prefs_save_error_string(Prefs_save_error error);
+
+// Just playing around with this design
+typedef struct {
+ FILE *file;
+ Usz index;
+ char *value;
+ char buffer[1024];
+} Ezconf_read;
+
+void ezconf_read_start(Ezconf_read *ezcr);
+bool ezconf_read_step(Ezconf_read *ezcr, char const *const*names, Usz nameslen);
--- a/tui_main.c
+++ b/tui_main.c
@@ -2366,47 +2366,32 @@
Prefs_load_ok = 0,
} Prefs_load_error;
-static char const *confkey_portmidi_output_device = "portmidi_output_device";
-static char const *confkey_margins = "margins";
+char const *const confopts[] = {"portmidi_output_device", "margins"};
+enum { Confoptslen = ORCA_ARRAY_COUNTOF(confopts) };
+enum {
+ Confopt_portmidi_output_device,
+ Confopt_margins,
+};
ORCA_FORCE_NO_INLINE
Prefs_load_error prefs_load_from_conf_file(Prefs *p) {
- (void)p;
- FILE *conffile = conf_file_open_for_reading();
- if (!conffile) {
- return Prefs_load_ok;
- }
- char linebuff[1024];
- char *left, *right;
- Usz leftsz, rightsz;
- for (;;) {
- Conf_read_result res = conf_read_line(conffile, linebuff, sizeof linebuff,
- &left, &leftsz, &right, &rightsz);
- switch (res) {
- case Conf_read_left_and_right: {
- if (strcmp(confkey_portmidi_output_device, left) == 0) {
- osoput(&p->portmidi_output_device, right);
- } else if (strcmp(confkey_margins, left) == 0) {
- int softmargin_y, softmargin_x;
- if (read_nxn_or_n(right, &softmargin_x, &softmargin_y) &&
- softmargin_y >= 0 && softmargin_x >= 0) {
- p->softmargin_y = softmargin_y;
- p->softmargin_x = softmargin_x;
- p->has_softmargins = true;
- }
+ Ezconf_read ez;
+ for (ezconf_read_start(&ez); ezconf_read_step(&ez, confopts, Confoptslen);) {
+ switch (ez.index) {
+ case Confopt_portmidi_output_device: {
+ osoput(&p->portmidi_output_device, ez.value);
+ } break;
+ case Confopt_margins: {
+ int softmargin_y, softmargin_x;
+ if (read_nxn_or_n(ez.value, &softmargin_x, &softmargin_y) &&
+ softmargin_y >= 0 && softmargin_x >= 0) {
+ p->softmargin_y = softmargin_y;
+ p->softmargin_x = softmargin_x;
+ p->has_softmargins = true;
}
- continue;
+ } break;
}
- case Conf_read_irrelevant:
- continue;
- case Conf_read_buffer_too_small:
- case Conf_read_eof:
- case Conf_read_io_error:
- break;
- }
- break;
}
- fclose(conffile);
return Prefs_load_ok;
}
@@ -2481,22 +2466,22 @@
switch (res) {
case Conf_read_left_and_right:
#ifdef FEAT_PORTMIDI
- if (strcmp(confkey_portmidi_output_device, left) == 0) {
+ if (strcmp(confopts[Confopt_portmidi_output_device], left) == 0) {
if (midi_output_pref != Midi_output_pref_portmidi)
continue;
midi_output_pref = Midi_output_pref_none;
- put_conf_pair(save.tempfile, confkey_portmidi_output_device,
+ put_conf_pair(save.tempfile, confopts[Confopt_portmidi_output_device],
osoc(midi_output_device_name));
osowipe(&midi_output_device_name);
continue;
}
#endif
- if (strcmp(confkey_margins, left) == 0) {
+ if (strcmp(confopts[Confopt_margins], left) == 0) {
if (!needs_write_margins)
continue;
needs_write_margins = false;
- fprintf(save.tempfile, "%s = %dx%d\n", confkey_margins, softmargin_x,
- softmargin_y);
+ fprintf(save.tempfile, "%s = %dx%d\n", confopts[Confopt_margins],
+ softmargin_x, softmargin_y);
continue;
}
put_conf_pair(save.tempfile, left, right);
@@ -2521,7 +2506,7 @@
break;
#ifdef FEAT_PORTMIDI
case Midi_output_pref_portmidi:
- put_conf_pair(save.tempfile, confkey_portmidi_output_device,
+ put_conf_pair(save.tempfile, confopts[Confopt_portmidi_output_device],
osoc(midi_output_device_name));
osowipe(&midi_output_device_name);
break;
@@ -2529,8 +2514,8 @@
}
if (needs_write_margins) {
needs_write_margins = false;
- fprintf(save.tempfile, "%s = %dx%d\n", confkey_margins, softmargin_x,
- softmargin_y); // TODO redundant
+ fprintf(save.tempfile, "%s = %dx%d\n", confopts[Confopt_margins],
+ softmargin_x, softmargin_y); // TODO redundant
}
need_cancel_save = false;
Conf_save_commit_error comerr = conf_save_commit(&save);