diff options
Diffstat (limited to 'src/lib/util/options.h')
-rw-r--r-- | src/lib/util/options.h | 336 |
1 files changed, 142 insertions, 194 deletions
diff --git a/src/lib/util/options.h b/src/lib/util/options.h index 0350b16bdb6..0ec2783dffd 100644 --- a/src/lib/util/options.h +++ b/src/lib/util/options.h @@ -42,216 +42,164 @@ #include "osdcore.h" #include "corefile.h" +#include "tagmap.h" -/*************************************************************************** - CONSTANTS -***************************************************************************/ - -/* unadorned option names */ -#define MAX_UNADORNED_OPTIONS 16 -#define OPTION_UNADORNED(x) (((x) < MAX_UNADORNED_OPTIONS) ? option_unadorned[x] : "") - -/* option flags */ -#define OPTION_BOOLEAN 0x0001 /* option is a boolean value */ -#define OPTION_DEPRECATED 0x0002 /* option is deprecated */ -#define OPTION_COMMAND 0x0004 /* option is a command */ -#define OPTION_HEADER 0x0008 /* text-only header */ -#define OPTION_INTERNAL 0x0010 /* option is internal-only */ -#define OPTION_REPEATS 0x0020 /* unadorned option repeats */ -#define OPTION_DRIVER_ONLY 0x0040 /* remove value if found in lower levels */ - -/* option priorities */ -#define OPTION_PRIORITY_DEFAULT 0 /* defaults are at 0 priority */ -#define OPTION_PRIORITY_LOW 50 /* low priority */ -#define OPTION_PRIORITY_NORMAL 100 /* normal priority */ -#define OPTION_PRIORITY_HIGH 150 /* high priority */ -#define OPTION_PRIORITY_MAXIMUM 255 /* maximum priority */ +//************************************************************************** +// CONSTANTS +//************************************************************************** +// option types +const UINT32 OPTION_TYPE_MASK = 0x0007; // up to 8 different types +enum +{ + OPTION_INVALID, // invalid + OPTION_HEADER, // a header item + OPTION_COMMAND, // a command + OPTION_BOOLEAN, // boolean option + OPTION_INTEGER, // integer option + OPTION_FLOAT, // floating-point option + OPTION_STRING // string option +}; +// option priorities +const int OPTION_PRIORITY_DEFAULT = 0; // defaults are at 0 priority +const int OPTION_PRIORITY_LOW = 50; // low priority +const int OPTION_PRIORITY_NORMAL = 100; // normal priority +const int OPTION_PRIORITY_HIGH = 150; // high priority +const int OPTION_PRIORITY_MAXIMUM = 255; // maximum priority -/*************************************************************************** - TYPE DEFINITIONS -***************************************************************************/ -/* opaque type representing a collection of options */ -typedef struct _core_options core_options; -/* opaque type use for enumeration of options */ -typedef struct _options_enumerator options_enumerator; +//************************************************************************** +// TYPE DEFINITIONS +//************************************************************************** -/* describes a single option with its description and default value */ -typedef struct _options_entry options_entry; -struct _options_entry +// static structure describing a single option with its description and default value +struct options_entry { - const char * name; /* name on the command line */ - const char * defvalue; /* default value of this argument */ - UINT32 flags; /* flags to describe the option */ - const char * description; /* description for -showusage */ + const char * name; // name on the command line + const char * defvalue; // default value of this argument + UINT32 flags; // flags to describe the option + const char * description; // description for -showusage }; -/* output messages are one of the following types */ -enum _options_message -{ - OPTMSG_INFO, - OPTMSG_WARNING, - OPTMSG_ERROR, - OPTMSG_COUNT -}; -typedef enum _options_message options_message; -/* option ranges are one of the following types */ -enum _options_range_type +// structure holding information about a collection of options +class core_options { - OPTION_RANGE_NONE, - OPTION_RANGE_INT, - OPTION_RANGE_FLOAT + static const int MAX_UNADORNED_OPTIONS = 16; + +public: + // information about a single entry in the options + class entry + { + friend class core_options; + + // construction/destruction + entry(const options_entry &entry); + + public: + // getters + entry *next() const { return m_next; } + const char *name() const { return m_name[0] ? m_name[0].cstr() : NULL; } + const char *description() const { return m_description; } + const char *value() const { return m_data; } + const char *default_value() const { return m_defdata; } + const char *minimum() const { return m_minimum; } + const char *maximum() const { return m_maximum; } + UINT32 seqid() const { return m_seqid; } + int type() const { return (m_flags & OPTION_TYPE_MASK); } + UINT32 flags() const { return m_flags; } + bool is_header() const { return type() == OPTION_HEADER; } + bool is_command() const { return type() == OPTION_COMMAND; } + bool has_range() const { return (m_minimum && m_maximum); } + + // setters + void set_value(const char *newvalue, int priority); + void set_default_value(const char *defvalue); + void revert(int priority); + + private: + // internal state + entry * m_next; // link to the next data + UINT32 m_flags; // flags from the entry + UINT32 m_seqid; // sequence ID; bumped on each change + bool m_error_reported; // have we reported an error on this option yet? + int m_priority; // priority of the data set + const char * m_description; // description for this item + astring m_name[4]; // up to 4 names for the item + astring m_data; // data for this item + astring m_defdata; // default data for this item + astring m_minimum; // minimum value + astring m_maximum; // maximum value + }; + + // construction/destruction + core_options(); + core_options(const options_entry *entrylist); + core_options(const options_entry *entrylist1, const options_entry *entrylist2); + core_options(const options_entry *entrylist1, const options_entry *entrylist2, const options_entry *entrylist3); + core_options(const core_options &src); + virtual ~core_options(); + + // operators + core_options &operator=(const core_options &rhs); + bool operator==(const core_options &rhs); + bool operator!=(const core_options &rhs); + + // getters + entry *first() const { return m_entrylist; } + const char *command() const { return m_command; } + + // configuration + void add_entries(const options_entry *entrylist, bool override_existing = false); + void set_default_value(const char *name, const char *defvalue); + void remove_entry(entry &delentry); + + // parsing/input + bool parse_command_line(int argc, char **argv, int priority, astring &error_string); + bool parse_ini_file(core_file &inifile, int priority, int ignore_priority, astring &error_string); + + // reverting + void revert(int priority = OPTION_PRIORITY_MAXIMUM); + + // output + const char *output_ini(astring &buffer, const core_options *diff = NULL); + const char *output_help(astring &buffer); + + // reading + const char *value(const char *option) const; + bool bool_value(const char *name) const { return (atoi(value(name)) != 0); } + int int_value(const char *name) const { return atoi(value(name)); } + float float_value(const char *name) const { return atof(value(name)); } + UINT32 seqid(const char *name) const; + + // setting + void set_command(const char *command); + bool set_value(const char *name, const char *value, int priority, astring &error_string); + bool set_value(const char *name, int value, int priority, astring &error_string); + bool set_value(const char *name, float value, int priority, astring &error_string); + + // misc + static const char *unadorned(int x = 0) { return s_option_unadorned[MIN(x, MAX_UNADORNED_OPTIONS)]; } + +private: + // internal helpers + void reset(); + void append_entry(entry &newentry); + void copyfrom(const core_options &src); + bool validate_and_set_data(entry &curentry, const char *newdata, int priority, astring &error_string); + + // internal state + entry * m_entrylist; // head of list of entries + entry ** m_entrylist_tailptr; // pointer to tail of entry list + tagmap_t<entry *> m_entrymap; // map for fast lookup + astring m_command; // command found + static const char *const s_option_unadorned[]; // array of unadorned option "names" }; -typedef enum _options_range_type options_range_type; - - - -/*************************************************************************** - GLOBAL VARIABLES -***************************************************************************/ - -extern const char *const option_unadorned[MAX_UNADORNED_OPTIONS]; - - - -/*************************************************************************** - FUNCTION PROTOTYPES -***************************************************************************/ - - -/* ----- options collection management ----- */ - -/* create a new collection of options */ -core_options *options_create(void (*fail)(const char *message)); - -/* free a collection of options */ -void options_free(core_options *opts); - -/* set a callback for a particular class of message */ -void options_set_output_callback(core_options *opts, options_message msgtype, void (*callback)(const char *s)); - -/* revert options at or below a certain priority back to their defaults */ -void options_revert(core_options *opts, int priority); - -/* revert options that are marked as driver only */ -void options_revert_driver_only(core_options *opts, int priority); - -/* copy one collection of options into another */ -int options_copy(core_options *dest_opts, core_options *src_opts); - -/* compare two collections of options */ -int options_equal(core_options *opts1, core_options *opts2); - - - -/* ----- option definitions ----- */ - -/* add a set of entries to an options collection */ -int options_add_entries(core_options *opts, const options_entry *entrylist); - -/* add a set of entries to an options collection, and force recreation if true */ -int options_add_entries(core_options *opts, const options_entry *entrylist, int force); - -/* set the default value for a particular option entry */ -int options_set_option_default_value(core_options *opts, const char *name, const char *defvalue); - -/* set a callback for a particular option entry */ -int options_set_option_callback(core_options *opts, const char *name, void (*callback)(core_options *opts, const char *arg)); - - - -/* ----- option data extraction ----- */ - -/* parse option data from a command line */ -int options_parse_command_line(core_options *opts, int argc, char **argv, int priority, int show_error); - -/* set option value and execute callback call */ -int options_force_option_callback(core_options *opts, const char *optionname, const char *newval, int priority); - -/* parse option data from an INI file */ -int options_parse_ini_file(core_options *opts, core_file *inifile, int priority, int ignoreprio); - - - -/* ----- options output ----- */ - -/* output option data to an INI file */ -void options_output_ini_file(core_options *opts, core_file *inifile); - -/* output differing option data to an INI file */ -void options_output_diff_ini_file(core_options *opts, core_options *baseopts, core_file *inifile); - -/* output option data to a standard file handle */ -void options_output_ini_stdfile(core_options *opts, FILE *inifile); - -/* output help using the specified output function */ -void options_output_help(core_options *opts, void (*output)(const char *s)); - - - -/* ----- options reading ----- */ - -/* read an option as a string */ -const char *options_get_string(core_options *opts, const char *name); - -/* read an option as a string with priority */ -const char *options_get_string_priority(core_options *opts, const char *name, int priority); - -/* read an option as a boolean */ -int options_get_bool(core_options *opts, const char *name); - -/* read an option as an integer */ -int options_get_int(core_options *opts, const char *name); - -/* read an option as a floating point value */ -float options_get_float(core_options *opts, const char *name); - -/* read an option as a string */ -UINT32 options_get_seqid(core_options *opts, const char *name); - - - -/* ----- options setting ----- */ - -/* set an option as a string */ -void options_set_string(core_options *opts, const char *name, const char *value, int priority); - -/* set an option as a boolean */ -void options_set_bool(core_options *opts, const char *name, int value, int priority); - -/* set an option as an integer */ -void options_set_int(core_options *opts, const char *name, int value, int priority); - -/* set an option as a floating point value */ -void options_set_float(core_options *opts, const char *name, float value, int priority); - - - -/* ----- option definition queries ----- */ - -/* begin enumerating option definitions */ -options_enumerator *options_enumerator_begin(core_options *opts); - -/* get the next option in sequence */ -const char *options_enumerator_next(options_enumerator *enumerator); - -/* free memory allocated for enumeration */ -void options_enumerator_free(options_enumerator *enumerator); - -/* get the type of range for a given option */ -options_range_type options_get_range_type(core_options *opts, const char *name); - -/* return an integer range for a given option */ -void options_get_range_int(core_options *opts, const char *name, int *minval, int *maxval); -/* return a floating point range for a given option */ -void options_get_range_float(core_options *opts, const char *name, float *minval, float *maxval); #endif /* __OPTIONS_H__ */ |