Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle different Boolean types for FMI 1, 2, and 3 #576

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions fmusim/FMIUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__); \
Expand Down Expand Up @@ -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) {
Expand Down