Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

A slow-but-working trick for proper local decimal separator handling (tickets 4020 and possibly 4631) #2708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 37 additions & 2 deletions SimulationRuntime/c/simulation/simulation_info_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "util/omc_mmap.h"
#include "solver/model_help.h"

#include <locale.h>

#define MAX_DOUBLE_LEN 100

static inline const char* skipSpace(const char* str)
{
do {
Expand All @@ -53,6 +57,37 @@ static inline const char* skipSpace(const char* str)
} while (1);
}

static double strtod_c(const char *nptr, char **endptr)
{
char loc_decimal_point = *localeconv()->decimal_point;
if (loc_decimal_point == '.') {
return strtod(nptr, endptr);
}

char buf[MAX_DOUBLE_LEN + 1];
strncpy(buf, nptr, MAX_DOUBLE_LEN);

// In case of integer number: [2, ...] -> [2] with ',' as a decimal separator
char *loc_decimal_point_in_buf = strchr(buf, loc_decimal_point);
if (loc_decimal_point_in_buf) {
*loc_decimal_point_in_buf = '\0';
}

// In case of real number: [2.1, ...] -> [2.1] -> [2,1]
char *point_in_buf = strchr(buf, '.');
if (point_in_buf) {
*point_in_buf = loc_decimal_point;
}

// Perform actual parsing
char *end;
double res = strtod(buf, &end);
if (endptr) {
*endptr = (char *)(nptr + (end - buf));
}
return res;
}

static const char* skipValue(const char* str);

static inline const char* skipObjectRest(const char* str, int first)
Expand Down Expand Up @@ -135,7 +170,7 @@ static const char* skipValue(const char* str)
case '9':
{
char *endptr = NULL;
strtod(str,&endptr);
strtod_c(str,&endptr);
if (str == endptr) {
fprintf(stderr, "Not a number, got %.20s\n", str);
abort();
Expand Down Expand Up @@ -175,7 +210,7 @@ static inline const char* assertNumber(const char *str, double expected)
char *endptr = NULL;
double d;
str = skipSpace(str);
d = strtod(str, &endptr);
d = strtod_c(str, &endptr);
if (str == endptr) {
fprintf(stderr, "Expected number, got: %.20s\n", str);
abort();
Expand Down