expend netsurf options API

This commit is contained in:
Vincent Sanders 2024-09-09 16:01:20 +01:00
parent 1c6232da6a
commit aa4476cb5f
2 changed files with 124 additions and 58 deletions

View File

@ -278,69 +278,48 @@ nsoption_is_set(const struct nsoption_s *opts,
} }
/** /**
* Output choices to file stream * Output an option value into a file stream, in plain text format.
* *
* @param option The option to output the value of.
* @param fp The file stream to write to. * @param fp The file stream to write to.
* @param opts The options table to write. * @return The number of bytes written to string or -1 on error
* @param defs The default value table to compare with.
* @param all Output all entries not just ones changed from defaults
*/ */
static nserror static size_t nsoption_output_value_file(struct nsoption_s *option, void *ctx)
nsoption_output(FILE *fp,
struct nsoption_s *opts,
struct nsoption_s *defs,
bool all)
{ {
unsigned int entry; /* index to option being output */ FILE *fp = ctx;
size_t slen = 0; /* length added to stream */
colour rgbcolour; /* RRGGBB */ colour rgbcolour; /* RRGGBB */
for (entry = 0; entry < NSOPTION_LISTEND; entry++) { switch (option->type) {
if ((all == false) &&
(nsoption_is_set(opts, defs, entry) == false)) {
continue;
}
switch (opts[entry].type) {
case OPTION_BOOL: case OPTION_BOOL:
fprintf(fp, "%s:%c\n", slen = fprintf(fp, "%s:%c\n", option->key, option->value.b ? '1' : '0');
opts[entry].key,
opts[entry].value.b ? '1' : '0');
break; break;
case OPTION_INTEGER: case OPTION_INTEGER:
fprintf(fp, "%s:%i\n", slen = fprintf(fp, "%s:%i\n", option->key, option->value.i);
opts[entry].key,
opts[entry].value.i);
break; break;
case OPTION_UINT: case OPTION_UINT:
fprintf(fp, "%s:%u\n", slen = fprintf(fp, "%s:%u\n", option->key, option->value.u);
opts[entry].key,
opts[entry].value.u);
break; break;
case OPTION_COLOUR: case OPTION_COLOUR:
rgbcolour = (((0x000000FF & opts[entry].value.c) << 16) | rgbcolour = (((0x000000FF & option->value.c) << 16) |
((0x0000FF00 & opts[entry].value.c) << 0) | ((0x0000FF00 & option->value.c) << 0) |
((0x00FF0000 & opts[entry].value.c) >> 16)); ((0x00FF0000 & option->value.c) >> 16));
fprintf(fp, "%s:%06"PRIx32"\n", slen = fprintf(fp, "%s:%06"PRIx32"\n", option->key, rgbcolour);
opts[entry].key,
rgbcolour);
break; break;
case OPTION_STRING: case OPTION_STRING:
fprintf(fp, "%s:%s\n", slen = fprintf(fp, "%s:%s\n",
opts[entry].key, option->key,
((opts[entry].value.s == NULL) || ((option->value.s == NULL) ||
(*opts[entry].value.s == 0)) ? "" : opts[entry].value.s); (*option->value.s == 0)) ? "" : option->value.s);
break; break;
} }
}
return NSERROR_OK; return slen;
} }
/** /**
@ -758,7 +737,51 @@ nsoption_read(const char *path, struct nsoption_s *opts)
} }
/* exported interface documented in utils/nsoption.h */ /*
* Generate options via callback.
*
* exported interface documented in utils/nsoption.h
*/
nserror
nsoption_generate(nsoption_generate_cb *generate_cb,
void *generate_ctx,
enum nsoption_generate_flags flags,
struct nsoption_s *opts,
struct nsoption_s *defs)
{
unsigned int entry; /* index to option being output */
/* check to see if global table selected */
if (opts == NULL) {
opts = nsoptions;
}
/* check to see if global table selected */
if (defs == NULL) {
defs = nsoptions_default;
}
if ((opts == NULL) || (defs == NULL)) {
return NSERROR_BAD_PARAMETER;
}
for (entry = 0; entry < NSOPTION_LISTEND; entry++) {
if (((flags & NSOPTION_GENERATE_CHANGED) != 0) &&
(nsoption_is_set(opts, defs, entry) == false)) {
continue;
}
generate_cb(opts + entry, generate_ctx);
}
return NSERROR_OK;
}
/*
* Write options that have changed from the defaults to a file.
*
* exported interface documented in utils/nsoption.h
*/
nserror nserror
nsoption_write(const char *path, nsoption_write(const char *path,
struct nsoption_s *opts, struct nsoption_s *opts,
@ -792,13 +815,18 @@ nsoption_write(const char *path,
return NSERROR_NOT_FOUND; return NSERROR_NOT_FOUND;
} }
ret = nsoption_output(fp, opts, defs, false); ret = nsoption_generate(nsoption_output_value_file,
fp,
NSOPTION_GENERATE_CHANGED,
opts,
defs);
fclose(fp); fclose(fp);
return ret; return ret;
} }
/* exported interface documented in utils/nsoption.h */ /* exported interface documented in utils/nsoption.h */
nserror nserror
nsoption_dump(FILE *outf, struct nsoption_s *opts) nsoption_dump(FILE *outf, struct nsoption_s *opts)
@ -815,7 +843,11 @@ nsoption_dump(FILE *outf, struct nsoption_s *opts)
return NSERROR_BAD_PARAMETER; return NSERROR_BAD_PARAMETER;
} }
return nsoption_output(outf, opts, NULL, true); return nsoption_generate(nsoption_output_value_file,
outf,
NSOPTION_GENERATE_ALL,
opts,
nsoptions_default);
} }

View File

@ -161,17 +161,36 @@ enum nsoption_e {
*/ */
extern struct nsoption_s *nsoptions; extern struct nsoption_s *nsoptions;
/** /**
* global default option table. * global default option table.
*/ */
extern struct nsoption_s *nsoptions_default; extern struct nsoption_s *nsoptions_default;
/** /**
* default setting callback. * default setting callback.
*/ */
typedef nserror(nsoption_set_default_t)(struct nsoption_s *defaults); typedef nserror(nsoption_set_default_t)(struct nsoption_s *defaults);
/**
* option generate callback
*/
typedef size_t(nsoption_generate_cb)(struct nsoption_s *option, void *ctx);
/**
* flags to control option output in the generate call
*/
enum nsoption_generate_flags {
/** Generate output for all options */
NSOPTION_GENERATE_ALL = 0,
/** Generate output for options which differ from the default */
NSOPTION_GENERATE_CHANGED = 1,
};
/** /**
* Initialise option system. * Initialise option system.
* *
@ -206,6 +225,21 @@ nserror nsoption_finalise(struct nsoption_s *opts, struct nsoption_s *defs);
nserror nsoption_read(const char *path, struct nsoption_s *opts); nserror nsoption_read(const char *path, struct nsoption_s *opts);
/**
* Generate options via acallback.
*
* iterates options controlled by flags calling a method for each matched option.
*
* @param cb Function called for each option to be output.
* @param ctx The context for the callback.
* @param flags Flags controlling option matching.
* @param opts The options table to enerate values from or NULL to use global.
* @param defs The default table to use or NULL to use global.
* @return The error status.
*/
nserror nsoption_generate(nsoption_generate_cb *cb, void *ctx, enum nsoption_generate_flags flags, struct nsoption_s *opts, struct nsoption_s *defs);
/** /**
* Write options that have changed from the defaults to a file. * Write options that have changed from the defaults to a file.
* *