ark_parser.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "ark_parser.h"
  17. static void strip_newline(char *str) {
  18. int len = strlen(str);
  19. while (len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r')) {
  20. str[len - 1] = '\0';
  21. len--;
  22. }
  23. }
  24. static char *trim_whitespace(char *str) {
  25. while (*str == ' ' || *str == '\t')
  26. str++;
  27. int len = strlen(str);
  28. while (len > 0 && (str[len - 1] == ' ' || str[len - 1] == '\t')) {
  29. str[len - 1] = '\0';
  30. len--;
  31. }
  32. return str;
  33. }
  34. ArkConfig *ark_parse(const char *filepath) {
  35. ArkConfig *config = (ArkConfig *)malloc(sizeof(ArkConfig));
  36. memset(config, 0, sizeof(ArkConfig));
  37. strncpy(config->filepath, filepath, sizeof(config->filepath) - 1);
  38. FILE *f = fopen(filepath, "r");
  39. if (!f)
  40. return config;
  41. char line[MAX_LINE_LEN];
  42. ArkSection *curr_sec = NULL;
  43. while (fgets(line, sizeof(line), f)) {
  44. strip_newline(line);
  45. char *trimmed = trim_whitespace(line);
  46. if (strlen(trimmed) == 0 || trimmed[0] == '#')
  47. continue;
  48. // Check for section
  49. if (strncmp(trimmed, "---", 3) == 0) {
  50. char *end = strstr(trimmed + 3, "---");
  51. if (end && config->section_count < MAX_SECTIONS) {
  52. *end = '\0';
  53. curr_sec = &config->sections[config->section_count++];
  54. strncpy(curr_sec->name, trim_whitespace(trimmed + 3), 255);
  55. }
  56. continue;
  57. }
  58. // Check for Key=Value or Key:Value
  59. char *sep = strchr(trimmed, '=');
  60. if (!sep)
  61. sep = strchr(trimmed, ':');
  62. if (sep) {
  63. *sep = '\0';
  64. char *key = trim_whitespace(trimmed);
  65. char *val = trim_whitespace(sep + 1);
  66. if (curr_sec) {
  67. if (curr_sec->pair_count < MAX_KEYS) {
  68. strncpy(curr_sec->pairs[curr_sec->pair_count].key, key, 255);
  69. strncpy(curr_sec->pairs[curr_sec->pair_count].value, val, 511);
  70. curr_sec->pair_count++;
  71. }
  72. } else {
  73. if (config->global_count < MAX_KEYS) {
  74. strncpy(config->global_pairs[config->global_count].key, key, 255);
  75. strncpy(config->global_pairs[config->global_count].value, val, 511);
  76. config->global_count++;
  77. }
  78. }
  79. } else {
  80. if (config->standalone_count < MAX_STANDALONE) {
  81. strncpy(config->standalone[config->standalone_count++], trimmed, 511);
  82. }
  83. }
  84. }
  85. fclose(f);
  86. return config;
  87. }
  88. const char *ark_get_global(ArkConfig *config, const char *key) {
  89. for (int i = 0; i < config->global_count; i++) {
  90. if (strcmp(config->global_pairs[i].key, key) == 0) {
  91. return config->global_pairs[i].value;
  92. }
  93. }
  94. return NULL;
  95. }
  96. const char *ark_get_section_val(ArkConfig *config, const char *section,
  97. const char *key) {
  98. for (int i = 0; i < config->section_count; i++) {
  99. if (strcmp(config->sections[i].name, section) == 0) {
  100. for (int j = 0; j < config->sections[i].pair_count; j++) {
  101. if (strcmp(config->sections[i].pairs[j].key, key) == 0) {
  102. return config->sections[i].pairs[j].value;
  103. }
  104. }
  105. }
  106. }
  107. return NULL;
  108. }
  109. void ark_set_global(ArkConfig *config, const char *key, const char *value) {
  110. for (int i = 0; i < config->global_count; i++) {
  111. if (strcmp(config->global_pairs[i].key, key) == 0) {
  112. strncpy(config->global_pairs[i].value, value, 511);
  113. return;
  114. }
  115. }
  116. if (config->global_count < MAX_KEYS) {
  117. strncpy(config->global_pairs[config->global_count].key, key, 255);
  118. strncpy(config->global_pairs[config->global_count].value, value, 511);
  119. config->global_count++;
  120. }
  121. }
  122. void ark_dump(ArkConfig *config, const char *filepath) {
  123. FILE *f = fopen(filepath, "w");
  124. if (!f)
  125. return;
  126. for (int i = 0; i < config->global_count; i++) {
  127. fprintf(f, "%s = %s\n", config->global_pairs[i].key,
  128. config->global_pairs[i].value);
  129. }
  130. for (int i = 0; i < config->section_count; i++) {
  131. fprintf(f, "\n--- %s ---\n\n", config->sections[i].name);
  132. for (int j = 0; j < config->sections[i].pair_count; j++) {
  133. fprintf(f, "%s = %s\n", config->sections[i].pairs[j].key,
  134. config->sections[i].pairs[j].value);
  135. }
  136. }
  137. if (config->standalone_count > 0) {
  138. fprintf(f, "\n");
  139. for (int i = 0; i < config->standalone_count; i++) {
  140. fprintf(f, "%s\n", config->standalone[i]);
  141. }
  142. }
  143. fclose(f);
  144. }
  145. void ark_free(ArkConfig *config) {
  146. if (config)
  147. free(config);
  148. }
  149. /* ── List Value Support ────────────────────────────────────────── */
  150. static ArkList parse_csv(const char *value) {
  151. ArkList list;
  152. memset(&list, 0, sizeof(ArkList));
  153. if (!value)
  154. return list;
  155. char buf[512];
  156. strncpy(buf, value, sizeof(buf) - 1);
  157. buf[sizeof(buf) - 1] = '\0';
  158. char *token = strtok(buf, ",");
  159. while (token && list.count < MAX_LIST_ITEMS) {
  160. char *trimmed = trim_whitespace(token);
  161. if (strlen(trimmed) > 0) {
  162. strncpy(list.items[list.count], trimmed, 255);
  163. list.count++;
  164. }
  165. token = strtok(NULL, ",");
  166. }
  167. return list;
  168. }
  169. ArkList ark_get_list(ArkConfig *config, const char *key) {
  170. const char *val = ark_get_global(config, key);
  171. return parse_csv(val);
  172. }
  173. ArkList ark_get_section_list(ArkConfig *config, const char *section,
  174. const char *key) {
  175. const char *val = ark_get_section_val(config, section, key);
  176. return parse_csv(val);
  177. }
  178. /* ── Boolean Value Support ─────────────────────────────────────── */
  179. static int parse_bool(const char *value, int default_val) {
  180. if (!value)
  181. return default_val;
  182. if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 ||
  183. strcmp(value, "1") == 0 || strcmp(value, "on") == 0)
  184. return 1;
  185. if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 ||
  186. strcmp(value, "0") == 0 || strcmp(value, "off") == 0)
  187. return 0;
  188. return default_val;
  189. }
  190. int ark_get_bool(ArkConfig *config, const char *key, int default_val) {
  191. const char *val = ark_get_global(config, key);
  192. return parse_bool(val, default_val);
  193. }
  194. int ark_get_section_bool(ArkConfig *config, const char *section,
  195. const char *key, int default_val) {
  196. const char *val = ark_get_section_val(config, section, key);
  197. return parse_bool(val, default_val);
  198. }