From 97501a3e348e96b61bda85567e6572efcbfe2d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Sch=C3=BCtt?= Date: Sat, 30 Aug 2025 14:26:08 +0200 Subject: [PATCH] grid: Workaround variable arguments bug on macOS --- src/grid/grid_replay.c | 67 ++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/src/grid/grid_replay.c b/src/grid/grid_replay.c index e4bf4923db..430523a6dc 100644 --- a/src/grid/grid_replay.c +++ b/src/grid/grid_replay.c @@ -35,38 +35,16 @@ static void read_next_line(char line[], int length, FILE *fp) { } } -/******************************************************************************* - * \brief Parses next line from file, expecting it to match "${key} ${format}". - * \author Ole Schuett - ******************************************************************************/ -static void parse_next_line(const char key[], FILE *fp, const char format[], - const int nargs, ...) { - char line[100]; - read_next_line(line, sizeof(line), fp); - - char full_format[100]; - strcpy(full_format, key); - strcat(full_format, " "); - strcat(full_format, format); - - va_list varargs; - va_start(varargs, nargs); - if (vsscanf(line, full_format, varargs) != nargs) { - fprintf(stderr, "Error: Could not parse line.\n"); - fprintf(stderr, "Line: %s\n", line); - fprintf(stderr, "Format: %s\n", full_format); - abort(); - } - va_end(varargs); -} - /******************************************************************************* * \brief Shorthand for parsing a single integer value. * \author Ole Schuett ******************************************************************************/ static int parse_int(const char key[], FILE *fp) { int value; - parse_next_line(key, fp, "%i", 1, &value); + char line[100], format[100]; + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "%s %%i", key); + assert(sscanf(line, format, &value) == 1); return value; } @@ -75,7 +53,10 @@ static int parse_int(const char key[], FILE *fp) { * \author Ole Schuett ******************************************************************************/ static void parse_int3(const char key[], FILE *fp, int vec[3]) { - parse_next_line(key, fp, "%i %i %i", 3, &vec[0], &vec[1], &vec[2]); + char line[100], format[100]; + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "%s %%i %%i %%i", key); + assert(sscanf(line, format, &vec[0], &vec[1], &vec[2]) == 3); } /******************************************************************************* @@ -84,7 +65,10 @@ static void parse_int3(const char key[], FILE *fp, int vec[3]) { ******************************************************************************/ static double parse_double(const char key[], FILE *fp) { double value; - parse_next_line(key, fp, "%le", 1, &value); + char line[100], format[100]; + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "%s %%le", key); + assert(sscanf(line, format, &value) == 1); return value; } @@ -93,7 +77,10 @@ static double parse_double(const char key[], FILE *fp) { * \author Ole Schuett ******************************************************************************/ static void parse_double3(const char key[], FILE *fp, double vec[3]) { - parse_next_line(key, fp, "%le %le %le", 3, &vec[0], &vec[1], &vec[2]); + char line[100], format[100]; + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "%s %%le %%le %%le", key); + assert(sscanf(line, format, &vec[0], &vec[1], &vec[2]) == 3); } /******************************************************************************* @@ -101,10 +88,11 @@ static void parse_double3(const char key[], FILE *fp, double vec[3]) { * \author Ole Schuett ******************************************************************************/ static void parse_double3x3(const char key[], FILE *fp, double mat[3][3]) { - char format[100]; + char line[100], format[100]; for (int i = 0; i < 3; i++) { - sprintf(format, "%i %%le %%le %%le", i); - parse_next_line(key, fp, format, 3, &mat[i][0], &mat[i][1], &mat[i][2]); + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "%s %i %%le %%le %%le", key, i); + assert(sscanf(line, format, &mat[i][0], &mat[i][1], &mat[i][2]) == 3); } } @@ -280,11 +268,12 @@ bool grid_replay(const char *filename, const int cycles, const bool collocate, const int n2 = parse_int("n2", fp); double pab_mutable[n2][n1]; - char format[100]; + char line[100], format[100]; for (int i = 0; i < n2; i++) { for (int j = 0; j < n1; j++) { - sprintf(format, "%i %i %%le", i, j); - parse_next_line("pab", fp, format, 1, &pab_mutable[i][j]); + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "pab %i %i %%le", i, j); + assert(sscanf(line, format, &pab_mutable[i][j]) == 1); } } const double(*pab)[n1] = (const double(*)[n1])pab_mutable; @@ -298,7 +287,8 @@ bool grid_replay(const char *filename, const int cycles, const bool collocate, for (int n = 0; n < ngrid_nonzero; n++) { int i, j, k; double value; - parse_next_line("grid", fp, "%i %i %i %le", 4, &i, &j, &k, &value); + read_next_line(line, sizeof(line), fp); + assert(sscanf(line, "grid %i %i %i %le", &i, &j, &k, &value) == 4); grid_ref->host_buffer[k * npts_local[1] * npts_local[0] + j * npts_local[0] + i] = value; } @@ -307,8 +297,9 @@ bool grid_replay(const char *filename, const int cycles, const bool collocate, memset(hab_ref, 0, n2 * n1 * sizeof(double)); for (int i = o2; i < ncoset(lb_max) + o2; i++) { for (int j = o1; j < ncoset(la_max) + o1; j++) { - sprintf(format, "%i %i %%le", i, j); - parse_next_line("hab", fp, format, 1, &hab_ref[i][j]); + read_next_line(line, sizeof(line), fp); + snprintf(format, sizeof(format), "hab %i %i %%le", i, j); + assert(sscanf(line, format, &hab_ref[i][j]) == 1); } }