Skip to content

Commit

Permalink
Remove dependency on shlwapi.h from FMI*.c (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer authored Oct 18, 2023
1 parent e7df82b commit 3ab6e73
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 57 deletions.
1 change: 1 addition & 0 deletions fmusim/FMIZip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <errno.h>
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <process.h>
#include <strsafe.h>
#else
Expand Down
2 changes: 0 additions & 2 deletions include/FMI.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ FMI_STATIC void FMIAppendToLogMessageBuffer(FMIInstance* instance, const char* f

FMI_STATIC void FMIAppendArrayToLogMessageBuffer(FMIInstance* instance, const void* values, size_t nValues, const size_t sizes[], FMIVariableType variableType);

FMI_STATIC FMIStatus FMIURIToPath(const char *uri, char *path, const size_t pathLength);

FMI_STATIC FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength);

FMI_STATIC FMIStatus FMIPlatformBinaryPath(const char *unzipdir, const char *modelIdentifier, FMIVersion fmiVersion, char *platformBinaryPath, size_t size);
Expand Down
65 changes: 14 additions & 51 deletions src/FMI.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <inttypes.h>

#ifdef _WIN32
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#else
#include <stdarg.h>
#include <dlfcn.h>
Expand All @@ -29,9 +27,6 @@ FMIInstance *FMICreateInstance(const char *instanceName, const char *libraryPath
// convert path to unicode
mbstowcs(dllDirectory, libraryPath, MAX_PATH);

// remove the file name
PathRemoveFileSpecW(dllDirectory);

// add the binaries directory temporarily to the DLL path to allow discovery of dependencies
DLL_DIRECTORY_COOKIE dllDirectoryCookie = AddDllDirectory(dllDirectory);

Expand Down Expand Up @@ -246,60 +241,23 @@ void FMIAppendArrayToLogMessageBuffer(FMIInstance* instance, const void* values,
}
}

FMIStatus FMIURIToPath(const char *uri, char *path, const size_t pathLength) {

#ifdef _WIN32
DWORD pcchPath = (DWORD)pathLength;

if (PathCreateFromUrlA(uri, path, &pcchPath, 0) != S_OK) {
return FMIError;
}
#else
const char *scheme1 = "file:///";
const char *scheme2 = "file:/";
FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength) {

strncpy(path, uri, pathLength);
const size_t pathLen = strlen(path);

if (strncmp(uri, scheme1, strlen(scheme1)) == 0) {
strncpy(path, &uri[strlen(scheme1)] - 1, pathLength);
} else if (strncmp(uri, scheme2, strlen(scheme2)) == 0) {
strncpy(path, &uri[strlen(scheme2) - 1], pathLength);
} else {
if (uriLength < strlen(path) + 8) {
return FMIError;
}
#endif

#ifdef _WIN32
const char* sep = "\\";
const char* scheme = "file:///";
#else
const char* sep = "/";
const char* scheme = "file://";
#endif

if (path[strlen(path) - 1] != sep[0]) {
strncat(path, sep, pathLength);
}
strcpy(uri, scheme);

return FMIOK;
}

FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength) {

#ifdef _WIN32
DWORD pcchUri = (DWORD)uriLength;

if (UrlCreateFromPathA(path, uri, &pcchUri, 0) != S_OK) {
return FMIError;
}
#else
const size_t pathLen = strlen(path);

if (uriLength < strlen(path) + 8) {
return FMIError;
}

strcpy(uri, "file://");

size_t p = 7;
size_t p = strlen(scheme);

// percent encode special characters
for (size_t i = 0; i < pathLen; i++) {
Expand All @@ -308,7 +266,13 @@ FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength) {
return FMIError;
}

const char c = path[i];
char c = path[i];

#ifdef _WIN32
if (c == '\\') {
c = '/';
}
#endif

bool encode = true;

Expand All @@ -331,7 +295,6 @@ FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength) {
}

uri[p] = '\0';
#endif

return FMIOK;
}
Expand Down
2 changes: 0 additions & 2 deletions src/FMI2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#ifdef _WIN32
#include <direct.h>
#include "Shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
#define strdup _strdup
#define INTERNET_MAX_URL_LENGTH 2083 // from wininet.h
#else
Expand Down
2 changes: 0 additions & 2 deletions src/FMI3.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#ifdef _WIN32
#include <direct.h>
#include "Shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
#else
#include <stdarg.h>
#include <dlfcn.h>
Expand Down

0 comments on commit 3ab6e73

Please sign in to comment.