natsuki.mplayerhq.hu -> rsync.mplayerhq.hu
[mplayer/glamo.git] / m_option.h
blobf535107132da14e23b6ae3bb3dcceab8b1ff7ecb
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 m_option_type_t m_option_type_flag;
25 extern m_option_type_t m_option_type_int;
26 extern m_option_type_t m_option_type_float;
27 extern m_option_type_t m_option_type_double;
28 extern m_option_type_t m_option_type_string;
29 extern m_option_type_t m_option_type_string_list;
30 extern m_option_type_t m_option_type_position;
32 extern m_option_type_t m_option_type_print;
33 extern m_option_type_t m_option_type_print_indirect;
34 extern m_option_type_t m_option_type_print_func;
35 extern m_option_type_t m_option_type_subconfig;
36 extern m_option_type_t m_option_type_imgfmt;
37 extern m_option_type_t m_option_type_afmt;
39 // Func-based types
40 extern m_option_type_t m_option_type_func_full;
41 extern m_option_type_t m_option_type_func_param;
42 extern m_option_type_t m_option_type_func;
44 /// Callback used to reset func options.
45 typedef void (*m_opt_default_func_t)(m_option_t *, char*);
47 /// Callback used by m_option_type_func_full options.
48 typedef int (*m_opt_func_full_t)(m_option_t *, char *, char *);
50 /// Callback used by m_option_type_func_param options.
51 typedef int (*m_opt_func_param_t)(m_option_t *, char *);
53 /// Callback used by m_option_type_func options.
54 typedef int (*m_opt_func_t)(m_option_t *);
56 // Backwards compatibility
57 typedef m_opt_default_func_t cfg_default_func_t;
58 typedef m_opt_func_full_t cfg_func_arg_param_t;
59 typedef m_opt_func_param_t cfg_func_param_t;
60 typedef m_opt_func_t cfg_func_t;
62 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
63 typedef struct {
64 /// Pointer to an array of pointer to some object type description struct.
65 void** list;
66 /// Offset of the object type name (char*) in the description struct.
67 void* name_off;
68 /// Offset of the object type info string (char*) in the description struct.
69 void* info_off;
70 /// \brief Offset of the object type parameter description (\ref m_struct_st)
71 /// in the description struct.
72 void* desc_off;
73 } m_obj_list_t;
75 /// The data type used by \ref m_option_type_obj_settings_list.
76 typedef struct m_obj_settings {
77 /// Type of the object.
78 char* name;
79 /// NULL terminated array of parameter/value pairs.
80 char** attribs;
81 } m_obj_settings_t;
83 /// A parser to set up a list of objects.
84 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
85 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
86 * the available object types.
88 extern m_option_type_t m_option_type_obj_settings_list;
90 /// Extra definition needed for \ref m_option_type_obj_presets options.
91 typedef struct {
92 /// Description of the struct holding the presets.
93 struct m_struct_st* in_desc;
94 /// Description of the struct that should be set by the presets.
95 struct m_struct_st* out_desc;
96 /// Pointer to an array of structs defining the various presets.
97 void* presets;
98 /// Offset of the preset's name inside the in_struct.
99 void* name_off;
100 } m_obj_presets_t;
102 /// Set several fields in a struct at once.
103 /** For this two struct descriptions are used. One for the struct holding the
104 * preset and one for the struct beeing set. Every field present in both
105 * structs will be copied from the preset struct to the destination one.
106 * The option priv field (\ref m_option::priv) must point to a correctly
107 * filled \ref m_obj_presets_t.
109 extern m_option_type_t m_option_type_obj_presets;
111 /// Parse an URL into a struct.
112 /** The option priv field (\ref m_option::priv) must point to a
113 * \ref m_struct_st describing which fields of the URL must be used.
115 extern m_option_type_t m_option_type_custom_url;
117 /// Extra definition needed for \ref m_option_type_obj_params options.
118 typedef struct {
119 /// Field descriptions.
120 struct m_struct_st* desc;
121 /// Field separator to use.
122 char separator;
123 } m_obj_params_t;
125 /// Parse a set of parameters.
126 /** Parameters are separated by the given separator and each one
127 * successively sets a field from the struct. The option priv field
128 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
130 extern m_option_type_t m_option_type_obj_params;
132 typedef struct {
133 int start;
134 int end;
135 } m_span_t;
136 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
137 extern m_obj_params_t m_span_params_def;
140 // FIXME: backward compatibility
141 #define CONF_TYPE_FLAG (&m_option_type_flag)
142 #define CONF_TYPE_INT (&m_option_type_int)
143 #define CONF_TYPE_FLOAT (&m_option_type_float)
144 #define CONF_TYPE_DOUBLE (&m_option_type_double)
145 #define CONF_TYPE_STRING (&m_option_type_string)
146 #define CONF_TYPE_FUNC (&m_option_type_func)
147 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
148 #define CONF_TYPE_PRINT (&m_option_type_print)
149 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
150 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
151 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
152 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
153 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
154 #define CONF_TYPE_POSITION (&m_option_type_position)
155 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
156 #define CONF_TYPE_AFMT (&m_option_type_afmt)
157 #define CONF_TYPE_SPAN (&m_option_type_span)
158 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
159 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
160 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
161 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
163 /////////////////////////////////////////////////////////////////////////////////////////////
165 /// Option type description
166 struct m_option_type {
167 char* name;
168 /// Syntax description, etc
169 char* comments;
170 /// Size needed for the data.
171 unsigned int size;
172 /// See \ref OptionTypeFlags.
173 unsigned int flags;
175 /// Parse the data from a string.
176 /** It is the only required function, all others can be NULL.
178 * \param opt The option that is parsed.
179 * \param name The full option name.
180 * \param param The parameter to parse.
181 * \param dst Pointer to the memory where the data should be written.
182 * If NULL the parameter validity should still be checked.
183 * \param src Source of the option, see \ref OptionParserModes.
184 * \return On error a negative value is returned, on success the number of arguments
185 * consumed. For details see \ref OptionParserReturn.
187 int (*parse)(m_option_t* opt,char *name, char *param, void* dst, int src);
189 /// Print back a value in string form.
190 /** \param opt The option to print.
191 * \param val Pointer to the memory holding the data to be printed.
192 * \return An allocated string containing the text value or (void*)-1
193 * on error.
195 char* (*print)(m_option_t* opt, void* val);
197 /** \name
198 * These functions are called to save/set/restore the status of the
199 * variables. The difference between the 3 only matters for types like
200 * \ref m_option_type_func where 'setting' needs to do more than just
201 * copying some data.
203 //@{
205 /// Update a save slot (dst) from the current value in the program (src).
206 /** \param opt The option to copy.
207 * \param dst Pointer to the destination memory.
208 * \param src Pointer to the source memory.
210 void (*save)(m_option_t* opt,void* dst, void* src);
212 /// Set the value in the program (dst) from a save slot.
213 /** \param opt The option to copy.
214 * \param dst Pointer to the destination memory.
215 * \param src Pointer to the source memory.
217 void (*set)(m_option_t* opt,void* dst, void* src);
219 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
220 /** \param opt The option to copy.
221 * \param dst Pointer to the destination memory.
222 * \param src Pointer to the source memory.
224 void (*copy)(m_option_t* opt,void* dst, void* src);
225 //@}
227 /// Free the data allocated for a save slot.
228 /** This is only needed for dynamic types like strings.
229 * \param dst Pointer to the data, usually a pointer that should be freed and
230 * set to NULL.
232 void (*free)(void* dst);
235 ///@}
237 /// Option description
238 /** \ingroup Options
240 struct m_option {
241 /// Option name.
242 char *name;
244 /// Reserved for higher level APIs, it shouldn't be used by parsers.
245 /** The suboption parser and func types do use it. They should instead
246 * use the priv field but this was inherited from older versions of the
247 * config code.
249 void *p;
251 /// Option type.
252 m_option_type_t* type;
254 /// See \ref OptionFlags.
255 unsigned int flags;
257 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
258 /// also be set.
259 double min;
261 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
262 /// also be set.
263 double max;
265 /// Type dependent data (for all kinds of extended settings).
266 /** This used to be a function pointer to hold a 'reverse to defaults' func.
267 * Now it can be used to pass any type of extra args needed by the parser.
268 * Passing a 'default func' is still valid for all func based option types.
270 void* priv;
274 /// \defgroup OptionFlags Option flags
275 ///@{
277 /// The option has a minimum set in \ref m_option::min.
278 #define M_OPT_MIN (1<<0)
280 /// The option has a maximum set in \ref m_option::max.
281 #define M_OPT_MAX (1<<1)
283 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
284 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
286 /// The option is forbidden in config files.
287 #define M_OPT_NOCFG (1<<2)
289 /// The option is forbidden on the command line.
290 #define M_OPT_NOCMD (1<<3)
292 /// The option is global in the \ref Config.
293 /** It won't be saved on push and the command line parser will set it when
294 * it's parsed (i.e. it won't be set later)
295 * e.g options : -v, -quiet
297 #define M_OPT_GLOBAL (1<<4)
299 /// The \ref Config won't save this option on push.
300 /** It won't be saved on push but the command line parser will add it with
301 * its entry (i.e. it may be set later)
302 * e.g options : -include
304 #define M_OPT_NOSAVE (1<<5)
306 /// \brief The \ref Config will emulate the old behavior by pushing the
307 /// option only if it was set by the user.
308 #define M_OPT_OLD (1<<6)
310 /// \defgroup OldOptionFlags Backward compatibility
312 /// These are kept for compatibility with older code.
313 /// @{
314 #define CONF_MIN M_OPT_MIN
315 #define CONF_MAX M_OPT_MAX
316 #define CONF_RANGE M_OPT_RANGE
317 #define CONF_NOCFG M_OPT_NOCFG
318 #define CONF_NOCMD M_OPT_NOCMD
319 #define CONF_GLOBAL M_OPT_GLOBAL
320 #define CONF_NOSAVE M_OPT_NOSAVE
321 #define CONF_OLD M_OPT_OLD
322 ///@}
324 ///@}
326 /// \defgroup OptionTypeFlags Option type flags
327 /// \ingroup OptionTypes
329 /// These flags are used to describe special parser capabilities or behavior.
331 ///@{
333 /// Suboption parser flag.
334 /** When this flag is set, m_option::p should point to another m_option
335 * array. Only the parse function will be called. If dst is set, it should
336 * create/update an array of char* containg opt/val pairs. The options in
337 * the child array will then be set automatically by the \ref Config.
338 * Also note that suboptions may be directly accessed by using
339 * -option:subopt blah.
341 #define M_OPT_TYPE_HAS_CHILD (1<<0)
343 /// Wildcard matching flag.
344 /** If set the option type has a use for option names ending with a *
345 * (used for -aa*), this only affects the option name matching.
347 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
349 /// Dynamic data type.
350 /** This flag indicates that the data is dynamically allocated (m_option::p
351 * points to a pointer). It enables a little hack in the \ref Config wich
352 * replaces the initial value of such variables with a dynamic copy in case
353 * the initial value is statically allocated (pretty common with strings).
355 #define M_OPT_TYPE_DYNAMIC (1<<2)
357 /// Indirect option type.
358 /** If this is set the parse function doesn't directly return
359 * the wanted thing. Options use this if for some reasons they have to wait
360 * until the set call to be able to correctly set the target var.
361 * So for those types new values must first be parsed, then set to the target
362 * var. If this flag isn't set then new values can be parsed directly to the
363 * target var. It's used by the callback-based options as the callback call
364 * may append later on.
366 #define M_OPT_TYPE_INDIRECT (1<<3)
368 ///@}
370 ///////////////////////////// Parser flags ////////////////////////////////////////
372 /// \defgroup OptionParserModes Option parser modes
373 /// \ingroup Options
375 /// Some parsers behave differently depending on the mode passed in the src
376 /// parameter of m_option_type::parse. For example the flag type doesn't take
377 /// an argument when parsing from the command line.
378 ///@{
380 /// Set when parsing from a config file.
381 #define M_CONFIG_FILE 0
382 /// Set when parsing command line arguments.
383 #define M_COMMAND_LINE 1
385 ///@}
387 /// \defgroup OptionParserReturn Option parser return code
388 /// \ingroup Options
390 /// On success parsers return the number of arguments consumed: 0 or 1.
392 /// To indicate that MPlayer should exit without playing anything,
393 /// parsers return M_OPT_EXIT minus the number of parameters they
394 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
396 /// On error one of the following (negative) error codes is returned:
397 ///@{
399 /// For use by higher level APIs when the option name is invalid.
400 #define M_OPT_UNKNOWN -1
402 /// Returned when a parameter is needed but wasn't provided.
403 #define M_OPT_MISSING_PARAM -2
405 /// Returned when the given parameter couldn't be parsed.
406 #define M_OPT_INVALID -3
408 /// \brief Returned if the value is "out of range". The exact meaning may
409 /// vary from type to type.
410 #define M_OPT_OUT_OF_RANGE -4
412 /// Returned if the parser failed for any other reason than a bad parameter.
413 #define M_OPT_PARSER_ERR -5
415 /// Returned when MPlayer should exit. Used by various help stuff.
416 /** M_OPT_EXIT must be the lowest number on this list.
418 #define M_OPT_EXIT -6
420 /// \defgroup OldOptionParserReturn Backward compatibility
422 /// These are kept for compatibility with older code.
424 ///@{
425 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
426 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
427 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
428 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
429 ///@}
431 ///@}
433 /// Find the option matching the given name in the list.
434 /** \ingroup Options
435 * This function takes the possible wildcards into account (see
436 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
438 * \param list Pointer to an array of \ref m_option.
439 * \param name Name of the option.
440 * \return The matching option or NULL.
442 m_option_t* m_option_list_find(m_option_t* list,char* name);
444 /// Helper to parse options, see \ref m_option_type::parse.
445 inline static int
446 m_option_parse(m_option_t* opt,char *name, char *param, void* dst, int src) {
447 return opt->type->parse(opt,name,param,dst,src);
450 /// Helper to print options, see \ref m_option_type::print.
451 inline static char*
452 m_option_print(m_option_t* opt, void* val_ptr) {
453 if(opt->type->print)
454 return opt->type->print(opt,val_ptr);
455 else
456 return (char*)-1;
459 /// Helper around \ref m_option_type::save.
460 inline static void
461 m_option_save(m_option_t* opt,void* dst, void* src) {
462 if(opt->type->save)
463 opt->type->save(opt,dst,src);
466 /// Helper around \ref m_option_type::set.
467 inline static void
468 m_option_set(m_option_t* opt,void* dst, void* src) {
469 if(opt->type->set)
470 opt->type->set(opt,dst,src);
473 /// Helper around \ref m_option_type::copy.
474 inline static void
475 m_option_copy(m_option_t* opt,void* dst, void* src) {
476 if(opt->type->copy)
477 opt->type->copy(opt,dst,src);
478 else if(opt->type->size > 0)
479 memcpy(dst,src,opt->type->size);
482 /// Helper around \ref m_option_type::free.
483 inline static void
484 m_option_free(m_option_t* opt,void* dst) {
485 if(opt->type->free)
486 opt->type->free(dst);
489 /*@}*/
491 #endif /* _M_OPTION_H */