2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (c) 2014-2020 Thomas Fussell
|
2016-08-03 12:12:18 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2017-04-13 22:58:40 +08:00
|
|
|
|
2018-02-14 13:22:33 +08:00
|
|
|
#include <algorithm>
|
2019-12-27 01:03:12 +08:00
|
|
|
#include <fstream>
|
2018-02-14 13:22:33 +08:00
|
|
|
#include <iterator>
|
2019-12-27 01:03:12 +08:00
|
|
|
#include <sstream>
|
2016-08-03 20:00:16 +08:00
|
|
|
#include <sys/stat.h>
|
2016-08-03 12:12:18 +08:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#elif defined(__linux)
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <sys/types.h>
|
2016-12-24 23:04:57 +08:00
|
|
|
#include <unistd.h>
|
2017-07-03 09:40:11 +08:00
|
|
|
#elif defined(_MSC_VER)
|
2017-07-03 09:39:44 +08:00
|
|
|
#include <codecvt>
|
2016-08-03 12:12:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <xlnt/utils/path.hpp>
|
2019-12-27 01:03:12 +08:00
|
|
|
#include <detail/external/include_windows.hpp>
|
2016-08-03 12:12:18 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-11-20 08:41:21 +08:00
|
|
|
#ifdef WIN32
|
2016-08-03 12:12:18 +08:00
|
|
|
|
|
|
|
char system_separator()
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return '\\';
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 11:06:25 +08:00
|
|
|
bool is_drive_letter(char letter)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return letter >= 'A' && letter <= 'Z';
|
2016-08-13 11:06:25 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 12:12:18 +08:00
|
|
|
bool is_root(const std::string &part)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (part.size() == 1 && part[0] == '/') return true;
|
|
|
|
if (part.size() != 3) return false;
|
2016-08-13 11:06:25 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return is_drive_letter(part[0]) && part[1] == ':' && (part[2] == '\\' || part[2] == '/');
|
2016-08-13 11:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_absolute(const std::string &part)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (!part.empty() && part[0] == '/') return true;
|
|
|
|
if (part.size() < 3) return false;
|
2016-08-13 11:06:25 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return is_root(part.substr(0, 3));
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
char system_separator()
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return '/';
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_root(const std::string &part)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return part == "/";
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 11:26:57 +08:00
|
|
|
bool is_absolute(const std::string &part)
|
|
|
|
{
|
|
|
|
return !part.empty() && part[0] == '/';
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:12:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<std::string> split_path(const std::string &path, char delim)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<std::string> split;
|
|
|
|
std::string::size_type previous_index = 0;
|
|
|
|
auto separator_index = path.find(delim);
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
while (separator_index != std::string::npos)
|
|
|
|
{
|
|
|
|
auto part = path.substr(previous_index, separator_index - previous_index);
|
|
|
|
split.push_back(part);
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
previous_index = separator_index + 1;
|
|
|
|
separator_index = path.find(delim, previous_index);
|
|
|
|
}
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
// Don't add trailing slash
|
|
|
|
if (previous_index < path.size())
|
|
|
|
{
|
|
|
|
split.push_back(path.substr(previous_index));
|
|
|
|
}
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return split;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 20:00:16 +08:00
|
|
|
bool file_exists(const std::string &path)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
struct stat info;
|
|
|
|
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFREG);
|
2016-08-03 20:00:16 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 12:12:18 +08:00
|
|
|
bool directory_exists(const std::string path)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
struct stat info;
|
|
|
|
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
char path::system_separator()
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return ::system_separator();
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
path::path()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
path::path(const std::string &path_string)
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
std::remove_copy(path_string.begin(), path_string.end(), std::back_inserter(internal_), '\"');
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:09:41 +08:00
|
|
|
path::path(const std::string &path_string, char sep)
|
|
|
|
: internal_(path_string)
|
|
|
|
{
|
|
|
|
char curr_sep = guess_separator();
|
|
|
|
if (curr_sep != sep)
|
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
for (char &c : internal_) // simple find and replace
|
2018-07-02 07:09:41 +08:00
|
|
|
{
|
|
|
|
if (c == curr_sep)
|
|
|
|
{
|
|
|
|
c = sep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
// general attributes
|
2016-08-03 12:12:18 +08:00
|
|
|
|
|
|
|
bool path::is_relative() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return !is_absolute();
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool path::is_absolute() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return ::is_absolute(internal_);
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool path::is_root() const
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
return ::is_root(internal_);
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
path path::parent() const
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
if (is_root()) return *this;
|
|
|
|
|
|
|
|
auto split_path = split();
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
split_path.pop_back();
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
if (split_path.empty())
|
|
|
|
{
|
|
|
|
return path("");
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
path result;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
for (const auto &component : split_path)
|
|
|
|
{
|
|
|
|
result = result.append(component);
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
|
|
|
return result;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
std::string path::filename() const
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
auto split_path = split();
|
2016-12-24 23:04:57 +08:00
|
|
|
return split_path.empty() ? "" : split_path.back();
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
std::string path::extension() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
auto base = filename();
|
|
|
|
auto last_dot = base.find_last_of('.');
|
|
|
|
|
|
|
|
return last_dot == std::string::npos ? "" : base.substr(last_dot + 1);
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 11:06:25 +08:00
|
|
|
std::pair<std::string, std::string> path::split_extension() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
auto base = filename();
|
|
|
|
auto last_dot = base.find_last_of('.');
|
2016-08-13 11:06:25 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return {base.substr(0, last_dot), base.substr(last_dot + 1)};
|
2016-08-13 11:06:25 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 12:12:18 +08:00
|
|
|
// conversion
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const std::string &path::string() const
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
return internal_;
|
|
|
|
}
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2017-04-13 22:58:40 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::wstring path::wstring() const
|
|
|
|
{
|
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
|
|
|
|
return convert.from_bytes(string());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
std::vector<std::string> path::split() const
|
|
|
|
{
|
|
|
|
return split_path(internal_, guess_separator());
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
path path::resolve(const path &base_path) const
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (is_absolute())
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
path copy(base_path.internal_);
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &part : split())
|
|
|
|
{
|
2018-10-05 21:30:54 +08:00
|
|
|
if (part == "..")
|
|
|
|
{
|
|
|
|
copy = copy.parent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
copy = copy.append(part);
|
2016-12-24 23:04:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
return copy;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// filesystem attributes
|
|
|
|
|
|
|
|
bool path::exists() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return is_file() || is_directory();
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool path::is_directory() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return directory_exists(string());
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool path::is_file() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return file_exists(string());
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// filesystem
|
|
|
|
|
|
|
|
std::string path::read_contents() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::ifstream f(string());
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << f.rdbuf();
|
2016-08-03 12:12:18 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return ss.str();
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
// append
|
2016-08-03 12:12:18 +08:00
|
|
|
|
|
|
|
path path::append(const std::string &to_append) const
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
path copy(internal_);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
if (!internal_.empty() && internal_.back() != guess_separator())
|
|
|
|
{
|
|
|
|
copy.internal_.push_back(guess_separator());
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
copy.internal_.append(to_append);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
return copy;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
path path::append(const path &to_append) const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
path copy(internal_);
|
2016-08-13 11:06:25 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &component : to_append.split())
|
|
|
|
{
|
|
|
|
copy = copy.append(component);
|
|
|
|
}
|
2016-08-13 11:06:25 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return copy;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
char path::guess_separator() const
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2016-08-13 11:06:25 +08:00
|
|
|
if (system_separator() == '/' || internal_.empty() || internal_.front() == '/') return '/';
|
2016-08-12 12:22:14 +08:00
|
|
|
if (is_absolute()) return internal_.at(2);
|
2016-08-13 11:06:25 +08:00
|
|
|
return internal_.find('\\') != std::string::npos ? '\\' : '/';
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
path path::relative_to(const path &base_path) const
|
|
|
|
{
|
|
|
|
if (is_relative()) return *this;
|
|
|
|
|
|
|
|
auto base_split = base_path.split();
|
|
|
|
auto this_split = split();
|
|
|
|
auto index = std::size_t(0);
|
|
|
|
|
|
|
|
while (index < base_split.size() && index < this_split.size() && base_split[index] == this_split[index])
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = path();
|
|
|
|
|
|
|
|
for (auto i = index; i < this_split.size(); i++)
|
|
|
|
{
|
|
|
|
result = result.append(this_split[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-31 11:52:57 +08:00
|
|
|
bool path::operator==(const path &other) const
|
2016-08-03 12:12:18 +08:00
|
|
|
{
|
2017-03-31 11:52:57 +08:00
|
|
|
return internal_ == other.internal_;
|
2016-08-03 12:12:18 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:19:56 +08:00
|
|
|
bool path::operator!=(const path &other) const
|
|
|
|
{
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:12:18 +08:00
|
|
|
} // namespace xlnt
|