Skip to content

Commit 0c13ea9

Browse files
authored
Decode URIs containing % (#9688)
1 parent d2cd102 commit 0c13ea9

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

OMCompiler/SimulationRuntime/c/simulation/omc_simulation_util.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ extern const char* OpenModelica_parseFmuResourcePath(const char *path)
5555
}
5656
}
5757
#endif
58-
return path;
58+
{
59+
char *res = strdup(path);
60+
OpenModelica_decode_uri_inplace(res);
61+
return res;
62+
}
5963
}
6064
return NULL;
6165
}

OMCompiler/SimulationRuntime/c/util/utility.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static modelica_string lookupDirectoryFromName(const char *name, void *nameDirAr
270270
return obj[1];
271271
}
272272

273-
static void getIdent(const char *str, char *this, const char **next)
273+
static void getIdent(char *str, char *this, char **next)
274274
{
275275
while (*str != 0 && *str != '.' && *str != '/') {
276276
*(this++) = *(str++);
@@ -279,6 +279,33 @@ static void getIdent(const char *str, char *this, const char **next)
279279
*next = str;
280280
}
281281

282+
extern void OpenModelica_decode_uri_inplace(char *src)
283+
{
284+
char *tmp = src;
285+
while (*src) {
286+
if (*src == '+') *(tmp++) = ' ';
287+
else if (*src == '%' && src[1]) {
288+
char buf[3];
289+
int i;
290+
buf[0] = src[1];
291+
buf[1] = src[2];
292+
buf[2] = '\0';
293+
errno = 0;
294+
i = strtol(buf,NULL,16);
295+
if (errno) {
296+
*(tmp++) = *src;
297+
errno = 0;
298+
} else {
299+
*(tmp++) = i;
300+
*tmp = 0;
301+
src += 2;
302+
}
303+
} else *(tmp++) = *src;
304+
src++;
305+
}
306+
*tmp = '\0';
307+
}
308+
282309
extern modelica_string OpenModelica_uriToFilename_impl(threadData_t *threadData, modelica_string uri_om, const char *resourcesDir)
283310
{
284311
#if defined(_MSC_VER)
@@ -287,8 +314,10 @@ extern modelica_string OpenModelica_uriToFilename_impl(threadData_t *threadData,
287314

288315
FILE_INFO info = omc_dummyFileInfo;
289316
char buf[PATH_MAX];
290-
const char *uri = MMC_STRINGDATA(uri_om);
317+
char uribuf[MMC_STRLEN(uri_om)+1];
318+
char *uri = uribuf;
291319
modelica_string dir;
320+
strcpy(uri, MMC_STRINGDATA(uri_om));
292321
if (0==strncasecmp(uri, "modelica://", 11)) {
293322
omc_stat_t stat_buf;
294323
uri += 11;
@@ -315,6 +344,7 @@ extern modelica_string OpenModelica_uriToFilename_impl(threadData_t *threadData,
315344
}
316345
}
317346
}
347+
OpenModelica_decode_uri_inplace(uri);
318348
/* We found where the package is stored */
319349
while (1) {
320350
if (*uri == '.') {

OMCompiler/SimulationRuntime/c/util/utility.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ extern int OpenModelica_regexImpl(const char* str, const char* re, const int max
124124
extern int OpenModelica_regex(const char* str, const char* re, int maxn, int extended, int sensitive, const char **result);
125125
#endif
126126

127+
extern void OpenModelica_decode_uri_inplace(char *uri);
128+
127129
extern modelica_string OpenModelica_uriToFilename_impl(threadData_t *threadData, modelica_string uri, const char *resourcesDir);
128130
#define OpenModelica_uriToFilename(URI) OpenModelica_uriToFilename_impl(threadData, URI, NULL)
129131
#define OpenModelica__uriToFilename(URI) OpenModelica_uriToFilename(URI)

OMCompiler/SimulationRuntime/fmi/export/openmodelica/fmu2_model_interface.c.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
579579
if (fmuResourceLocation) {
580580
comp->fmuData->modelData->resourcesDir = functions->allocateMemory(1 + strlen(fmuResourceLocation), sizeof(char));
581581
strcpy(comp->fmuData->modelData->resourcesDir, fmuResourceLocation);
582+
free(fmuResourceLocation);
582583
} else {
583584
FILTERED_LOG(comp, fmi2OK, LOG_STATUSWARNING, "fmi2Instantiate: Ignoring unknown resource URI: %s", fmuResourceLocation)
584585
}

testsuite/openmodelica/fmi/ModelExchange/2.0/FMUResourceTest.mos

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,33 @@
88

99
loadModel(Modelica, {"3.2.3"}); getErrorString();
1010
loadFile("./FMUResourceTest/package.mo"); getErrorString();
11+
mkdir("@");
12+
echo(false);
13+
cd("@");
14+
echo(true);
1115
translateModelFMU(FMUResourceTest.TestResource, version="2.0"); getErrorString();
1216
importFMU("FMUResourceTest.TestResource.fmu"); getErrorString();
1317
loadFile("FMUResourceTest_TestResource_me_FMU.mo"); getErrorString();
14-
buildModel(FMUResourceTest_TestResource_me_FMU); getErrorString();
15-
18+
echo(false);
19+
res := simulate(FMUResourceTest_TestResource_me_FMU);
20+
echo(true);
21+
getErrorString();
22+
res.resultFile;
1623

1724
// Result:
1825
// true
1926
// ""
2027
// true
2128
// ""
29+
// true
30+
// true
2231
// "FMUResourceTest.TestResource.fmu"
2332
// ""
2433
// "FMUResourceTest_TestResource_me_FMU.mo"
2534
// ""
2635
// true
2736
// ""
28-
// {"FMUResourceTest_TestResource_me_FMU","FMUResourceTest_TestResource_me_FMU_init.xml"}
37+
// true
2938
// ""
39+
// "FMUResourceTest_TestResource_me_FMU_res.mat"
3040
// endResult

testsuite/openmodelica/fmi/ModelExchange/2.0/FMUResourceTest/package.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package FMUResourceTest "Test table resource"
2323
extends Test0(t_new(
2424
tableOnFile=true,
2525
tableName="a",
26-
fileName=loadResource("modelica://FMUResourceTest/Resources/table_test_utf8.txt")));
26+
fileName=loadResource("modelica://FMUResourceTest/Resources/table_test%40utf8.txt")));
2727
annotation (experiment(StartTime=0, StopTime=100));
2828
end TestResource;
2929
end FMUResourceTest;

0 commit comments

Comments
 (0)