Fix use of strtok on windows. (#261)

Fix use of strtok on windows.
This commit is contained in:
Matthieu Gautier 2019-08-19 17:42:55 +02:00 committed by GitHub
commit e25b27b354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -94,6 +94,12 @@ std::string computeRelativePath(const std::string& path, const std::string& abso
return relativePath; return relativePath;
} }
#ifdef _WIN32
# define STRTOK strtok_s
#else
# define STRTOK strtok_r
#endif
/* Warning: the relative path must be with slashes */ /* Warning: the relative path must be with slashes */
std::string computeAbsolutePath(const std::string& path, const std::string& relativePath) std::string computeAbsolutePath(const std::string& path, const std::string& relativePath)
{ {
@ -122,20 +128,20 @@ std::string computeAbsolutePath(const std::string& path, const std::string& rela
char* cRelativePath = strdup(relativePath.c_str()); char* cRelativePath = strdup(relativePath.c_str());
#endif #endif
char* saveptr = nullptr; char* saveptr = nullptr;
char* token = strtok_r(cRelativePath, "/", &saveptr); char* token = STRTOK(cRelativePath, "/", &saveptr);
while (token != NULL) { while (token != NULL) {
if (std::string(token) == "..") { if (std::string(token) == "..") {
absolutePath = removeLastPathElement(absolutePath, true, false); absolutePath = removeLastPathElement(absolutePath, true, false);
token = strtok_r(NULL, "/", &saveptr); token = STRTOK(NULL, "/", &saveptr);
} else if (strcmp(token, ".") && strcmp(token, "")) { } else if (strcmp(token, ".") && strcmp(token, "")) {
absolutePath += std::string(token); absolutePath += std::string(token);
token = strtok_r(NULL, "/", &saveptr); token = STRTOK(NULL, "/", &saveptr);
if (token != NULL) { if (token != NULL) {
absolutePath += SEPARATOR; absolutePath += SEPARATOR;
} }
} else { } else {
token = strtok_r(NULL, "/", &saveptr); token = STRTOK(NULL, "/", &saveptr);
} }
} }
free(cRelativePath); free(cRelativePath);