Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_json_config.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Provides lightweight JSON and conf value extraction helpers for manifests and config files.
4 */
5
7
8#include "native_support.h"
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#ifdef _WIN32
15#define pharos_json_strdup _strdup
16#else
17#define pharos_json_strdup strdup
18#endif
19
20char *json_find_string_after(const char *text, const char *key, const char *start_at) {
21 char pattern[256];
22 const char *cursor;
23 const char *colon;
24 const char *open_quote;
25 const char *end_quote;
26 size_t key_len;
27 if (text == NULL || key == NULL) {
28 return NULL;
29 }
30 snprintf(pattern, sizeof(pattern), "\"%s\"", key);
31 cursor = strstr(start_at != NULL ? start_at : text, pattern);
32 if (cursor == NULL) {
33 return NULL;
34 }
35 key_len = strlen(pattern);
36 colon = strchr(cursor + key_len, ':');
37 if (colon == NULL) {
38 return NULL;
39 }
40 open_quote = strchr(colon, '"');
41 if (open_quote == NULL) {
42 return NULL;
43 }
44 open_quote++;
45 end_quote = open_quote;
46 while (*end_quote != '\0') {
47 if (*end_quote == '"' && end_quote[-1] != '\\') {
48 break;
49 }
50 end_quote++;
51 }
52 if (*end_quote != '"') {
53 return NULL;
54 }
55 return dup_range(open_quote, (size_t)(end_quote - open_quote));
56}
57
58char *json_find_string(const char *text, const char *key) {
59 return json_find_string_after(text, key, text);
60}
61
62char *json_find_string_in_section(const char *text, const char *section, const char *key) {
63 char pattern[256];
64 const char *cursor;
65 const char *brace;
66 if (text == NULL || section == NULL || key == NULL) {
67 return NULL;
68 }
69 snprintf(pattern, sizeof(pattern), "\"%s\"", section);
70 cursor = strstr(text, pattern);
71 if (cursor == NULL) {
72 return NULL;
73 }
74 brace = strchr(cursor, '{');
75 if (brace == NULL) {
76 return NULL;
77 }
78 return json_find_string_after(text, key, brace);
79}
80
81char *json_extract_array_in_section(const char *text, const char *section, const char *key) {
82 char section_pattern[256];
83 char key_pattern[256];
84 const char *section_cursor;
85 const char *brace;
86 const char *key_cursor;
87 const char *colon;
88 const char *open_bracket;
89 const char *close_bracket;
90 int depth;
91 size_t len;
92 char *result;
93 if (text == NULL || section == NULL || key == NULL) {
94 return NULL;
95 }
96 snprintf(section_pattern, sizeof(section_pattern), "\"%s\"", section);
97 section_cursor = strstr(text, section_pattern);
98 if (section_cursor == NULL) {
99 return NULL;
100 }
101 brace = strchr(section_cursor, '{');
102 if (brace == NULL) {
103 return NULL;
104 }
105 snprintf(key_pattern, sizeof(key_pattern), "\"%s\"", key);
106 key_cursor = strstr(brace, key_pattern);
107 if (key_cursor == NULL) {
108 return NULL;
109 }
110 colon = strchr(key_cursor + strlen(key_pattern), ':');
111 if (colon == NULL) {
112 return NULL;
113 }
114 open_bracket = strchr(colon, '[');
115 if (open_bracket == NULL) {
116 return NULL;
117 }
118 depth = 1;
119 for (close_bracket = open_bracket + 1; *close_bracket != '\0' && depth > 0; close_bracket++) {
120 if (*close_bracket == '[') {
121 depth++;
122 } else if (*close_bracket == ']') {
123 depth--;
124 }
125 }
126 if (depth != 0) {
127 return NULL;
128 }
129 len = (size_t)(close_bracket - open_bracket);
130 result = (char *)malloc(len + 1);
131 if (result == NULL) {
132 return NULL;
133 }
134 memcpy(result, open_bracket, len);
135 result[len] = '\0';
136 return result;
137}
138
139char *conf_lookup_value_in_text(const char *text, const char *key) {
140 const char *cursor;
141 size_t key_len;
142 if (text == NULL || key == NULL) {
143 return NULL;
144 }
145 key_len = strlen(key);
146 cursor = text;
147 while (*cursor != '\0') {
148 const char *line_end = strchr(cursor, '\n');
149 size_t line_len = line_end != NULL ? (size_t)(line_end - cursor) : strlen(cursor);
150 char *line = dup_range(cursor, line_len);
151 char *eq;
152 char *value_copy;
153 if (line == NULL) {
154 return NULL;
155 }
156 trim_in_place(line);
157 if (line[0] != '\0' && line[0] != '#') {
158 eq = strchr(line, '=');
159 if (eq != NULL) {
160 *eq = '\0';
161 trim_in_place(line);
162 trim_in_place(eq + 1);
163 if (strlen(line) == key_len && strcmp(line, key) == 0) {
164 value_copy = pharos_json_strdup(eq + 1);
165 free(line);
166 return value_copy;
167 }
168 }
169 }
170 free(line);
171 if (line_end == NULL) {
172 break;
173 }
174 cursor = line_end + 1;
175 }
176 return NULL;
177}
178
179char *conf_lookup_value(const char *path, const char *key) {
180 char *text;
181 char *value;
182 if (path == NULL || key == NULL) {
183 return NULL;
184 }
185 text = read_file_text(path);
186 if (text == NULL) {
187 return NULL;
188 }
189 value = conf_lookup_value_in_text(text, key);
190 free(text);
191 return value;
192}
char * json_find_string_after(const char *text, const char *key, const char *start_at)
Finds a JSON string value for a key after a given scan position.
char * conf_lookup_value_in_text(const char *text, const char *key)
Looks up one key from Pharos conf-format text.
#define pharos_json_strdup
char * conf_lookup_value(const char *path, const char *key)
Reads a Pharos conf file and looks up one key.
char * json_extract_array_in_section(const char *text, const char *section, const char *key)
Extracts the raw JSON array text for a key inside a named section.
char * json_find_string(const char *text, const char *key)
Finds the first JSON string value associated with a key.
char * json_find_string_in_section(const char *text, const char *section, const char *key)
Finds a JSON string value for a key inside a named section object.
Declares lightweight JSON and conf value lookup helpers.
void trim_in_place(char *text)
Trims leading and trailing ASCII whitespace from a string in place.
char * dup_range(const char *start, size_t len)
Duplicates a byte range into a newly allocated NUL-terminated string.
char * read_file_text(const char *path)
Reads an entire file into a newly allocated text buffer.
Declares shared low-level support helpers used across the Pharos native runtime.
const char size_t len
Definition yyjson.h:8364