refactor(flatfile): single-line file header with format-version marker

This commit is contained in:
2026-05-05 13:50:06 +03:00
parent b8a4900393
commit 2990ec795a
3 changed files with 18 additions and 15 deletions

View File

@@ -26,12 +26,12 @@
#define DIR_FLATLOG "flatlog"
#define FLATFILE_FORMAT_VERSION 1
#define FF_VERSION_PREFIX "# format-version: "
#define FLATFILE_HEADER \
"# profanity chat log — UTF-8, LF line endings\n" \
"# format-version: 1\n"
#define FF_MAX_LINE_LEN (10 * 1024 * 1024) /* 10 MB — reject lines longer than this */
#define FF_MAX_LMC_DEPTH 100 /* max correction chain depth */
#define FF_VERSION_MARKER "format-version: "
#define FF_STRINGIFY_(x) #x
#define FF_STRINGIFY(x) FF_STRINGIFY_(x)
#define FLATFILE_HEADER "# cproof chat log — UTF-8, LF line endings, " FF_VERSION_MARKER FF_STRINGIFY(FLATFILE_FORMAT_VERSION) "\n"
#define FF_MAX_LINE_LEN (10 * 1024 * 1024) /* 10 MB — reject lines longer than this */
#define FF_MAX_LMC_DEPTH 100 /* max correction chain depth */
// --- Shared global ---

View File

@@ -342,10 +342,11 @@ ff_skip_bom(FILE* fp)
// Format-version helper
// =========================================================================
//
// Scan leading '#' comment lines for `# format-version: <N>`. Stops at
// the first non-comment line or after FF_VERSION_SCAN_MAX comments.
// Restores fp to its position at entry, so callers can re-parse the
// header normally afterwards.
// Scan leading '#' comment lines for the `format-version: <N>` marker
// (anywhere within the comment line). Stops at the first non-comment
// line or after FF_VERSION_SCAN_MAX comments. Restores fp to its
// position at entry, so callers can re-parse the header normally
// afterwards.
#define FF_VERSION_SCAN_MAX 16
int
@@ -356,7 +357,7 @@ ff_read_format_version(FILE* fp)
return -1;
int version = 0;
size_t prefix_len = strlen(FF_VERSION_PREFIX);
size_t marker_len = strlen(FF_VERSION_MARKER);
for (int i = 0; i < FF_VERSION_SCAN_MAX; i++) {
gboolean truncated = FALSE;
@@ -367,10 +368,12 @@ ff_read_format_version(FILE* fp)
free(line);
break;
}
if (strncmp(line, FF_VERSION_PREFIX, prefix_len) == 0) {
char* found = strstr(line, FF_VERSION_MARKER);
if (found) {
char* num_start = found + marker_len;
char* endptr = NULL;
long v = strtol(line + prefix_len, &endptr, 10);
if (endptr != line + prefix_len && v > 0 && v <= INT_MAX)
long v = strtol(num_start, &endptr, 10);
if (endptr != num_start && v > 0 && v <= INT_MAX)
version = (int)v;
free(line);
break;

View File

@@ -458,7 +458,7 @@ void
test_ff_parse_line_comment(void** state)
{
assert_null(ff_parse_line("# this is a comment"));
assert_null(ff_parse_line("# profanity chat log — UTF-8, LF line endings"));
assert_null(ff_parse_line("# cproof chat log — UTF-8, LF line endings"));
}
void