From 2990ec795a18f04fafdf16b5986a7e390bc90b8a Mon Sep 17 00:00:00 2001 From: "jabber.developer2" Date: Tue, 5 May 2026 13:50:06 +0300 Subject: [PATCH] refactor(flatfile): single-line file header with format-version marker --- src/database_flatfile.h | 12 ++++++------ src/database_flatfile_parser.c | 19 +++++++++++-------- tests/unittests/test_database_export.c | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/database_flatfile.h b/src/database_flatfile.h index 979747b5..1f48dba8 100644 --- a/src/database_flatfile.h +++ b/src/database_flatfile.h @@ -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 --- diff --git a/src/database_flatfile_parser.c b/src/database_flatfile_parser.c index 063efc8d..932878df 100644 --- a/src/database_flatfile_parser.c +++ b/src/database_flatfile_parser.c @@ -342,10 +342,11 @@ ff_skip_bom(FILE* fp) // Format-version helper // ========================================================================= // -// Scan leading '#' comment lines for `# format-version: `. 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: ` 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; diff --git a/tests/unittests/test_database_export.c b/tests/unittests/test_database_export.c index 6f1a71cf..bb6a071a 100644 --- a/tests/unittests/test_database_export.c +++ b/tests/unittests/test_database_export.c @@ -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