smarty template cache syncing daemon (PoC)
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.

183 lines
4.6KB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "list.h"
  25. #include "configfile.h"
  26. #include "logging.h"
  27. static LIST_HEAD(config_list);
  28. static struct conf_section * config_add_section(char *name)
  29. {
  30. struct conf_section *section;
  31. if (!name)
  32. return NULL;
  33. section = malloc(sizeof(struct conf_section));
  34. if (section) {
  35. INIT_LIST_HEAD(&section->list);
  36. INIT_LIST_HEAD(&section->tupel);
  37. strncpy(section->name, name, sizeof(section->name));
  38. list_add_tail(&section->list, &config_list);
  39. }
  40. return section;
  41. }
  42. static int config_add_tupel(struct conf_section *section, char *option, char *parameter)
  43. {
  44. struct conf_tupel *tupel;
  45. if (!section || !option || !parameter)
  46. return 0;
  47. if (!(tupel = malloc(sizeof(struct conf_tupel))))
  48. return 0;
  49. INIT_LIST_HEAD(&tupel->list);
  50. tupel->option = strdup(option);
  51. tupel->parameter = strdup(parameter);
  52. list_add_tail(&tupel->list, &section->tupel);
  53. return 1;
  54. }
  55. static void config_free(void)
  56. {
  57. struct conf_section *section, *section_tmp;
  58. struct conf_tupel *tupel, *tupel_tmp;
  59. list_for_each_entry_safe(section, section_tmp, &config_list, list) {
  60. list_for_each_entry_safe(tupel, tupel_tmp, &section->tupel, list) {
  61. list_del(&tupel->list);
  62. free(tupel->option);
  63. free(tupel->parameter);
  64. free(tupel);
  65. }
  66. list_del(&section->list);
  67. free(section);
  68. }
  69. }
  70. int config_parse(char *config)
  71. {
  72. struct conf_section *section = NULL;
  73. FILE *fz;
  74. int i = 0, ret = 1;
  75. char *row, *tok, *tok2;
  76. if (!config)
  77. return 0;
  78. if (!(row = malloc(1024)))
  79. return 0;
  80. if (!(fz = fopen(config, "r"))) {
  81. log_print(LOG_ERROR, "config_parse(): %s", config);
  82. return 0;
  83. }
  84. while (fgets(row, 1024, fz)) {
  85. i++;
  86. if (row[0] == '#' || row[0] <= ' ') {
  87. continue;
  88. } else if (row[0] == '[') {
  89. tok = strtok(row +1, " ]\n");
  90. section = config_add_section(tok);
  91. if (!section) {
  92. log_print(LOG_WARN, "config_parse(): invalid section in row %d", i);
  93. ret = 0;
  94. break;
  95. }
  96. continue;
  97. } else if (!section) {
  98. log_print(LOG_WARN, "config_parse(): missing section in row %d", i);
  99. ret = 0;
  100. break;
  101. }
  102. if ((tok = strtok(row, " \n")) && (tok2 = strtok(NULL, " \n")))
  103. if (!config_add_tupel(section, tok, tok2))
  104. log_print(LOG_WARN, "config_parse(): invalid row %d", i);
  105. }
  106. fclose(fz);
  107. free(row);
  108. if (atexit(config_free) != 0) {
  109. log_print(LOG_ERROR, "config_parse(): atexit()");
  110. return 0;
  111. }
  112. return ret;
  113. }
  114. struct conf_section * config_get_section(char *name)
  115. {
  116. struct conf_section *section;
  117. list_for_each_entry(section, &config_list, list) {
  118. if (!strcmp(section->name, name))
  119. return section;
  120. }
  121. return NULL;
  122. }
  123. char * config_get_parameter(struct conf_section *section, char *option)
  124. {
  125. struct conf_tupel *tupel;
  126. list_for_each_entry(tupel, &section->tupel, list) {
  127. if (!strcmp(tupel->option, option))
  128. return tupel->parameter;
  129. }
  130. return NULL;
  131. }
  132. char * config_get_string(char *section, char *option, char *def)
  133. {
  134. struct conf_section *tmp;
  135. char *ret;
  136. tmp = config_get_section(section);
  137. if (tmp && (ret = config_get_parameter(tmp, option)))
  138. return ret;
  139. return def;
  140. }
  141. int config_get_int(char *section, char *option, int def)
  142. {
  143. char *ret;
  144. ret = config_get_string(section, option, NULL);
  145. return ret ? atoi(ret) : def;
  146. }