A tool for adding anime to your anidb list.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

130 lignes
3.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_SUBCOMMAND,
  17. _OTYPE_COUNT
  18. };
  19. #if 0
  20. /* This should be used to make loopups O(1), but w/e for now */
  21. enum config_global_options {
  22. OPT_HELP = 0,
  23. OPT_USERNAME,
  24. OPT_PASSWORD,
  25. OPT_PORT,
  26. OPT_API_SERVER,
  27. OPT_API_KEY,
  28. OPT_SAVE_SESSION,
  29. OPT_DESTROY_SESSION,
  30. OPT_CACHEDB,
  31. OPT_DEBUG,
  32. };
  33. enum config_subcommands {
  34. SUBCOMM_SERVER_VERSION = 0,
  35. SUBCOMM_VERSION,
  36. SUBCOMM_UPTIME,
  37. SUBCOMM_ED2k,
  38. SUBCOMM_ADD,
  39. SUBCOMM_MODIFY,
  40. };
  41. #endif
  42. struct conf_entry {
  43. const char *l_name; /* The long name for the option, or for the config file */
  44. int s_name; /* Short option name */
  45. const char *h_desc; /* Description for the help screen */
  46. const char *h_desc_more; /* More description for the help screen */
  47. union { /* Value of the param */
  48. char *s;
  49. uint16_t hu;
  50. bool b;
  51. } value;
  52. /* The function to use to init a default value, if it's a complex one */
  53. int (*default_func)(struct conf_entry *ce);
  54. union {
  55. /* The function to use to set the value of the arg from the
  56. * command line or from the loaded config file */
  57. int (*set_func)(struct conf_entry *ce, char *arg);
  58. /* Callback for an action option */
  59. int (*action_func)(struct conf_entry *ce);
  60. };
  61. int has_arg : 4; /* Do we need to specify an argument for this option on the cmd line? */
  62. /* Did we set the value? If not, we may need to call the default func */
  63. bool value_is_set : 1;
  64. /* Is the value required? */
  65. bool required : 1;
  66. bool value_is_dyn : 1; /* Do we need to free the value? */
  67. bool in_file : 1; /* Is this option in the config file? */
  68. bool in_args : 1; /* Is this option in the argument list? */
  69. enum option_type type : 4; /* Type of the option's value */
  70. /*
  71. * In which step do we handle this arg?
  72. * We need this, because:
  73. * 1. Read in the base dir option from the command line, if present
  74. * 2. Use the base dir to load the options from the config file
  75. * 3. Read, and override options from the command line
  76. * 4. Execute action arguments
  77. */
  78. int handle_order : 4;
  79. /* If type is a subcommand, this is the arguments for that subcommand */
  80. /* '-h' option is available for all of the subcommands if this is not set */
  81. int subopts_count;
  82. struct conf_entry *subopts;
  83. /* The subcommand parent of this entry, if it is inside of a subcommand */
  84. //struct conf_entry *subcomm_parent;
  85. };
  86. /*
  87. * Parse options from the command line
  88. *
  89. * Returns 0 on success.
  90. */
  91. enum error config_parse(int argc, char **argv);
  92. /*
  93. * Free any memory that may have been allocated
  94. * in config_parse
  95. */
  96. int config_free();
  97. /*
  98. * Write out the options to stdout
  99. */
  100. void config_dump();
  101. /*
  102. * Get a config option by its long name
  103. * The pointer to the options value will be stored, at the pointer pointed to
  104. * by out
  105. * If the option is unset, it returns ERR_OPT_UNSET and out is unchanged
  106. * It the options is not found, it returns ERR_OPT_NOTFOUND, and out is unchanged
  107. */
  108. enum error config_get(const char *key, void **out);
  109. enum error config_get_subopt(const char *subcomm, const char *key, void **out);
  110. /*
  111. * Return an cmd line that is not an option
  112. * or null if error, or out of elements
  113. */
  114. const char *config_get_nonopt(int index);
  115. int config_get_nonopt_count();
  116. #endif /* _CONFIG_H */