Browse Source

fix: preserve newlines in config_updater insert/update — no extra newlines on key replacement, add leading nl when inserting at end without trailing nl

chatgui v0.1.0-rc1
Evgeny 3 months ago
parent
commit
487cb8718e
  1. 47
      src/config_updater.c

47
src/config_updater.c

@ -151,19 +151,23 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
char *opt_pos = find_option(*buf, *buf_len, option); char *opt_pos = find_option(*buf, *buf_len, option);
if (opt_pos) { if (opt_pos) {
// Find option line end // Determine if old line has trailing \n
char *line_end = strchr(opt_pos, '\n'); char *nl = strchr(opt_pos, '\n');
int has_trailing_nl = (nl != NULL);
if (!line_end) line_end = *buf + *buf_len; char *line_end = nl ? nl : *buf + *buf_len;
// Prepare new line // Prepare new line
char new_line[MAX_LINE_LEN]; char new_line[MAX_LINE_LEN];
int new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value); int new_len;
if (has_trailing_nl) {
new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value);
} else {
new_len = snprintf(new_line, sizeof(new_line), "%s=%s", option, value);
}
if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1; if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1;
// Calculate length difference // Calculate length difference
size_t old_line_len = line_end - opt_pos + 1; size_t old_line_len = has_trailing_nl ? (line_end - opt_pos + 1) : (line_end - opt_pos);
long len_diff = new_len - old_line_len; long len_diff = new_len - old_line_len;
// Ensure buffer capacity // Ensure buffer capacity
@ -177,9 +181,9 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
opt_pos = find_option(*buf, *buf_len, option); opt_pos = find_option(*buf, *buf_len, option);
if (!opt_pos) return -1; if (!opt_pos) return -1;
line_end = strchr(opt_pos, '\n'); nl = strchr(opt_pos, '\n');
has_trailing_nl = (nl != NULL);
if (!line_end) line_end = *buf + *buf_len; line_end = nl ? nl : *buf + *buf_len;
} }
// Move content and insert new line // Move content and insert new line
@ -197,9 +201,13 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
// Find end of global section (next [ or end of buffer) // Find end of global section (next [ or end of buffer)
char *insert_pos = strchr(section_end, '['); char *insert_pos = strchr(section_end, '[');
int need_leading_nl = 0;
if (!insert_pos) insert_pos = *buf + *buf_len; if (!insert_pos) {
else { insert_pos = *buf + *buf_len;
if (*buf_len > 0 && (*buf)[*buf_len - 1] != '\n')
need_leading_nl = 1;
} else {
// Move to beginning of next line // Move to beginning of next line
char *prev_nl = insert_pos; char *prev_nl = insert_pos;
while (prev_nl > *buf && *prev_nl != '\n') prev_nl--; while (prev_nl > *buf && *prev_nl != '\n') prev_nl--;
@ -208,8 +216,11 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
// Prepare new line // Prepare new line
char new_line[MAX_LINE_LEN]; char new_line[MAX_LINE_LEN];
int new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value); int new_len;
if (need_leading_nl)
new_len = snprintf(new_line, sizeof(new_line), "\n%s=%s\n", option, value);
else
new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value);
if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1; if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1;
// Ensure buffer capacity // Ensure buffer capacity
@ -224,9 +235,13 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
if (!section_end) return -1; if (!section_end) return -1;
insert_pos = strchr(section_end, '['); insert_pos = strchr(section_end, '[');
need_leading_nl = 0;
if (!insert_pos) insert_pos = *buf + *buf_len; if (!insert_pos) {
else { insert_pos = *buf + *buf_len;
if (*buf_len > 0 && (*buf)[*buf_len - 1] != '\n')
need_leading_nl = 1;
} else {
char *prev_nl = insert_pos; char *prev_nl = insert_pos;
while (prev_nl > *buf && *prev_nl != '\n') prev_nl--; while (prev_nl > *buf && *prev_nl != '\n') prev_nl--;
if (*prev_nl == '\n') insert_pos = prev_nl + 1; if (*prev_nl == '\n') insert_pos = prev_nl + 1;

Loading…
Cancel
Save