Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pathing issues on Windows #49

Merged
merged 5 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions libs/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ install(FILES ${DIST_HDRS} DESTINATION include/${TARGET})
# ==================================================================================================
# Test executables
# ==================================================================================================
add_executable(test_${TARGET}

set(TEST_SRCS
test/test_algorithm.cpp
test/test_Allocators.cpp
test/test_bitset.cpp
Expand All @@ -102,11 +103,16 @@ add_executable(test_${TARGET}
test/test_JobSystem.cpp
test/test_StructureOfArrays.cpp
test/test_utils_main.cpp
test/test_Zip2Iterator.cpp)
test/test_Zip2Iterator.cpp
)

# The Path tests are platform-specific, and fail on Windows
if (NOT WIN32)
list(APPEND SRCS test/test_Path.cpp)
# The Path tests are platform-specific
if (WIN32)
list(APPEND TEST_SRCS test/test_WinPath.cpp)
else()
list(APPEND TEST_SRCS test/test_Path.cpp)
endif()

add_executable(test_${TARGET} ${TEST_SRCS})

target_link_libraries(test_${TARGET} PRIVATE gtest utils tsl math)
4 changes: 1 addition & 3 deletions libs/utils/include/utils/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ class Path {
* @return true if this abstract pathname is not empty
* and starts with a leading '/', false otherwise
*/
bool isAbsolute() const {
return !isEmpty() && m_path.front() == '/';
}
bool isAbsolute() const;

/**
* Splits this object's abstract pathname in a vector of file
Expand Down
42 changes: 28 additions & 14 deletions libs/utils/src/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sstream>
#include <ostream>
#include <iterator>
#include <regex>

#if defined(WIN32)
# include <utils/compiler.h>
Expand Down Expand Up @@ -104,18 +105,26 @@ Path Path::getAbsolutePath() const {
return getCurrentDirectory().concat(*this);
}


#if !defined(WIN32)
bool Path::isAbsolute() const {
return !isEmpty() && m_path.front() == '/';
}
#endif

Path Path::getParent() const {
if (isEmpty()) return "";

std::string result;

// if our path starts with '/', keep the '/'
if (m_path.front() == SEPARATOR) {
result.append(SEPARATOR_STR);
std::vector<std::string> segments(split());

// if our path is absolute with a single segment,
// be sure to keep the prefix component
if (!isAbsolute() || segments.size() > 1) {
segments.pop_back(); // peel the last one
}

std::vector<std::string> segments(split());
segments.pop_back(); // peel the last one
for (auto const& s : segments) {
result.append(s).append(SEPARATOR_STR);
}
Expand Down Expand Up @@ -161,11 +170,17 @@ std::vector<std::string> Path::split() const {
std::vector<std::string> segments;
if (isEmpty()) return segments;

if (m_path.front() == SEPARATOR) segments.push_back(SEPARATOR_STR);

size_t current;
ssize_t next = -1;

// Matches a leading disk designator (C:\), forward slash (/), or back slash (\)
const static std::regex driveDesignationRegex(R"_regex(^([a-zA-Z]:\\|\\|\/))_regex");
std::smatch match;
if (std::regex_search(m_path, match, driveDesignationRegex)) {
segments.push_back(match[0]);
next = match[0].length() - 1;
}

do {
current = size_t(next + 1);
next = m_path.find_first_of(SEPARATOR_STR, current);
Expand All @@ -179,24 +194,24 @@ std::vector<std::string> Path::split() const {
return segments;
}

#if !defined(WIN32)
std::string Path::getCanonicalPath(const std::string& path) {
if (path.empty()) return "";

std::vector<std::string> segments;

// If the path starts with a / we must preserve it
bool starts_with_slash = path.front() == '/';
bool starts_with_slash = path.front() == SEPARATOR;
// If the path does not end with a / we need to remove the
// extra / added by the join process
bool ends_with_slash = path.back() == '/';
bool ends_with_slash = path.back() == SEPARATOR;

size_t current;
ssize_t next = -1;

do {
current = size_t(next + 1);
next = path.find_first_of("/", current);
// Handle both Unix and Windows style separators
next = path.find_first_of("/\\", current);

std::string segment(path.substr(current, next - current));
size_t size = segment.length();
Expand Down Expand Up @@ -230,11 +245,11 @@ std::string Path::getCanonicalPath(const std::string& path) {
// the end that might need to be removed
std::stringstream clean_path;
std::copy(segments.begin(), segments.end(),
std::ostream_iterator<std::string>(clean_path, "/"));
std::ostream_iterator<std::string>(clean_path, SEPARATOR_STR));
std::string new_path = clean_path.str();

if (starts_with_slash && new_path.empty()) {
new_path = "/";
new_path = SEPARATOR_STR;
}

if (!ends_with_slash && new_path.length() > 1) {
Expand All @@ -243,7 +258,6 @@ std::string Path::getCanonicalPath(const std::string& path) {

return new_path;
}
#endif

bool Path::mkdirRecursive() const {
if (isEmpty()) {
Expand Down
144 changes: 70 additions & 74 deletions libs/utils/src/win32/Path.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
/*
Copy link
Member Author

@bejado bejado Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed line endings to UNIX style, hence the massive diff. New function at the very bottom: Path::isAbsolute

* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <utils/Path.h>

#include <direct.h>
#include <Strsafe.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <windows.h>
#include <shlwapi.h>

namespace utils {

bool Path::mkdir() const {
return _mkdir(m_path.c_str()) == 0;
}

Path Path::getCurrentExecutable() {
// First, need to establish resource path.
TCHAR path[MAX_PATH + 1];
Path result;

GetModuleFileName(NULL, path, MAX_PATH + 1);
result.setPath(path);

return result;
}

std::vector<Path> Path::listContents() const {
// Return an empty vector if the path doesn't exist or is not a directory
if (!isDirectory() || !exists()) {
return {};
}

TCHAR dirName[MAX_PATH];
StringCchCopy(dirName, MAX_PATH, c_str());

WIN32_FIND_DATA findData;
HANDLE find = FindFirstFile(dirName, &findData);

std::vector<Path> directory_contents;
do
{
if (findData.cFileName[0] != '.') {
directory_contents.push_back(concat(findData.cFileName));
}
} while (FindNextFile(find, &findData) != 0);

return directory_contents;
}

std::string Path::getCanonicalPath(const std::string& path) {
if (path.empty()) return "";

char canonized[MAX_PATH];
PathCanonicalize(canonized, path.c_str());
return canonized;
}

} // namespace utils
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <utils/Path.h>

#include <direct.h>
#include <Strsafe.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <windows.h>
#include <shlwapi.h>

namespace utils {

bool Path::mkdir() const {
return _mkdir(m_path.c_str()) == 0;
}

Path Path::getCurrentExecutable() {
// First, need to establish resource path.
TCHAR path[MAX_PATH + 1];
Path result;

GetModuleFileName(NULL, path, MAX_PATH + 1);
result.setPath(path);

return result;
}

std::vector<Path> Path::listContents() const {
// Return an empty vector if the path doesn't exist or is not a directory
if (!isDirectory() || !exists()) {
return {};
}

TCHAR dirName[MAX_PATH];
StringCchCopy(dirName, MAX_PATH, c_str());

WIN32_FIND_DATA findData;
HANDLE find = FindFirstFile(dirName, &findData);

std::vector<Path> directory_contents;
do
{
if (findData.cFileName[0] != '.') {
directory_contents.push_back(concat(findData.cFileName));
}
} while (FindNextFile(find, &findData) != 0);

return directory_contents;
}

bool Path::isAbsolute() const {
return !PathIsRelative(m_path.c_str());
}

} // namespace utils
Loading