xlnt/source/utils/path.cpp

325 lines
6.3 KiB
C++
Raw Normal View History

2016-08-03 12:12:18 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
//
// 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
#include <fstream>
#include <sstream>
#include <sys/stat.h>
2016-08-03 12:12:18 +08:00
#ifdef __APPLE__
#include <mach-o/dyld.h>
#elif defined(__linux)
#include <unistd.h>
#include <linux/limits.h>
#include <sys/types.h>
#endif
#include <detail/include_windows.hpp>
2016-08-03 12:12:18 +08:00
#include <xlnt/utils/path.hpp>
namespace {
2016-11-20 08:41:21 +08:00
#ifdef WIN32
2016-08-03 12:12:18 +08:00
char system_separator()
{
return '\\';
}
2016-08-13 11:06:25 +08:00
bool is_drive_letter(char letter)
{
return letter >= 'A' && letter <= 'Z';
}
2016-08-03 12:12:18 +08:00
bool is_root(const std::string &part)
{
2016-08-13 11:06:25 +08:00
if (part.size() == 1 && part[0] == '/') return true;
if (part.size() != 3) return false;
return is_drive_letter(part[0]) && part[1] == ':'
&& (part[2] == '\\' || part[2] == '/');
}
bool is_absolute(const std::string &part)
{
if (!part.empty() && part[0] == '/') return true;
if (part.size() < 3) return false;
return is_root(part.substr(0, 3));
2016-08-03 12:12:18 +08:00
}
#else
char system_separator()
{
return '/';
}
bool is_root(const std::string &part)
{
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)
{
std::vector<std::string> split;
std::string::size_type previous_index = 0;
auto separator_index = path.find(delim);
while (separator_index != std::string::npos)
{
auto part = path.substr(previous_index, separator_index - previous_index);
split.push_back(part);
previous_index = separator_index + 1;
separator_index = path.find(delim, previous_index);
}
// Don't add trailing slash
if (previous_index < path.size())
{
split.push_back(path.substr(previous_index));
}
return split;
}
bool file_exists(const std::string &path)
{
struct stat info;
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFREG);
}
2016-08-03 12:12:18 +08:00
bool directory_exists(const std::string path)
{
struct stat info;
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
}
} // namespace
namespace xlnt {
char path::system_separator()
2016-08-03 12:12:18 +08:00
{
return ::system_separator();
2016-08-03 12:12:18 +08:00
}
path::path()
{
}
path::path(const std::string &path_string) : internal_(path_string)
2016-08-03 12:12:18 +08:00
{
}
// general attributes
bool path::is_relative() const
{
return !is_absolute();
}
bool path::is_absolute() const
{
2016-08-13 11:06:25 +08:00
return ::is_absolute(internal_);
2016-08-03 12:12:18 +08:00
}
bool path::is_root() const
{
return ::is_root(internal_);
2016-08-03 12:12:18 +08:00
}
path path::parent() const
{
if (is_root()) return *this;
auto split_path = split();
split_path.pop_back();
if (split_path.empty())
{
return path("");
}
path result;
for (const auto &component : split_path)
{
result = result.append(component);
}
2016-08-03 12:12:18 +08:00
return result;
}
std::string path::filename() const
2016-08-03 12:12:18 +08:00
{
auto split_path = split();
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
{
auto base = filename();
2016-08-05 13:52:05 +08:00
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
{
auto base = filename();
auto last_dot = base.find_last_of('.');
return{ base.substr(0, last_dot), base.substr(last_dot + 1) };
}
2016-08-03 12:12:18 +08:00
// conversion
std::string path::string() const
2016-08-03 12:12:18 +08:00
{
return internal_;
}
2016-08-03 12:12:18 +08:00
std::vector<std::string> path::split() const
{
return split_path(internal_, guess_separator());
2016-08-03 12:12:18 +08:00
}
path path::resolve(const path &base_path) const
2016-08-03 12:12:18 +08:00
{
if (is_absolute())
{
return *this;
}
path copy(base_path.internal_);
2016-08-03 12:12:18 +08:00
for (const auto &part : split())
2016-08-03 12:12:18 +08:00
{
copy = copy.append(part);
2016-08-03 12:12:18 +08:00
}
return copy;
2016-08-03 12:12:18 +08:00
}
// filesystem attributes
bool path::exists() const
{
return is_file() || is_directory();
}
bool path::is_directory() const
{
return directory_exists(string());
2016-08-03 12:12:18 +08:00
}
bool path::is_file() const
{
return file_exists(string());
2016-08-03 12:12:18 +08:00
}
// filesystem
std::string path::read_contents() const
{
std::ifstream f(string());
2016-08-03 12:12:18 +08:00
std::ostringstream ss;
ss << f.rdbuf();
return ss.str();
}
// append
2016-08-03 12:12:18 +08:00
path path::append(const std::string &to_append) const
{
path copy(internal_);
if (!internal_.empty() && internal_.back() != guess_separator())
{
copy.internal_.push_back(guess_separator());
}
copy.internal_.append(to_append);
return copy;
2016-08-03 12:12:18 +08:00
}
path path::append(const path &to_append) const
{
2016-08-13 11:06:25 +08:00
path copy(internal_);
for (const auto &component : to_append.split())
{
copy = copy.append(component);
}
return copy;
2016-08-03 12:12:18 +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 '/';
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
}
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;
}
bool operator==(const path &left, const path &right)
2016-08-03 12:12:18 +08:00
{
return left.internal_ == right.internal_;
2016-08-03 12:12:18 +08:00
}
} // namespace xlnt