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.

96 lines
2.8KB

  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. const char *h_desc; /* Description for the help screen */
  22. union { /* Value of the param */
  23. char *s;
  24. uint16_t hu;
  25. bool b;
  26. } value;
  27. /* The function to use to init a default value, if it's a complex one */
  28. int (*default_func)(struct conf_entry *ce);
  29. union {
  30. /* The function to use to set the value of the arg from the
  31. * command line or from the loaded config file */
  32. int (*set_func)(struct conf_entry *ce, char *arg);
  33. /* Callback for an action option */
  34. int (*action_func)(struct conf_entry *ce);
  35. };
  36. int has_arg : 4; /* Do we need to specify an argument for this option on the cmd line? */
  37. /* Did we set the value? If not, we may need to call the default func */
  38. bool value_is_set : 1;
  39. /* Is the value required? */
  40. bool required : 1;
  41. bool value_is_dyn : 1; /* Do we need to free the value? */
  42. bool in_file : 1; /* Is this option in the config file? */
  43. bool in_args : 1; /* Is this option in the argument list? */
  44. enum option_type type : 4; /* Type of the option's value */
  45. /*
  46. * In which step do we handle this arg?
  47. * We need this, because:
  48. * 1. Read in the base dir option from the command line, if present
  49. * 2. Use the base dir to load the options from the config file
  50. * 3. Read, and override options from the command line
  51. * 4. Execute action arguments
  52. */
  53. int handle_order : 4;
  54. };
  55. /*
  56. * Parse options from the command line
  57. *
  58. * Returns 0 on success.
  59. */
  60. enum error config_parse(int argc, char **argv);
  61. /*
  62. * Free any memory that may have been allocated
  63. * in config_parse
  64. */
  65. int config_free();
  66. /*
  67. * Write out the options to stdout
  68. */
  69. void config_dump();
  70. /*
  71. * Get a config option by its long name
  72. * The pointer to the options value will be stored, at the pointer pointed to
  73. * by out
  74. * If the option is unset, it returns ERR_OPT_UNSET and out is unchanged
  75. * It the options is not found, it returns ERR_OPT_NOTFOUND, and out is unchanged
  76. */
  77. enum error config_get(const char *key, void **out);
  78. /*
  79. * Return an cmd line that is not an option
  80. * or null if error, or out of elements
  81. */
  82. const char *config_get_nonopt(int index);
  83. int config_get_nonopt_count();
  84. #endif /* _CONFIG_H */