From 89aaa077e801bda135b2225a53c443701a4497a7 Mon Sep 17 00:00:00 2001 From: Torsten Sommer Date: Wed, 11 Sep 2024 10:14:07 +0200 Subject: [PATCH] Handle different Boolean types for FMI 1, 2, and 3 fixes #574 --- fmusim/FMIUtil.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/fmusim/FMIUtil.c b/fmusim/FMIUtil.c index 41991346..7e6cd107 100644 --- a/fmusim/FMIUtil.c +++ b/fmusim/FMIUtil.c @@ -245,7 +245,7 @@ FMIStatus FMISetValues( #define PARSE_VALUES(t, f, ...) \ while (strlen(next) > 0) { \ - CALL(FMIRealloc(values, sizeof(t)* ((*nValues) + 1))); \ + CALL(FMIRealloc(values, sizeof(t) * ((*nValues) + 1))); \ t* v = (t*)*values; \ char* end; \ v[*nValues] = f(next, &end, ##__VA_ARGS__); \ @@ -342,38 +342,37 @@ FMIStatus FMIParseValues(FMIMajorVersion fmiMajorVersion, FMIVariableType type, case FMIBooleanType: case FMIClockType: { - size_t size = 0; - - switch (fmiMajorVersion) { - case FMIMajorVersion1: - size = sizeof(fmi1Boolean); - break; - case FMIMajorVersion2: - size = sizeof(fmi2Boolean); - break; - case FMIMajorVersion3: - size = sizeof(fmi2Boolean); - break; - } + const size_t size = FMISizeOfVariableType(FMIBooleanType, fmiMajorVersion); while (strlen(next) > 0) { - CALL(FMIRealloc(values, size * ((*nValues) + 1))); - - fmi3Boolean* v = (fmi3Boolean*)*values; + bool value; size_t delimiter = strcspn(next, " "); if (!strncmp(next, "0", delimiter) || !strncmp(next, "false", delimiter)) { - v[*nValues] = fmi3False; + value = false; } else if (!strncmp(next, "1", delimiter) || !strncmp(next, "true", delimiter)) { - v[*nValues] = fmi3True; + value = true; } else { - FMILogError("Values for Boolean must be one of 0, false, 1, or true.\n"); + FMILogError("Values for Boolean must be one of 0, 1, false, or true.\n"); status = FMIError; goto TERMINATE; } + CALL(FMIRealloc(values, size * ((*nValues) + 1))); + + if (fmiMajorVersion == FMIMajorVersion1) { + fmi1Boolean* v = (fmi1Boolean*)*values; + v[*nValues] = value ? fmi1True : fmi1False; + } else if (fmiMajorVersion == FMIMajorVersion2) { + fmi2Boolean* v = (fmi2Boolean*)*values; + v[*nValues] = value ? fmi2True : fmi2False; + } else if (fmiMajorVersion == FMIMajorVersion3) { + fmi3Boolean* v = (fmi3Boolean*)*values; + v[*nValues] = value ? fmi3True : fmi3False; + } + (*nValues)++; if (strlen(next) == delimiter) {