Skip to content

Commit 85e66fe

Browse files
Update ini.c
1 parent 101b87b commit 85e66fe

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

code/logic/ini.c

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ static fossil_media_ini_entry_t *find_entry(fossil_media_ini_section_t *section,
6666

6767
int fossil_media_ini_load_string(const char *data, fossil_media_ini_t *ini) {
6868
memset(ini, 0, sizeof(*ini));
69-
if (!data || *data == '\0') return 0;
69+
if (!data || *data == '\0')
70+
return 0;
7071

7172
fossil_media_ini_section_t *current_section = NULL;
7273
const char *line_start = data;
@@ -82,14 +83,13 @@ int fossil_media_ini_load_string(const char *data, fossil_media_ini_t *ini) {
8283
memcpy(line, line_start, line_len);
8384
line[line_len] = '\0';
8485

85-
// Remove inline comments (only if not inside a quoted multiline value)
86-
if (!multiline_quote) remove_inline_comment(line);
86+
if (!multiline_quote)
87+
remove_inline_comment(line);
8788

88-
// Trim
8989
char *trimmed = strdup_trim(line);
9090
free(line);
9191

92-
// Handle multiline quoted value
92+
// --- Handle multiline quoted value ---
9393
if (multiline_quote) {
9494
size_t tlen = strlen(trimmed);
9595
char *end_quote = NULL;
@@ -99,53 +99,54 @@ int fossil_media_ini_load_string(const char *data, fossil_media_ini_t *ini) {
9999
break;
100100
}
101101
}
102+
103+
size_t oldlen = strlen(multiline_value);
104+
size_t addlen = strlen(trimmed);
105+
multiline_value = realloc(multiline_value, oldlen + addlen + 2);
106+
multiline_value[oldlen] = '\n';
107+
memcpy(multiline_value + oldlen + 1, trimmed, addlen + 1);
108+
102109
if (end_quote) {
103-
// End of multiline value
104110
*end_quote = '\0';
105-
size_t oldlen = strlen(multiline_value);
106-
size_t addlen = strlen(trimmed);
107-
multiline_value = realloc(multiline_value, oldlen + addlen + 2);
108-
multiline_value[oldlen] = '\n';
109-
memcpy(multiline_value + oldlen + 1, trimmed, addlen + 1);
110-
// Store the key/value
111+
// End of multiline value: store key/value
111112
current_section->entries = realloc(
112113
current_section->entries,
113114
sizeof(*current_section->entries) * (current_section->entry_count + 1)
114115
);
115-
fossil_media_ini_entry_t *entry = &current_section->entries[current_section->entry_count++];
116+
fossil_media_ini_entry_t *entry =
117+
&current_section->entries[current_section->entry_count++];
116118
entry->key = multiline_key;
117119
entry->value = multiline_value;
118120
multiline_key = NULL;
119121
multiline_value = NULL;
120122
multiline_quote = 0;
121-
free(trimmed);
122-
goto next_line;
123-
} else {
124-
// Continue multiline value
125-
size_t oldlen = strlen(multiline_value);
126-
size_t addlen = strlen(trimmed);
127-
multiline_value = realloc(multiline_value, oldlen + addlen + 2);
128-
multiline_value[oldlen] = '\n';
129-
memcpy(multiline_value + oldlen + 1, trimmed, addlen + 1);
130-
free(trimmed);
131-
goto next_line;
132123
}
124+
125+
free(trimmed);
126+
if (!line_end) break;
127+
line_start = line_end + 1;
128+
continue; // <-- clean jump to next line
133129
}
134130

135-
// Skip blank and comments
131+
// --- Skip blanks and comments ---
136132
if (!trimmed || *trimmed == '\0') {
137-
if (trimmed) free(trimmed);
138-
goto next_line;
133+
free(trimmed);
134+
if (!line_end) break;
135+
line_start = line_end + 1;
136+
continue;
139137
}
140138

141-
// Section header
139+
// --- Section header ---
142140
if (*trimmed == '[') {
143141
char *end = strchr(trimmed, ']');
144142
if (end) {
145143
*end = '\0';
146144
char *section_name = strdup_trim(trimmed + 1);
147145
if (section_name && *section_name) {
148-
ini->sections = realloc(ini->sections, sizeof(*ini->sections) * (ini->section_count + 1));
146+
ini->sections = realloc(
147+
ini->sections,
148+
sizeof(*ini->sections) * (ini->section_count + 1)
149+
);
149150
current_section = &ini->sections[ini->section_count++];
150151
current_section->name = section_name;
151152
current_section->entries = NULL;
@@ -155,35 +156,40 @@ int fossil_media_ini_load_string(const char *data, fossil_media_ini_t *ini) {
155156
current_section = NULL;
156157
}
157158
}
159+
free(trimmed);
160+
if (!line_end) break;
161+
line_start = line_end + 1;
162+
continue;
158163
}
159-
// Key=value pair or key only
160-
else if (current_section) {
164+
165+
// --- Key=value pair (or ignored key) ---
166+
if (current_section) {
161167
char *eq = strchr(trimmed, '=');
162168
if (eq) {
163169
*eq = '\0';
164170
char *key = strdup_trim(trimmed);
165171
char *value = strdup_trim(eq + 1);
166172

167-
// Handle quoted values (including multiline)
173+
// Handle quoted (possibly multiline) values
168174
if (value && (*value == '"' || *value == '\'')) {
169175
char quote = *value;
170176
size_t vlen = strlen(value);
171-
if (vlen > 1 && value[vlen-1] == quote) {
172-
value[vlen-1] = '\0';
173-
memmove(value, value+1, vlen-1);
177+
if (vlen > 1 && value[vlen - 1] == quote) {
178+
value[vlen - 1] = '\0';
179+
memmove(value, value + 1, vlen - 1);
174180
} else {
175-
// Multiline quoted value
176-
memmove(value, value+1, vlen); // remove leading quote
181+
memmove(value, value + 1, vlen); // remove leading quote
177182
multiline_key = key;
178183
multiline_value = fossil_media_strdup(value);
179184
multiline_quote = quote;
180185
free(value);
181186
free(trimmed);
182-
goto next_line;
187+
if (!line_end) break;
188+
line_start = line_end + 1;
189+
continue;
183190
}
184191
}
185192

186-
// Check for duplicate key: overwrite previous
187193
fossil_media_ini_entry_t *entry = find_entry(current_section, key);
188194
if (entry) {
189195
free(entry->value);
@@ -198,29 +204,24 @@ int fossil_media_ini_load_string(const char *data, fossil_media_ini_t *ini) {
198204
entry->key = key;
199205
entry->value = value;
200206
}
201-
} else {
202-
// Key without '=': ignore (do not store)
203207
}
204208
}
205209

206210
free(trimmed);
207-
next_line:
208211
if (!line_end) break;
209212
line_start = line_end + 1;
210213
}
211214

212-
// If file ends while still in multiline quoted value, store it
215+
// Handle EOF during multiline quoted value
213216
if (multiline_quote && current_section && multiline_key && multiline_value) {
214217
current_section->entries = realloc(
215218
current_section->entries,
216219
sizeof(*current_section->entries) * (current_section->entry_count + 1)
217220
);
218-
fossil_media_ini_entry_t *entry = &current_section->entries[current_section->entry_count++];
221+
fossil_media_ini_entry_t *entry =
222+
&current_section->entries[current_section->entry_count++];
219223
entry->key = multiline_key;
220224
entry->value = multiline_value;
221-
multiline_key = NULL;
222-
multiline_value = NULL;
223-
multiline_quote = 0;
224225
}
225226

226227
return 0;

0 commit comments

Comments
 (0)