ref: e3a979ce631b8a32dd75280d2fae889b10cc0eec
parent: 852d20768b2ff77c27f87324ad3bd9697450ec51
author: cancel <cancel@cancel.fm>
date: Wed Jan 15 15:33:33 EST 2020
Cleanup
--- a/sysmisc.c
+++ b/sysmisc.c
@@ -384,14 +384,13 @@
return "Unknown";
}
-void ezconf_read_start(Ezconf_read *ezcr) {
+void ezconf_r_start(Ezconf_r *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) {
+bool ezconf_r_step(Ezconf_r *ezcr, char const *const *names, size_t nameslen) {
return conf_read_match(&ezcr->file, names, nameslen, ezcr->buffer,
sizeof ezcr->buffer, &ezcr->index, &ezcr->value);
}
@@ -398,9 +397,10 @@
enum {
Confwflag_add_newline = 1 << 0,
+ Ezconf_opt_written = 1 << 0,
};
-void ezconf_w_start(Ezconf_w *ezcw, Confopt_w *optsbuffer, size_t buffercap) {
+void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer, size_t buffercap) {
*ezcw = (Ezconf_w){.save = {0}}; // Weird to silence clang warning
ezcw->opts = optsbuffer;
ezcw->optscap = buffercap;
@@ -438,13 +438,13 @@
size_t count = ezcw->optscount, cap = ezcw->optscap;
if (count == cap)
return;
- ezcw->opts[count] = (Confopt_w){.name = key, .id = id, .written = 0};
+ ezcw->opts[count] = (Ezconf_opt){.name = key, .id = id, .flags = 0};
ezcw->optscount = count + 1;
}
bool ezconf_w_step(Ezconf_w *ezcw) {
- U32 stateflags = ezcw->stateflags;
+ uint32_t stateflags = ezcw->stateflags;
FILE *origfile = ezcw->save.origfile, *tempfile = ezcw->save.tempfile;
- Confopt_w *opts = ezcw->opts, *chosen = NULL;
+ Ezconf_opt *opts = ezcw->opts, *chosen = NULL;
size_t optscount = ezcw->optscount;
if (ezcw->error || !tempfile) // Already errored or finished ok
return false;
@@ -452,7 +452,7 @@
// write it now.
if (stateflags & Confwflag_add_newline) {
fputs("\n", tempfile);
- stateflags &= ~(U32)Confwflag_add_newline;
+ stateflags &= ~(uint32_t)Confwflag_add_newline;
}
if (!optscount)
goto commit;
@@ -461,7 +461,7 @@
for (;;) { // Scan through file looking for known keys in key=value lines
char linebuff[1024];
char *left, *right;
- Usz leftsz, rightsz;
+ size_t leftsz, rightsz;
Conf_read_result res = conf_read_line(origfile, linebuff, sizeof linebuff,
&left, &leftsz, &right, &rightsz);
switch (res) {
@@ -474,7 +474,7 @@
continue;
// If we already wrote this one, comment out the line instead, and move
// on to the next line.
- if (opts[i].written) {
+ if (opts[i].flags & (uint8_t)Ezconf_opt_written) {
fputs("# ", tempfile);
goto write_landr;
}
@@ -516,7 +516,7 @@
// "write the leftovers" phase.)
opts++;
optscount--;
- if (!chosen->written)
+ if (!(chosen->flags & (uint8_t)Ezconf_opt_written))
break;
}
// Once control has reached here, we're going to return true to the caller.
@@ -527,11 +527,11 @@
ezcw->opts = opts;
ezcw->optscount = optscount;
return_for_writing:
- chosen->written = true;
+ chosen->flags |= (uint8_t)Ezconf_opt_written;
fputs(chosen->name, tempfile);
fputs(" = ", tempfile);
ezcw->optid = chosen->id;
- stateflags |= (U32)Confwflag_add_newline;
+ stateflags |= (uint32_t)Confwflag_add_newline;
ezcw->stateflags = stateflags;
return true;
cancel:
--- a/sysmisc.h
+++ b/sysmisc.h
@@ -83,11 +83,10 @@
Usz index;
char *value;
char buffer[1024];
-} Ezconf_read;
+} Ezconf_r;
-void ezconf_read_start(Ezconf_read *ezcr);
-bool ezconf_read_step(Ezconf_read *ezcr, char const *const *names,
- Usz nameslen);
+void ezconf_r_start(Ezconf_r *ezcr);
+bool ezconf_r_step(Ezconf_r *ezcr, char const *const *names, Usz nameslen);
typedef enum {
Ezconf_w_ok = 0,
@@ -111,19 +110,19 @@
typedef struct {
char const *name;
intptr_t id;
- U8 written : 1;
-} Confopt_w;
+ uint8_t flags;
+} Ezconf_opt;
typedef struct {
Conf_save save;
- Confopt_w *opts;
+ Ezconf_opt *opts;
size_t optscount, optscap;
intptr_t optid;
FILE *file;
Ezconf_w_error error;
- U32 stateflags;
+ uint32_t stateflags;
} Ezconf_w;
-void ezconf_w_start(Ezconf_w *ezcw, Confopt_w *optsbuffer, size_t buffercap);
+void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer, size_t buffercap);
void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id);
bool ezconf_w_step(Ezconf_w *ezcw);
--- a/tui_main.c
+++ b/tui_main.c
@@ -2375,8 +2375,8 @@
ORCA_FORCE_NO_INLINE
Prefs_load_error prefs_load_from_conf_file(Prefs *p) {
- Ezconf_read ez;
- for (ezconf_read_start(&ez); ezconf_read_step(&ez, confopts, Confoptslen);) {
+ Ezconf_r ez;
+ for (ezconf_r_start(&ez); ezconf_r_step(&ez, confopts, Confoptslen);) {
switch (ez.index) {
case Confopt_portmidi_output_device: {
osoput(&p->portmidi_output_device, ez.value);
@@ -2398,7 +2398,7 @@
void save_prefs_with_error_message(Midi_mode const *midi_mode, int softmargin_y,
int softmargin_x,
bool softmargins_touched_by_user) {
- Confopt_w optsbuff[Confoptslen];
+ Ezconf_opt optsbuff[Confoptslen];
Ezconf_w ez;
ezconf_w_start(&ez, optsbuff, ORCA_ARRAY_COUNTOF(optsbuff));
oso *midi_output_device_name = NULL;