A tool for adding anime to your anidb list.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7KB

  1. #ifndef _CONFIG_H
  2. #define _CONFIG_H
  3. #include <inttypes.h>
  4. #include <stdbool.h>
  5. #include "error.h"
  6. #ifndef CONFIG_DIR_NAME
  7. #define CONFIG_DIR_NAME "caniadd"
  8. #endif
  9. enum option_type {
  10. OTYPE_S, /* Stores a string */
  11. OTYPE_HU, /* Stores an unsigned short */
  12. OTYPE_B, /* Stores a boolean */
  13. /* Does not store anything, does an action. Handled after every
  14. * other option are parsed, and defaults set */
  15. OTYPE_ACTION,
  16. _OTYPE_COUNT
  17. };
  18. struct conf_entry {
  19. const char *l_name; /* The long name for the option, or for the config file */
  20. int s_name; /* Short option name */
  21. union { /* Value of the param */
  22. char *s;
  23. uint16_t hu;
  24. bool b;
  25. } value;
  26. /* The function to use to init a default value, if it's a complex one */
  27. int (*default_func)(struct conf_entry *ce);
  28. union {
  29. /* The function to use to set the value of the arg from the
  30. * command line or from the loaded config file */
  31. int (*set_func)(struct conf_entry *ce, char *arg);
  32. /* Callback for an action option */
  33. int (*action_func)(struct conf_entry *ce);
  34. };
  35. int has_arg : 4; /* Do we need to specify an argument for this option on the cmd line? */
  36. /* Did we set the value? If not, we may need to call the default func */
  37. bool value_is_set : 1;
  38. /* Is the value required? */
  39. bool required : 1;
  40. bool value_is_dyn : 1; /* Do we need to free the value? */
  41. bool in_file : 1; /* Is this option in the config file? */
  42. bool in_args : 1; /* Is this option in the argument list? */
  43. enum option_type type : 4; /* Type of the option's value */
  44. /*
  45. * In which step do we handle this arg?
  46. * We need this, because:
  47. * 1. Read in the base dir option from the command line, if present
  48. * 2. Use the base dir to load the options from the config file
  49. * 3. Read, and override options from the command line
  50. * 4. Execute action arguments
  51. */
  52. int handle_order : 4;
  53. };
  54. /*
  55. * Parse options from the command line
  56. *
  57. * Returns 0 on success.
  58. */
  59. enum error config_parse(int argc, char **argv);
  60. /*
  61. * Free any memory that may have been allocated
  62. * in config_parse
  63. */
  64. int config_free();
  65. /*
  66. * Write out the options to stdout
  67. */
  68. void config_dump();
  69. /*
  70. * Get a config option by its long name
  71. * The pointer to the options value will be stored, at the pointer pointed to
  72. * by out
  73. * If the option is unset, it returns ERR_OPT_UNSET and out is unchanged
  74. * It the options is not found, it returns ERR_OPT_NOTFOUND, and out is unchanged
  75. */
  76. enum error config_get(const char *key, void **out);
  77. /*
  78. * Return an cmd line that is not an option
  79. * or null if error, or out of elements
  80. */
  81. const char *config_get_nonopt(int index);
  82. int config_get_nonopt_count();
  83. #endif /* _CONFIG_H */