remove useless casts
[mplayer/glamo.git] / m_option.h
blob7c5d2efa91eb8fc0059fd19c04b28e6ce84984de
1 #ifndef M_OPTION_H
2 #define M_OPTION_H
4 /// \defgroup Options
5 /// m_option allows to parse, print and copy data of various types.
6 /// It is the base of the \ref OptionsStruct, \ref Config and
7 /// \ref Properties APIs.
8 ///@{
10 /// \file m_option.h
12 /// \ingroup OptionTypes
13 typedef struct m_option_type m_option_type_t;
14 typedef struct m_option m_option_t;
15 struct m_struct_st;
17 /// \defgroup OptionTypes Options types
18 /// \ingroup Options
19 ///@{
21 ///////////////////////////// Options types declarations ////////////////////////////
23 // Simple types
24 extern const m_option_type_t m_option_type_flag;
25 extern const m_option_type_t m_option_type_int;
26 extern const m_option_type_t m_option_type_float;
27 extern const m_option_type_t m_option_type_double;
28 extern const m_option_type_t m_option_type_string;
29 extern const m_option_type_t m_option_type_string_list;
30 extern const m_option_type_t m_option_type_position;
31 extern const m_option_type_t m_option_type_time;
32 extern const m_option_type_t m_option_type_time_size;
34 extern const m_option_type_t m_option_type_print;
35 extern const m_option_type_t m_option_type_print_indirect;
36 extern const m_option_type_t m_option_type_print_func;
37 extern const m_option_type_t m_option_type_subconfig;
38 extern const m_option_type_t m_option_type_imgfmt;
39 extern const m_option_type_t m_option_type_afmt;
41 // Func-based types
42 extern const m_option_type_t m_option_type_func_full;
43 extern const m_option_type_t m_option_type_func_param;
44 extern const m_option_type_t m_option_type_func;
46 /// Callback used to reset func options.
47 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
49 /// Callback used by m_option_type_func_full options.
50 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, char *);
52 /// Callback used by m_option_type_func_param options.
53 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
55 /// Callback used by m_option_type_func options.
56 typedef int (*m_opt_func_t)(const m_option_t *);
58 // Backwards compatibility
59 typedef m_opt_default_func_t cfg_default_func_t;
60 typedef m_opt_func_full_t cfg_func_arg_param_t;
61 typedef m_opt_func_param_t cfg_func_param_t;
62 typedef m_opt_func_t cfg_func_t;
64 #define END_AT_NONE 0
65 #define END_AT_TIME 1
66 #define END_AT_SIZE 2
67 typedef struct {
68 double pos;
69 int type;
70 } m_time_size_t;
72 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
73 typedef struct {
74 /// Pointer to an array of pointer to some object type description struct.
75 void** list;
76 /// Offset of the object type name (char*) in the description struct.
77 void* name_off;
78 /// Offset of the object type info string (char*) in the description struct.
79 void* info_off;
80 /// \brief Offset of the object type parameter description (\ref m_struct_st)
81 /// in the description struct.
82 void* desc_off;
83 } m_obj_list_t;
85 /// The data type used by \ref m_option_type_obj_settings_list.
86 typedef struct m_obj_settings {
87 /// Type of the object.
88 char* name;
89 /// NULL terminated array of parameter/value pairs.
90 char** attribs;
91 } m_obj_settings_t;
93 /// A parser to set up a list of objects.
94 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
95 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
96 * the available object types.
98 extern const m_option_type_t m_option_type_obj_settings_list;
100 /// Extra definition needed for \ref m_option_type_obj_presets options.
101 typedef struct {
102 /// Description of the struct holding the presets.
103 struct m_struct_st* in_desc;
104 /// Description of the struct that should be set by the presets.
105 struct m_struct_st* out_desc;
106 /// Pointer to an array of structs defining the various presets.
107 void* presets;
108 /// Offset of the preset's name inside the in_struct.
109 void* name_off;
110 } m_obj_presets_t;
112 /// Set several fields in a struct at once.
113 /** For this two struct descriptions are used. One for the struct holding the
114 * preset and one for the struct beeing set. Every field present in both
115 * structs will be copied from the preset struct to the destination one.
116 * The option priv field (\ref m_option::priv) must point to a correctly
117 * filled \ref m_obj_presets_t.
119 extern const m_option_type_t m_option_type_obj_presets;
121 /// Parse an URL into a struct.
122 /** The option priv field (\ref m_option::priv) must point to a
123 * \ref m_struct_st describing which fields of the URL must be used.
125 extern const m_option_type_t m_option_type_custom_url;
127 /// Extra definition needed for \ref m_option_type_obj_params options.
128 typedef struct {
129 /// Field descriptions.
130 const struct m_struct_st* desc;
131 /// Field separator to use.
132 char separator;
133 } m_obj_params_t;
135 /// Parse a set of parameters.
136 /** Parameters are separated by the given separator and each one
137 * successively sets a field from the struct. The option priv field
138 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
140 extern const m_option_type_t m_option_type_obj_params;
142 typedef struct {
143 int start;
144 int end;
145 } m_span_t;
146 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
147 extern const m_obj_params_t m_span_params_def;
150 // FIXME: backward compatibility
151 #define CONF_TYPE_FLAG (&m_option_type_flag)
152 #define CONF_TYPE_INT (&m_option_type_int)
153 #define CONF_TYPE_FLOAT (&m_option_type_float)
154 #define CONF_TYPE_DOUBLE (&m_option_type_double)
155 #define CONF_TYPE_STRING (&m_option_type_string)
156 #define CONF_TYPE_FUNC (&m_option_type_func)
157 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
158 #define CONF_TYPE_PRINT (&m_option_type_print)
159 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
160 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
161 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
162 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
163 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
164 #define CONF_TYPE_POSITION (&m_option_type_position)
165 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
166 #define CONF_TYPE_AFMT (&m_option_type_afmt)
167 #define CONF_TYPE_SPAN (&m_option_type_span)
168 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
169 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
170 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
171 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
172 #define CONF_TYPE_TIME (&m_option_type_time)
173 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
175 /////////////////////////////////////////////////////////////////////////////////////////////
177 /// Option type description
178 struct m_option_type {
179 char* name;
180 /// Syntax description, etc
181 char* comments;
182 /// Size needed for the data.
183 unsigned int size;
184 /// See \ref OptionTypeFlags.
185 unsigned int flags;
187 /// Parse the data from a string.
188 /** It is the only required function, all others can be NULL.
190 * \param opt The option that is parsed.
191 * \param name The full option name.
192 * \param param The parameter to parse.
193 * \param dst Pointer to the memory where the data should be written.
194 * If NULL the parameter validity should still be checked.
195 * \param src Source of the option, see \ref OptionParserModes.
196 * \return On error a negative value is returned, on success the number of arguments
197 * consumed. For details see \ref OptionParserReturn.
199 int (*parse)(const m_option_t* opt,const char *name, char *param, void* dst, int src);
201 /// Print back a value in string form.
202 /** \param opt The option to print.
203 * \param val Pointer to the memory holding the data to be printed.
204 * \return An allocated string containing the text value or (void*)-1
205 * on error.
207 char* (*print)(const m_option_t* opt, const void* val);
209 /** \name
210 * These functions are called to save/set/restore the status of the
211 * variables. The difference between the 3 only matters for types like
212 * \ref m_option_type_func where 'setting' needs to do more than just
213 * copying some data.
215 //@{
217 /// Update a save slot (dst) from the current value in the program (src).
218 /** \param opt The option to copy.
219 * \param dst Pointer to the destination memory.
220 * \param src Pointer to the source memory.
222 void (*save)(const m_option_t* opt,void* dst, void* src);
224 /// Set the value in the program (dst) from a save slot.
225 /** \param opt The option to copy.
226 * \param dst Pointer to the destination memory.
227 * \param src Pointer to the source memory.
229 void (*set)(const m_option_t* opt,void* dst, void* src);
231 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
232 /** \param opt The option to copy.
233 * \param dst Pointer to the destination memory.
234 * \param src Pointer to the source memory.
236 void (*copy)(const m_option_t* opt,void* dst, void* src);
237 //@}
239 /// Free the data allocated for a save slot.
240 /** This is only needed for dynamic types like strings.
241 * \param dst Pointer to the data, usually a pointer that should be freed and
242 * set to NULL.
244 void (*free)(void* dst);
247 ///@}
249 /// Option description
250 /** \ingroup Options
252 struct m_option {
253 /// Option name.
254 const char *name;
256 /// Reserved for higher level APIs, it shouldn't be used by parsers.
257 /** The suboption parser and func types do use it. They should instead
258 * use the priv field but this was inherited from older versions of the
259 * config code.
261 void *p;
263 /// Option type.
264 const m_option_type_t* type;
266 /// See \ref OptionFlags.
267 unsigned int flags;
269 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
270 /// also be set.
271 double min;
273 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
274 /// also be set.
275 double max;
277 /// Type dependent data (for all kinds of extended settings).
278 /** This used to be a function pointer to hold a 'reverse to defaults' func.
279 * Now it can be used to pass any type of extra args needed by the parser.
280 * Passing a 'default func' is still valid for all func based option types.
282 void* priv;
286 /// \defgroup OptionFlags Option flags
287 ///@{
289 /// The option has a minimum set in \ref m_option::min.
290 #define M_OPT_MIN (1<<0)
292 /// The option has a maximum set in \ref m_option::max.
293 #define M_OPT_MAX (1<<1)
295 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
296 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
298 /// The option is forbidden in config files.
299 #define M_OPT_NOCFG (1<<2)
301 /// The option is forbidden on the command line.
302 #define M_OPT_NOCMD (1<<3)
304 /// The option is global in the \ref Config.
305 /** It won't be saved on push and the command line parser will set it when
306 * it's parsed (i.e. it won't be set later)
307 * e.g options : -v, -quiet
309 #define M_OPT_GLOBAL (1<<4)
311 /// The \ref Config won't save this option on push.
312 /** It won't be saved on push but the command line parser will add it with
313 * its entry (i.e. it may be set later)
314 * e.g options : -include
316 #define M_OPT_NOSAVE (1<<5)
318 /// \brief The \ref Config will emulate the old behavior by pushing the
319 /// option only if it was set by the user.
320 #define M_OPT_OLD (1<<6)
322 /// \defgroup OldOptionFlags Backward compatibility
324 /// These are kept for compatibility with older code.
325 /// @{
326 #define CONF_MIN M_OPT_MIN
327 #define CONF_MAX M_OPT_MAX
328 #define CONF_RANGE M_OPT_RANGE
329 #define CONF_NOCFG M_OPT_NOCFG
330 #define CONF_NOCMD M_OPT_NOCMD
331 #define CONF_GLOBAL M_OPT_GLOBAL
332 #define CONF_NOSAVE M_OPT_NOSAVE
333 #define CONF_OLD M_OPT_OLD
334 ///@}
336 ///@}
338 /// \defgroup OptionTypeFlags Option type flags
339 /// \ingroup OptionTypes
341 /// These flags are used to describe special parser capabilities or behavior.
343 ///@{
345 /// Suboption parser flag.
346 /** When this flag is set, m_option::p should point to another m_option
347 * array. Only the parse function will be called. If dst is set, it should
348 * create/update an array of char* containg opt/val pairs. The options in
349 * the child array will then be set automatically by the \ref Config.
350 * Also note that suboptions may be directly accessed by using
351 * -option:subopt blah.
353 #define M_OPT_TYPE_HAS_CHILD (1<<0)
355 /// Wildcard matching flag.
356 /** If set the option type has a use for option names ending with a *
357 * (used for -aa*), this only affects the option name matching.
359 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
361 /// Dynamic data type.
362 /** This flag indicates that the data is dynamically allocated (m_option::p
363 * points to a pointer). It enables a little hack in the \ref Config wich
364 * replaces the initial value of such variables with a dynamic copy in case
365 * the initial value is statically allocated (pretty common with strings).
367 #define M_OPT_TYPE_DYNAMIC (1<<2)
369 /// Indirect option type.
370 /** If this is set the parse function doesn't directly return
371 * the wanted thing. Options use this if for some reasons they have to wait
372 * until the set call to be able to correctly set the target var.
373 * So for those types new values must first be parsed, then set to the target
374 * var. If this flag isn't set then new values can be parsed directly to the
375 * target var. It's used by the callback-based options as the callback call
376 * may append later on.
378 #define M_OPT_TYPE_INDIRECT (1<<3)
380 ///@}
382 ///////////////////////////// Parser flags ////////////////////////////////////////
384 /// \defgroup OptionParserModes Option parser modes
385 /// \ingroup Options
387 /// Some parsers behave differently depending on the mode passed in the src
388 /// parameter of m_option_type::parse. For example the flag type doesn't take
389 /// an argument when parsing from the command line.
390 ///@{
392 /// Set when parsing from a config file.
393 #define M_CONFIG_FILE 0
394 /// Set when parsing command line arguments.
395 #define M_COMMAND_LINE 1
397 ///@}
399 /// \defgroup OptionParserReturn Option parser return code
400 /// \ingroup Options
402 /// On success parsers return the number of arguments consumed: 0 or 1.
404 /// To indicate that MPlayer should exit without playing anything,
405 /// parsers return M_OPT_EXIT minus the number of parameters they
406 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
408 /// On error one of the following (negative) error codes is returned:
409 ///@{
411 /// For use by higher level APIs when the option name is invalid.
412 #define M_OPT_UNKNOWN -1
414 /// Returned when a parameter is needed but wasn't provided.
415 #define M_OPT_MISSING_PARAM -2
417 /// Returned when the given parameter couldn't be parsed.
418 #define M_OPT_INVALID -3
420 /// \brief Returned if the value is "out of range". The exact meaning may
421 /// vary from type to type.
422 #define M_OPT_OUT_OF_RANGE -4
424 /// Returned if the parser failed for any other reason than a bad parameter.
425 #define M_OPT_PARSER_ERR -5
427 /// Returned when MPlayer should exit. Used by various help stuff.
428 /** M_OPT_EXIT must be the lowest number on this list.
430 #define M_OPT_EXIT -6
432 /// \defgroup OldOptionParserReturn Backward compatibility
434 /// These are kept for compatibility with older code.
436 ///@{
437 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
438 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
439 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
440 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
441 ///@}
443 ///@}
445 /// Find the option matching the given name in the list.
446 /** \ingroup Options
447 * This function takes the possible wildcards into account (see
448 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
450 * \param list Pointer to an array of \ref m_option.
451 * \param name Name of the option.
452 * \return The matching option or NULL.
454 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
456 /// Helper to parse options, see \ref m_option_type::parse.
457 inline static int
458 m_option_parse(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
459 return opt->type->parse(opt,name,param,dst,src);
462 /// Helper to print options, see \ref m_option_type::print.
463 inline static char*
464 m_option_print(const m_option_t* opt, const void* val_ptr) {
465 if(opt->type->print)
466 return opt->type->print(opt,val_ptr);
467 else
468 return (char*)-1;
471 /// Helper around \ref m_option_type::save.
472 inline static void
473 m_option_save(const m_option_t* opt,void* dst, void* src) {
474 if(opt->type->save)
475 opt->type->save(opt,dst,src);
478 /// Helper around \ref m_option_type::set.
479 inline static void
480 m_option_set(const m_option_t* opt,void* dst, void* src) {
481 if(opt->type->set)
482 opt->type->set(opt,dst,src);
485 /// Helper around \ref m_option_type::copy.
486 inline static void
487 m_option_copy(const m_option_t* opt,void* dst, void* src) {
488 if(opt->type->copy)
489 opt->type->copy(opt,dst,src);
490 else if(opt->type->size > 0)
491 memcpy(dst,src,opt->type->size);
494 /// Helper around \ref m_option_type::free.
495 inline static void
496 m_option_free(const m_option_t* opt,void* dst) {
497 if(opt->type->free)
498 opt->type->free(dst);
501 /*@}*/
503 #endif /* M_OPTION_H */