Browse Source

Cleanup

master
cancel 5 years ago
parent
commit
e3a979ce63
  1. 26
      sysmisc.c
  2. 17
      sysmisc.h
  3. 6
      tui_main.c

26
sysmisc.c

@ -384,23 +384,23 @@ char const *ezconf_w_error_string(Ezconf_w_error error) {
return "Unknown"; return "Unknown";
} }
void ezconf_read_start(Ezconf_read *ezcr) { void ezconf_r_start(Ezconf_r *ezcr) {
ezcr->file = conf_file_open_for_reading(); ezcr->file = conf_file_open_for_reading();
ezcr->index = 0; ezcr->index = 0;
ezcr->value = NULL; ezcr->value = NULL;
} }
bool ezconf_read_step(Ezconf_read *ezcr, char const *const *names, bool ezconf_r_step(Ezconf_r *ezcr, char const *const *names, size_t nameslen) {
Usz nameslen) {
return conf_read_match(&ezcr->file, names, nameslen, ezcr->buffer, return conf_read_match(&ezcr->file, names, nameslen, ezcr->buffer,
sizeof ezcr->buffer, &ezcr->index, &ezcr->value); sizeof ezcr->buffer, &ezcr->index, &ezcr->value);
} }
enum { enum {
Confwflag_add_newline = 1 << 0, 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 = (Ezconf_w){.save = {0}}; // Weird to silence clang warning
ezcw->opts = optsbuffer; ezcw->opts = optsbuffer;
ezcw->optscap = buffercap; 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; size_t count = ezcw->optscount, cap = ezcw->optscap;
if (count == cap) if (count == cap)
return; 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; ezcw->optscount = count + 1;
} }
bool ezconf_w_step(Ezconf_w *ezcw) { 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; 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; size_t optscount = ezcw->optscount;
if (ezcw->error || !tempfile) // Already errored or finished ok if (ezcw->error || !tempfile) // Already errored or finished ok
return false; return false;
@ -452,7 +452,7 @@ bool ezconf_w_step(Ezconf_w *ezcw) {
// write it now. // write it now.
if (stateflags & Confwflag_add_newline) { if (stateflags & Confwflag_add_newline) {
fputs("\n", tempfile); fputs("\n", tempfile);
stateflags &= ~(U32)Confwflag_add_newline; stateflags &= ~(uint32_t)Confwflag_add_newline;
} }
if (!optscount) if (!optscount)
goto commit; 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 for (;;) { // Scan through file looking for known keys in key=value lines
char linebuff[1024]; char linebuff[1024];
char *left, *right; char *left, *right;
Usz leftsz, rightsz; size_t leftsz, rightsz;
Conf_read_result res = conf_read_line(origfile, linebuff, sizeof linebuff, Conf_read_result res = conf_read_line(origfile, linebuff, sizeof linebuff,
&left, &leftsz, &right, &rightsz); &left, &leftsz, &right, &rightsz);
switch (res) { switch (res) {
@ -474,7 +474,7 @@ bool ezconf_w_step(Ezconf_w *ezcw) {
continue; continue;
// If we already wrote this one, comment out the line instead, and move // If we already wrote this one, comment out the line instead, and move
// on to the next line. // on to the next line.
if (opts[i].written) { if (opts[i].flags & (uint8_t)Ezconf_opt_written) {
fputs("# ", tempfile); fputs("# ", tempfile);
goto write_landr; goto write_landr;
} }
@ -516,7 +516,7 @@ write_leftovers: // Write out any guys that weren't in original file.
// "write the leftovers" phase.) // "write the leftovers" phase.)
opts++; opts++;
optscount--; optscount--;
if (!chosen->written) if (!(chosen->flags & (uint8_t)Ezconf_opt_written))
break; break;
} }
// Once control has reached here, we're going to return true to the caller. // 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->opts = opts;
ezcw->optscount = optscount; ezcw->optscount = optscount;
return_for_writing: return_for_writing:
chosen->written = true; chosen->flags |= (uint8_t)Ezconf_opt_written;
fputs(chosen->name, tempfile); fputs(chosen->name, tempfile);
fputs(" = ", tempfile); fputs(" = ", tempfile);
ezcw->optid = chosen->id; ezcw->optid = chosen->id;
stateflags |= (U32)Confwflag_add_newline; stateflags |= (uint32_t)Confwflag_add_newline;
ezcw->stateflags = stateflags; ezcw->stateflags = stateflags;
return true; return true;
cancel: cancel:

17
sysmisc.h

@ -83,11 +83,10 @@ typedef struct {
Usz index; Usz index;
char *value; char *value;
char buffer[1024]; char buffer[1024];
} Ezconf_read; } Ezconf_r;
void ezconf_read_start(Ezconf_read *ezcr); void ezconf_r_start(Ezconf_r *ezcr);
bool ezconf_read_step(Ezconf_read *ezcr, char const *const *names, bool ezconf_r_step(Ezconf_r *ezcr, char const *const *names, Usz nameslen);
Usz nameslen);
typedef enum { typedef enum {
Ezconf_w_ok = 0, Ezconf_w_ok = 0,
@ -111,19 +110,19 @@ char const *ezconf_w_error_string(Ezconf_w_error error);
typedef struct { typedef struct {
char const *name; char const *name;
intptr_t id; intptr_t id;
U8 written : 1; uint8_t flags;
} Confopt_w; } Ezconf_opt;
typedef struct { typedef struct {
Conf_save save; Conf_save save;
Confopt_w *opts; Ezconf_opt *opts;
size_t optscount, optscap; size_t optscount, optscap;
intptr_t optid; intptr_t optid;
FILE *file; FILE *file;
Ezconf_w_error error; Ezconf_w_error error;
U32 stateflags; uint32_t stateflags;
} Ezconf_w; } 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); void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id);
bool ezconf_w_step(Ezconf_w *ezcw); bool ezconf_w_step(Ezconf_w *ezcw);

6
tui_main.c

@ -2375,8 +2375,8 @@ enum {
ORCA_FORCE_NO_INLINE ORCA_FORCE_NO_INLINE
Prefs_load_error prefs_load_from_conf_file(Prefs *p) { Prefs_load_error prefs_load_from_conf_file(Prefs *p) {
Ezconf_read ez; Ezconf_r ez;
for (ezconf_read_start(&ez); ezconf_read_step(&ez, confopts, Confoptslen);) { for (ezconf_r_start(&ez); ezconf_r_step(&ez, confopts, Confoptslen);) {
switch (ez.index) { switch (ez.index) {
case Confopt_portmidi_output_device: { case Confopt_portmidi_output_device: {
osoput(&p->portmidi_output_device, ez.value); 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, void save_prefs_with_error_message(Midi_mode const *midi_mode, int softmargin_y,
int softmargin_x, int softmargin_x,
bool softmargins_touched_by_user) { bool softmargins_touched_by_user) {
Confopt_w optsbuff[Confoptslen]; Ezconf_opt optsbuff[Confoptslen];
Ezconf_w ez; Ezconf_w ez;
ezconf_w_start(&ez, optsbuff, ORCA_ARRAY_COUNTOF(optsbuff)); ezconf_w_start(&ez, optsbuff, ORCA_ARRAY_COUNTOF(optsbuff));
oso *midi_output_device_name = NULL; oso *midi_output_device_name = NULL;

Loading…
Cancel
Save