-identify output: use ordered chapters length for ID_LENGTH
[mplayer/glamo.git] / m_option.h
blob3387193db4e69029761d18f4b501e78ffab3f5db
1 #ifndef MPLAYER_M_OPTION_H
2 #define MPLAYER_M_OPTION_H
4 #include <string.h>
5 #include <stddef.h>
7 /// \defgroup Options
8 /// m_option allows to parse, print and copy data of various types.
9 /// It is the base of the \ref OptionsStruct, \ref Config and
10 /// \ref Properties APIs.
11 ///@{
13 /// \file m_option.h
15 /// \ingroup OptionTypes
16 typedef struct m_option_type m_option_type_t;
17 typedef struct m_option m_option_t;
18 struct m_struct_st;
20 /// \defgroup OptionTypes Options types
21 /// \ingroup Options
22 ///@{
24 ///////////////////////////// Options types declarations ////////////////////////////
26 // Simple types
27 extern const m_option_type_t m_option_type_flag;
28 extern const m_option_type_t m_option_type_int;
29 extern const m_option_type_t m_option_type_int64;
30 extern const m_option_type_t m_option_type_float;
31 extern const m_option_type_t m_option_type_double;
32 extern const m_option_type_t m_option_type_string;
33 extern const m_option_type_t m_option_type_string_list;
34 extern const m_option_type_t m_option_type_position;
35 extern const m_option_type_t m_option_type_time;
36 extern const m_option_type_t m_option_type_time_size;
38 extern const m_option_type_t m_option_type_print;
39 extern const m_option_type_t m_option_type_print_indirect;
40 extern const m_option_type_t m_option_type_print_func;
41 extern const m_option_type_t m_option_type_subconfig;
42 extern const m_option_type_t m_option_type_imgfmt;
43 extern const m_option_type_t m_option_type_afmt;
45 // Func-based types
46 extern const m_option_type_t m_option_type_func_full;
47 extern const m_option_type_t m_option_type_func_param;
48 extern const m_option_type_t m_option_type_func;
50 /// Callback used to reset func options.
51 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
53 /// Callback used by m_option_type_func_full options.
54 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, char *);
56 /// Callback used by m_option_type_func_param options.
57 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
59 /// Callback used by m_option_type_func options.
60 typedef int (*m_opt_func_t)(const m_option_t *);
62 // Backwards compatibility
63 typedef m_opt_default_func_t cfg_default_func_t;
64 typedef m_opt_func_full_t cfg_func_arg_param_t;
65 typedef m_opt_func_param_t cfg_func_param_t;
66 typedef m_opt_func_t cfg_func_t;
68 #define END_AT_NONE 0
69 #define END_AT_TIME 1
70 #define END_AT_SIZE 2
71 typedef struct {
72 double pos;
73 int type;
74 } m_time_size_t;
76 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
77 typedef struct {
78 /// Pointer to an array of pointer to some object type description struct.
79 void** list;
80 /// Offset of the object type name (char*) in the description struct.
81 void* name_off;
82 /// Offset of the object type info string (char*) in the description struct.
83 void* info_off;
84 /// \brief Offset of the object type parameter description (\ref m_struct_st)
85 /// in the description struct.
86 void* desc_off;
87 } m_obj_list_t;
89 /// The data type used by \ref m_option_type_obj_settings_list.
90 typedef struct m_obj_settings {
91 /// Type of the object.
92 char* name;
93 /// NULL terminated array of parameter/value pairs.
94 char** attribs;
95 } m_obj_settings_t;
97 /// A parser to set up a list of objects.
98 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
99 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
100 * the available object types.
102 extern const m_option_type_t m_option_type_obj_settings_list;
104 /// Extra definition needed for \ref m_option_type_obj_presets options.
105 typedef struct {
106 /// Description of the struct holding the presets.
107 struct m_struct_st* in_desc;
108 /// Description of the struct that should be set by the presets.
109 struct m_struct_st* out_desc;
110 /// Pointer to an array of structs defining the various presets.
111 void* presets;
112 /// Offset of the preset's name inside the in_struct.
113 void* name_off;
114 } m_obj_presets_t;
116 /// Set several fields in a struct at once.
117 /** For this two struct descriptions are used. One for the struct holding the
118 * preset and one for the struct beeing set. Every field present in both
119 * structs will be copied from the preset struct to the destination one.
120 * The option priv field (\ref m_option::priv) must point to a correctly
121 * filled \ref m_obj_presets_t.
123 extern const m_option_type_t m_option_type_obj_presets;
125 /// Parse an URL into a struct.
126 /** The option priv field (\ref m_option::priv) must point to a
127 * \ref m_struct_st describing which fields of the URL must be used.
129 extern const m_option_type_t m_option_type_custom_url;
131 /// Extra definition needed for \ref m_option_type_obj_params options.
132 typedef struct {
133 /// Field descriptions.
134 const struct m_struct_st* desc;
135 /// Field separator to use.
136 char separator;
137 } m_obj_params_t;
139 /// Parse a set of parameters.
140 /** Parameters are separated by the given separator and each one
141 * successively sets a field from the struct. The option priv field
142 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
144 extern const m_option_type_t m_option_type_obj_params;
146 typedef struct {
147 int start;
148 int end;
149 } m_span_t;
150 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
151 extern const m_obj_params_t m_span_params_def;
154 // FIXME: backward compatibility
155 #define CONF_TYPE_FLAG (&m_option_type_flag)
156 #define CONF_TYPE_INT (&m_option_type_int)
157 #define CONF_TYPE_INT64 (&m_option_type_int64)
158 #define CONF_TYPE_FLOAT (&m_option_type_float)
159 #define CONF_TYPE_DOUBLE (&m_option_type_double)
160 #define CONF_TYPE_STRING (&m_option_type_string)
161 #define CONF_TYPE_FUNC (&m_option_type_func)
162 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
163 #define CONF_TYPE_PRINT (&m_option_type_print)
164 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
165 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
166 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
167 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
168 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
169 #define CONF_TYPE_POSITION (&m_option_type_position)
170 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
171 #define CONF_TYPE_AFMT (&m_option_type_afmt)
172 #define CONF_TYPE_SPAN (&m_option_type_span)
173 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
174 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
175 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
176 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
177 #define CONF_TYPE_TIME (&m_option_type_time)
178 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
180 /////////////////////////////////////////////////////////////////////////////////////////////
182 /// Option type description
183 struct m_option_type {
184 char* name;
185 /// Syntax description, etc
186 char* comments;
187 /// Size needed for the data.
188 unsigned int size;
189 /// See \ref OptionTypeFlags.
190 unsigned int flags;
192 /// Parse the data from a string.
193 /** It is the only required function, all others can be NULL.
195 * \param opt The option that is parsed.
196 * \param name The full option name.
197 * \param param The parameter to parse.
198 * \param dst Pointer to the memory where the data should be written.
199 * If NULL the parameter validity should still be checked.
200 * \param src Source of the option, see \ref OptionParserModes.
201 * \return On error a negative value is returned, on success the number of arguments
202 * consumed. For details see \ref OptionParserReturn.
204 int (*parse)(const m_option_t* opt,const char *name, char *param, void* dst, int src);
206 /// Print back a value in string form.
207 /** \param opt The option to print.
208 * \param val Pointer to the memory holding the data to be printed.
209 * \return An allocated string containing the text value or (void*)-1
210 * on error.
212 char* (*print)(const m_option_t* opt, const void* val);
214 /** \name
215 * These functions are called to save/set/restore the status of the
216 * variables. The difference between the 3 only matters for types like
217 * \ref m_option_type_func where 'setting' needs to do more than just
218 * copying some data.
220 //@{
222 /// Update a save slot (dst) from the current value in the program (src).
223 /** \param opt The option to copy.
224 * \param dst Pointer to the destination memory.
225 * \param src Pointer to the source memory.
227 void (*save)(const m_option_t* opt,void* dst, void* src);
229 /// Set the value in the program (dst) from a save slot.
230 /** \param opt The option to copy.
231 * \param dst Pointer to the destination memory.
232 * \param src Pointer to the source memory.
234 void (*set)(const m_option_t* opt,void* dst, void* src);
236 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
237 /** \param opt The option to copy.
238 * \param dst Pointer to the destination memory.
239 * \param src Pointer to the source memory.
241 void (*copy)(const m_option_t* opt,void* dst, void* src);
242 //@}
244 /// Free the data allocated for a save slot.
245 /** This is only needed for dynamic types like strings.
246 * \param dst Pointer to the data, usually a pointer that should be freed and
247 * set to NULL.
249 void (*free)(void* dst);
252 ///@}
254 /// Option description
255 /** \ingroup Options
257 struct m_option {
258 /// Option name.
259 const char *name;
261 /// Reserved for higher level APIs, it shouldn't be used by parsers.
262 /** The suboption parser and func types do use it. They should instead
263 * use the priv field but this was inherited from older versions of the
264 * config code.
266 void *p;
268 /// Option type.
269 const m_option_type_t* type;
271 /// See \ref OptionFlags.
272 unsigned int flags;
274 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
275 /// also be set.
276 double min;
278 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
279 /// also be set.
280 double max;
282 /// Type dependent data (for all kinds of extended settings).
283 /** This used to be a function pointer to hold a 'reverse to defaults' func.
284 * Now it can be used to pass any type of extra args needed by the parser.
285 * Passing a 'default func' is still valid for all func based option types.
287 void* priv;
289 int new;
291 int offset;
295 /// \defgroup OptionFlags Option flags
296 ///@{
298 /// The option has a minimum set in \ref m_option::min.
299 #define M_OPT_MIN (1<<0)
301 /// The option has a maximum set in \ref m_option::max.
302 #define M_OPT_MAX (1<<1)
304 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
305 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
307 /// The option is forbidden in config files.
308 #define M_OPT_NOCFG (1<<2)
310 /// The option is forbidden on the command line.
311 #define M_OPT_NOCMD (1<<3)
313 /// The option is global in the \ref Config.
314 /** It won't be saved on push and the command line parser will set it when
315 * it's parsed (i.e. it won't be set later)
316 * e.g options : -v, -quiet
318 #define M_OPT_GLOBAL (1<<4)
320 /// The \ref Config won't save this option on push.
321 /** It won't be saved on push but the command line parser will add it with
322 * its entry (i.e. it may be set later)
323 * e.g options : -include
325 #define M_OPT_NOSAVE (1<<5)
327 /// \brief The \ref Config will emulate the old behavior by pushing the
328 /// option only if it was set by the user.
329 #define M_OPT_OLD (1<<6)
331 /// The option should be set during command line pre-parsing
332 #define M_OPT_PRE_PARSE (1<<7)
334 /// \defgroup OldOptionFlags Backward compatibility
336 /// These are kept for compatibility with older code.
337 /// @{
338 #define CONF_MIN M_OPT_MIN
339 #define CONF_MAX M_OPT_MAX
340 #define CONF_RANGE M_OPT_RANGE
341 #define CONF_NOCFG M_OPT_NOCFG
342 #define CONF_NOCMD M_OPT_NOCMD
343 #define CONF_GLOBAL M_OPT_GLOBAL
344 #define CONF_NOSAVE M_OPT_NOSAVE
345 #define CONF_OLD M_OPT_OLD
346 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
347 ///@}
349 ///@}
351 /// \defgroup OptionTypeFlags Option type flags
352 /// \ingroup OptionTypes
354 /// These flags are used to describe special parser capabilities or behavior.
356 ///@{
358 /// Suboption parser flag.
359 /** When this flag is set, m_option::p should point to another m_option
360 * array. Only the parse function will be called. If dst is set, it should
361 * create/update an array of char* containg opt/val pairs. The options in
362 * the child array will then be set automatically by the \ref Config.
363 * Also note that suboptions may be directly accessed by using
364 * -option:subopt blah.
366 #define M_OPT_TYPE_HAS_CHILD (1<<0)
368 /// Wildcard matching flag.
369 /** If set the option type has a use for option names ending with a *
370 * (used for -aa*), this only affects the option name matching.
372 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
374 /// Dynamic data type.
375 /** This flag indicates that the data is dynamically allocated (m_option::p
376 * points to a pointer). It enables a little hack in the \ref Config wich
377 * replaces the initial value of such variables with a dynamic copy in case
378 * the initial value is statically allocated (pretty common with strings).
380 #define M_OPT_TYPE_DYNAMIC (1<<2)
382 /// Indirect option type.
383 /** If this is set the parse function doesn't directly return
384 * the wanted thing. Options use this if for some reasons they have to wait
385 * until the set call to be able to correctly set the target var.
386 * So for those types new values must first be parsed, then set to the target
387 * var. If this flag isn't set then new values can be parsed directly to the
388 * target var. It's used by the callback-based options as the callback call
389 * may append later on.
391 #define M_OPT_TYPE_INDIRECT (1<<3)
393 ///@}
395 ///////////////////////////// Parser flags ////////////////////////////////////////
397 /// \defgroup OptionParserModes Option parser modes
398 /// \ingroup Options
400 /// Some parsers behave differently depending on the mode passed in the src
401 /// parameter of m_option_type::parse. For example the flag type doesn't take
402 /// an argument when parsing from the command line.
403 ///@{
405 /// Set when parsing from a config file.
406 #define M_CONFIG_FILE 0
407 /// Set when parsing command line arguments.
408 #define M_COMMAND_LINE 1
409 /// Set when pre-parsing the command line
410 #define M_COMMAND_LINE_PRE_PARSE 2
412 ///@}
414 /// \defgroup OptionParserReturn Option parser return code
415 /// \ingroup Options
417 /// On success parsers return the number of arguments consumed: 0 or 1.
419 /// To indicate that MPlayer should exit without playing anything,
420 /// parsers return M_OPT_EXIT minus the number of parameters they
421 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
423 /// On error one of the following (negative) error codes is returned:
424 ///@{
426 /// For use by higher level APIs when the option name is invalid.
427 #define M_OPT_UNKNOWN -1
429 /// Returned when a parameter is needed but wasn't provided.
430 #define M_OPT_MISSING_PARAM -2
432 /// Returned when the given parameter couldn't be parsed.
433 #define M_OPT_INVALID -3
435 /// \brief Returned if the value is "out of range". The exact meaning may
436 /// vary from type to type.
437 #define M_OPT_OUT_OF_RANGE -4
439 /// Returned if the parser failed for any other reason than a bad parameter.
440 #define M_OPT_PARSER_ERR -5
442 /// Returned when MPlayer should exit. Used by various help stuff.
443 /** M_OPT_EXIT must be the lowest number on this list.
445 #define M_OPT_EXIT -6
447 /// \defgroup OldOptionParserReturn Backward compatibility
449 /// These are kept for compatibility with older code.
451 ///@{
452 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
453 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
454 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
455 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
456 ///@}
458 ///@}
460 /// Find the option matching the given name in the list.
461 /** \ingroup Options
462 * This function takes the possible wildcards into account (see
463 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
465 * \param list Pointer to an array of \ref m_option.
466 * \param name Name of the option.
467 * \return The matching option or NULL.
469 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
471 /// Helper to parse options, see \ref m_option_type::parse.
472 inline static int
473 m_option_parse(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
474 return opt->type->parse(opt,name,param,dst,src);
477 /// Helper to print options, see \ref m_option_type::print.
478 inline static char*
479 m_option_print(const m_option_t* opt, const void* val_ptr) {
480 if(opt->type->print)
481 return opt->type->print(opt,val_ptr);
482 else
483 return (char*)-1;
486 /// Helper around \ref m_option_type::copy.
487 inline static void
488 m_option_copy(const m_option_t* opt,void* dst, void* src) {
489 if(opt->type->copy)
490 opt->type->copy(opt,dst,src);
491 else if(opt->type->size > 0)
492 memcpy(dst,src,opt->type->size);
495 /// Helper around \ref m_option_type::free.
496 inline static void
497 m_option_free(const m_option_t* opt,void* dst) {
498 if(opt->type->free)
499 opt->type->free(dst);
502 /*@}*/
504 #define OPT_FLAG_ON(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 0, 1, NULL, 1, offsetof(struct MPOpts, varname)}
505 #define OPT_FLAG_OFF(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 1, 0, NULL, 1, offsetof(struct MPOpts, varname)}
506 #define OPT_FLAG_CONSTANTS(optname, varname, flags, offvalue, value) {optname, NULL, &m_option_type_flag, flags, offvalue, value, NULL, 1, offsetof(struct MPOpts, varname)}
507 #define OPT_STRINGLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
508 #define OPT_INT(optname, varname, flags) {optname, NULL, &m_option_type_int, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
509 #define OPT_INTRANGE(optname, varname, flags, min, max) {optname, NULL, &m_option_type_int, (flags)|CONF_RANGE, min, max, NULL, 1, offsetof(struct MPOpts, varname)}
510 #define OPT_FLOATRANGE(optname, varname, flags, min, max) {optname, NULL, &m_option_type_float, (flags)|CONF_RANGE, min, max, NULL, 1, offsetof(struct MPOpts, varname)}
511 #define OPT_STRING(optname, varname, flags) {optname, NULL, &m_option_type_string, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
512 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) {optname, NULL, &m_option_type_obj_settings_list, flags, 0, 0, objlist, 1, offsetof(struct MPOpts, varname)}
514 #endif /* MPLAYER_M_OPTION_H */