Skip to content

Commit

Permalink
fix ticket:5431
Browse files Browse the repository at this point in the history
- proper handling of modelica:// URI resources for Windows
- fix handling of start values/bindings for strings in the generated FMUs
  • Loading branch information
adrpo committed Jul 5, 2019
1 parent 1d78a97 commit 9fa7b26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/Template/CodegenFMU.tpl
Expand Up @@ -490,7 +490,7 @@ template initValDefault(SimVar var) ::=
case T_REAL(__)
case T_ENUMERATION(__)
case T_BOOL(__) then '0'
case T_STRING(__) then '""'
case T_STRING(__) then 'mmc_mk_scon("")'
else error(sourceInfo(), 'Unknown type for initValDefault: <%unparseType(var.type_)%>')
end initValDefault;

Expand Down Expand Up @@ -3120,7 +3120,7 @@ template optInitValFMU(Option<Exp> exp, String default)
match e
case ICONST(__) then integer
case RCONST(__) then real
case SCONST(__) then '"<%Util.escapeModelicaStringToCString(string)%>"'
case SCONST(__) then 'mmc_mk_scon("<%Util.escapeModelicaStringToCString(string)%>")'
case BCONST(__) then if bool then 1 else 0
case ENUM_LITERAL(__) then '<%index%>'
else default // error(sourceInfo(), 'initial value of unknown type: <%printExpStr(e)%>')
Expand Down Expand Up @@ -3157,7 +3157,7 @@ template ScalarVariableTypeFMU(String attrstr, String unit, String displayUnit,
>>
case T_STRING(__) then
<<
<%attrstr%>.start = <%optInitValFMU(startValue,"\"\"")%>;
<%attrstr%>.start = <%optInitValFMU(startValue,"mmc_mk_scon(\"\")")%>;
>>
case T_ENUMERATION(__) then
<<
Expand Down
46 changes: 43 additions & 3 deletions OMCompiler/SimulationRuntime/c/util/utility.c
Expand Up @@ -160,15 +160,55 @@ void OpenModelica_updateUriMapping(threadData_t *threadData, void *namesAndDirs)

static const char *PATH_NOT_IN_FMU_RESOURCES = "Returning path (%s) not in the resources directory. The FMU might not work as expected if you send it to a different system";

static int isWindows(void)
{
#if defined(__MINGW32__) || defined(_MSC_VER)
return 1;
#else /* not windows */
return 0;
#endif
}

static int isMinGW(void)
{
#if defined(__MINGW32__)
return 1;
#else /* not MinGW */
return 0;
#endif
}

static int isDriveLetter(char c)
{
return isWindows() && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

static int isPathSeparator(char c)
{
return (c == '/') || (isWindows() && (c == '\\'));
}

static modelica_string uriToFilenameRegularPaths(modelica_string uri_om, const char *uri, char buf[PATH_MAX], const char *origUri, const char *resourcesDir)
{
FILE_INFO info = omc_dummyFileInfo;
struct stat stat_buf;
size_t len;
size_t len, i, j = 0;
int uriExists = 0==stat(uri, &stat_buf);
if (resourcesDir) {
if (strlen(resourcesDir)+strlen(uri)+2 < PATH_MAX) {
sprintf(buf, "%s/%s", resourcesDir, uri);
if (isWindows()) {
sprintf(buf, "%s/", resourcesDir);
len = strlen(buf);
for (i = 0; i < strlen(uri); i++)
if (uri[i] != ':')
{
buf[len+j] = (uri[i] == '\\') ? '/' : uri[i];
j++;
}
buf[len+j]='\0';
} else {
sprintf(buf, "%s/%s", resourcesDir, uri);
}
if (!uriExists || 0==stat(buf, &stat_buf)) {
/* The path with resources prepended either exists or the path without resources does not exist
* So re-run uriToFilenameRegularPaths with resourcesDir prepended to the URI
Expand Down Expand Up @@ -206,7 +246,7 @@ static modelica_string uriToFilenameRegularPaths(modelica_string uri_om, const c
return (0==strcmp(uri, buf) && uri_om) ? uri_om : mmc_mk_scon(buf);
}

if (uri[0]=='/') {
if (uri[0]=='/' || (strlen(uri) > 2 && isDriveLetter(uri[0]) && uri[1]==':' && isPathSeparator(uri[2]))) {
/* Absolute path */
return uri_om ? uri_om : mmc_mk_scon(uri);
}
Expand Down

0 comments on commit 9fa7b26

Please sign in to comment.