Config.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "Config.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. int load_config(const char *filename, Config *config) {
  6. FILE *file = fopen(filename, "r");
  7. if (!file) {
  8. return -1;
  9. }
  10. char line[512];
  11. char section[64] = "";
  12. while (fgets(line, sizeof(line), file)) {
  13. line[strcspn(line, "\r\n")] = 0;
  14. if (line[0] == '\0' || line[0] == '#' || line[0] == ';') {
  15. continue;
  16. }
  17. if (line[0] == '[') {
  18. char *end = strchr(line, ']');
  19. if (end) {
  20. *end = '\0';
  21. snprintf(section, sizeof(section), "%.*s", (int)(sizeof(section) - 1),
  22. line + 1);
  23. section[sizeof(section) - 1] = '\0';
  24. }
  25. continue;
  26. }
  27. char *delimiter = strchr(line, '=');
  28. if (delimiter) {
  29. *delimiter = '\0';
  30. char *key = line;
  31. char *value = delimiter + 1;
  32. while (*key == ' ' || *key == '\t')
  33. key++;
  34. while (*value == ' ' || *value == '\t')
  35. value++;
  36. char *key_end = key + strlen(key) - 1;
  37. while (key_end > key && (*key_end == ' ' || *key_end == '\t')) {
  38. *key_end = '\0';
  39. key_end--;
  40. }
  41. char *value_end = value + strlen(value) - 1;
  42. while (value_end > value && (*value_end == ' ' || *value_end == '\t' ||
  43. *value_end == '"' || *value_end == '\'')) {
  44. *value_end = '\0';
  45. value_end--;
  46. }
  47. while (*value == '"' || *value == '\'')
  48. value++;
  49. if (strcmp(section, "server") == 0) {
  50. if (strcmp(key, "host") == 0) {
  51. strncpy(config->host, value, sizeof(config->host) - 1);
  52. config->host[sizeof(config->host) - 1] = '\0';
  53. } else if (strcmp(key, "port") == 0) {
  54. config->port = atoi(value);
  55. }
  56. } else if (strcmp(section, "proxy") == 0) {
  57. if (strcmp(key, "proxy") == 0) {
  58. strncpy(config->proxy, value, sizeof(config->proxy) - 1);
  59. config->proxy[sizeof(config->proxy) - 1] = '\0';
  60. } else if (strcmp(key, "list_file") == 0) {
  61. strncpy(config->proxy_list_file, value,
  62. sizeof(config->proxy_list_file) - 1);
  63. config->proxy_list_file[sizeof(config->proxy_list_file) - 1] = '\0';
  64. } else if (strcmp(key, "max_retries") == 0) {
  65. config->max_proxy_retries = atoi(value);
  66. } else if (strcmp(key, "randomize_username") == 0) {
  67. config->randomize_username = atoi(value);
  68. } else if (strcmp(key, "randomize_password") == 0) {
  69. config->randomize_password = atoi(value);
  70. }
  71. } else if (strcmp(section, "cache") == 0) {
  72. if (strcmp(key, "dir") == 0) {
  73. strncpy(config->cache_dir, value, sizeof(config->cache_dir) - 1);
  74. config->cache_dir[sizeof(config->cache_dir) - 1] = '\0';
  75. } else if (strcmp(key, "ttl_search") == 0) {
  76. config->cache_ttl_search = atoi(value);
  77. } else if (strcmp(key, "ttl_infobox") == 0) {
  78. config->cache_ttl_infobox = atoi(value);
  79. }
  80. }
  81. }
  82. }
  83. fclose(file);
  84. return 0;
  85. }