From e3a979ce631b8a32dd75280d2fae889b10cc0eec Mon Sep 17 00:00:00 2001 From: cancel Date: Wed, 15 Jan 2020 20:33:33 +0900 Subject: [PATCH] Cleanup --- sysmisc.c | 26 +++++++++++++------------- sysmisc.h | 17 ++++++++--------- tui_main.c | 6 +++--- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/sysmisc.c b/sysmisc.c index ace14d4..bcf00d6 100644 --- a/sysmisc.c +++ b/sysmisc.c @@ -384,23 +384,23 @@ char const *ezconf_w_error_string(Ezconf_w_error error) { 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); } 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 @@ void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id) { 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 @@ bool ezconf_w_step(Ezconf_w *ezcw) { // 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 @@ bool ezconf_w_step(Ezconf_w *ezcw) { 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 @@ bool ezconf_w_step(Ezconf_w *ezcw) { 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_leftovers: // Write out any guys that weren't in original file. // "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 @@ write_leftovers: // Write out any guys that weren't in original file. 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: diff --git a/sysmisc.h b/sysmisc.h index c530f0a..c85de64 100644 --- a/sysmisc.h +++ b/sysmisc.h @@ -83,11 +83,10 @@ typedef struct { 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 @@ char const *ezconf_w_error_string(Ezconf_w_error error); 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); diff --git a/tui_main.c b/tui_main.c index 2ead466..2684373 100644 --- a/tui_main.c +++ b/tui_main.c @@ -2375,8 +2375,8 @@ enum { 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 @@ Prefs_load_error prefs_load_from_conf_file(Prefs *p) { 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;