commit 892a28c56eab76f73578e04f8ae66006862f77ac
parent 25bb564c6ab860d43ff8ca27b8d5b15cfc0e4c24
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 3 Oct 2017 05:53:06 -0400
(path: don't hard-code unix file separator throughout the code)
Diffstat:
5 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/src/archive.cpp b/src/archive.cpp
@@ -196,7 +196,7 @@ int ArchiveReader::extractFile(const Path &path)
int ArchiveReader::extractFile(const Path &path, ostream &stream) noexcept
{
- int status = unzLocateFile(m_zip, path.join('/').c_str(), false);
+ int status = unzLocateFile(m_zip, path.join(false).c_str(), false);
if(status != UNZ_OK)
return status;
@@ -279,7 +279,7 @@ int ArchiveWriter::addFile(const Path &path)
int ArchiveWriter::addFile(const Path &path, istream &stream) noexcept
{
- const int status = zipOpenNewFileInZip(m_zip, path.join('/').c_str(), nullptr,
+ const int status = zipOpenNewFileInZip(m_zip, path.join(false).c_str(), nullptr,
nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
if(status != ZIP_OK)
diff --git a/src/path.cpp b/src/path.cpp
@@ -23,11 +23,13 @@
using namespace std;
+static constexpr char UNIX_SEPARATOR = '/';
+
#ifndef _WIN32
-static constexpr char SEPARATOR = '/';
+static constexpr char NATIVE_SEPARATOR = UNIX_SEPARATOR;
#else
#include <windows.h> // MAX_PATH
-static constexpr char SEPARATOR = '\\';
+static constexpr char NATIVE_SEPARATOR = '\\';
#endif
static constexpr const char *DOT = ".";
@@ -151,8 +153,10 @@ Path Path::dirname() const
return dir;
}
-string Path::join(const char sep) const
+string Path::join(const bool nativeSeparator) const
{
+ const char sep = nativeSeparator ? NATIVE_SEPARATOR : UNIX_SEPARATOR;
+
#ifdef _WIN32
constexpr bool absoluteSlash = false;
#else
@@ -163,7 +167,7 @@ string Path::join(const char sep) const
for(const string &part : m_parts) {
if(!path.empty() || absoluteSlash)
- path += sep ? sep : SEPARATOR;
+ path += sep;
path += part;
}
diff --git a/src/path.hpp b/src/path.hpp
@@ -45,7 +45,7 @@ public:
bool absolute() const { return m_absolute; }
Path dirname() const;
- std::string join(const char sep = 0) const;
+ std::string join(bool nativeSeparator = true) const;
std::string first() const;
std::string last() const;
bool startsWith(const Path &) const;
diff --git a/src/registry.cpp b/src/registry.cpp
@@ -186,7 +186,7 @@ auto Registry::push(const Version *ver, vector<Path> *conflicts) -> Entry
const Path &path = src->targetPath();
m_insertFile->bind(1, entryId);
- m_insertFile->bind(2, path.join('/'));
+ m_insertFile->bind(2, path.join(false));
m_insertFile->bind(3, src->sections());
m_insertFile->bind(4, src->typeOverride());
@@ -303,7 +303,7 @@ auto Registry::getOwner(const Path &path) const -> Entry
{
Entry entry{};
- m_getOwner->bind(1, path.join('/'));
+ m_getOwner->bind(1, path.join(false));
m_getOwner->exec([&] {
fillEntry(m_getOwner, &entry);
diff --git a/test/path.cpp b/test/path.cpp
@@ -101,12 +101,12 @@ TEST_CASE("modify path", M) {
REQUIRE(a.join() == "world");
}
-TEST_CASE("custom separator", M) {
+TEST_CASE("force unix separator", M) {
Path a;
a.append("hello");
a.append("world");
- REQUIRE(a.join('-') == "hello-world");
+ REQUIRE(a.join(false) == "hello/world");
}
TEST_CASE("split input", M) {