15#define pharos_json_strdup _strdup
17#define pharos_json_strdup strdup
24 const char *open_quote;
25 const char *end_quote;
27 if (text == NULL || key == NULL) {
30 snprintf(pattern,
sizeof(pattern),
"\"%s\"", key);
31 cursor = strstr(start_at != NULL ? start_at : text, pattern);
35 key_len = strlen(pattern);
36 colon = strchr(cursor + key_len,
':');
40 open_quote = strchr(colon,
'"');
41 if (open_quote == NULL) {
45 end_quote = open_quote;
46 while (*end_quote !=
'\0') {
47 if (*end_quote ==
'"' && end_quote[-1] !=
'\\') {
52 if (*end_quote !=
'"') {
55 return dup_range(open_quote, (
size_t)(end_quote - open_quote));
66 if (text == NULL || section == NULL || key == NULL) {
69 snprintf(pattern,
sizeof(pattern),
"\"%s\"", section);
70 cursor = strstr(text, pattern);
74 brace = strchr(cursor,
'{');
82 char section_pattern[256];
83 char key_pattern[256];
84 const char *section_cursor;
86 const char *key_cursor;
88 const char *open_bracket;
89 const char *close_bracket;
93 if (text == NULL || section == NULL || key == NULL) {
96 snprintf(section_pattern,
sizeof(section_pattern),
"\"%s\"", section);
97 section_cursor = strstr(text, section_pattern);
98 if (section_cursor == NULL) {
101 brace = strchr(section_cursor,
'{');
105 snprintf(key_pattern,
sizeof(key_pattern),
"\"%s\"", key);
106 key_cursor = strstr(brace, key_pattern);
107 if (key_cursor == NULL) {
110 colon = strchr(key_cursor + strlen(key_pattern),
':');
114 open_bracket = strchr(colon,
'[');
115 if (open_bracket == NULL) {
119 for (close_bracket = open_bracket + 1; *close_bracket !=
'\0' && depth > 0; close_bracket++) {
120 if (*close_bracket ==
'[') {
122 }
else if (*close_bracket ==
']') {
129 len = (size_t)(close_bracket - open_bracket);
130 result = (
char *)malloc(
len + 1);
131 if (result == NULL) {
134 memcpy(result, open_bracket,
len);
142 if (text == NULL || key == NULL) {
145 key_len = strlen(key);
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);
157 if (line[0] !=
'\0' && line[0] !=
'#') {
158 eq = strchr(line,
'=');
163 if (strlen(line) == key_len && strcmp(line, key) == 0) {
171 if (line_end == NULL) {
174 cursor = line_end + 1;
182 if (path == NULL || key == NULL) {
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.