| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libepubgen project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "EPUBPath.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <iterator> |
| 14 |
#include <stdexcept> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
#include <boost/algorithm/string/classification.hpp> |
| 18 |
#include <boost/algorithm/string/join.hpp> |
| 19 |
#include <boost/algorithm/string/split.hpp> |
| 20 |
#include <boost/algorithm/string/trim.hpp> |
| 21 |
|
| 22 |
namespace libepubgen |
| 23 |
{ |
| 24 |
|
| 25 |
namespace algorithm = boost::algorithm; |
| 26 |
|
| 27 |
namespace |
| 28 |
{ |
| 29 |
|
| 30 |
struct FindDots |
| 31 |
{ |
| 32 |
bool operator()(const std::string &component) |
| 33 |
{ |
| 34 |
return ("." == component) || (".." == component); |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
bool findAnyDots(const std::vector<std::string> &components) |
| 39 |
{ |
| 40 |
return components.end() != find_if(components.begin(), components.end(), FindDots()); |
| 41 |
} |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
const std::string EPUBPath::Relative::str() const |
| 46 |
{ |
| 47 |
return algorithm::join(m_components, "/"); |
| 48 |
} |
| 49 |
|
| 50 |
EPUBPath::Relative::Relative(const std::vector<std::string> &components) |
| 51 |
: m_components(components) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
EPUBPath::EPUBPath(const std::string &path) |
| 56 |
: m_components() |
| 57 |
, m_title() |
| 58 |
, m_chapters() |
| 59 |
{ |
| 60 |
const std::string trimmed(algorithm::trim_left_copy_if(path, algorithm::is_any_of("/"))); |
| 61 |
algorithm::split(m_components, trimmed, algorithm::is_any_of("/"), algorithm::token_compress_on); |
| 62 |
|
| 63 |
if (m_components.empty() || m_components.back().empty() || findAnyDots(m_components)) |
| 64 |
throw std::logic_error("invalid path"); |
| 65 |
} |
| 66 |
|
| 67 |
void EPUBPath::swap(EPUBPath &other) |
| 68 |
{ |
| 69 |
m_components.swap(other.m_components); |
| 70 |
} |
| 71 |
|
| 72 |
void EPUBPath::append(const EPUBPath &subpath) |
| 73 |
{ |
| 74 |
m_components.insert(m_components.end(), subpath.m_components.begin(), subpath.m_components.end()); |
| 75 |
} |
| 76 |
|
| 77 |
void EPUBPath::appendComponent(const std::string &pathComponent) |
| 78 |
{ |
| 79 |
if (std::string::npos != pathComponent.find('/')) |
| 80 |
throw std::logic_error("the component cannot be path"); |
| 81 |
if (("." == pathComponent) || (".." == pathComponent)) |
| 82 |
throw std::logic_error("the component cannot be relative"); |
| 83 |
|
| 84 |
m_components.push_back(pathComponent); |
| 85 |
} |
| 86 |
|
| 87 |
const std::string EPUBPath::str() const |
| 88 |
{ |
| 89 |
return algorithm::join(m_components, "/"); |
| 90 |
} |
| 91 |
|
| 92 |
const EPUBPath::Relative EPUBPath::relativeTo(const EPUBPath &base) const |
| 93 |
{ |
| 94 |
typedef std::vector<std::string> Path_t; |
| 95 |
typedef Path_t::const_iterator PathIter_t; |
| 96 |
|
| 97 |
const PathIter_t baseEnd = base.m_components.end() - 1; |
| 98 |
const Path_t::size_type baseSize = base.m_components.size() - 1; |
| 99 |
|
| 100 |
const std::pair<PathIter_t, PathIter_t> mismatch( |
| 101 |
std::mismatch( |
| 102 |
m_components.begin(), |
| 103 |
// If base has more or the same number components than this, leave out the last component. |
| 104 |
// This ensures we always get path starting with ../, even if there is a full match. |
| 105 |
(baseSize >= m_components.size()) ? (m_components.end() - 1) : (m_components.begin() + baseSize), |
| 106 |
base.m_components.begin())); |
| 107 |
|
| 108 |
Path_t components; |
| 109 |
|
| 110 |
std::fill_n(std::back_inserter(components), std::distance(mismatch.second, baseEnd), std::string("..")); |
| 111 |
std::copy(mismatch.first, m_components.end(), std::back_inserter(components)); |
| 112 |
|
| 113 |
return Relative(components); |
| 114 |
} |
| 115 |
|
| 116 |
void EPUBPath::appendTitle(const std::string &title) |
| 117 |
{ |
| 118 |
m_title += title; |
| 119 |
} |
| 120 |
|
| 121 |
void EPUBPath::addChapter(const std::string &chapter) |
| 122 |
{ |
| 123 |
m_chapters.push_back(chapter); |
| 124 |
} |
| 125 |
|
| 126 |
const std::vector<std::string> &EPUBPath::getChapters() const |
| 127 |
{ |
| 128 |
return m_chapters; |
| 129 |
} |
| 130 |
|
| 131 |
std::string EPUBPath::getTitle() const |
| 132 |
{ |
| 133 |
return m_title; |
| 134 |
} |
| 135 |
|
| 136 |
bool operator==(const EPUBPath &left, const EPUBPath &right) |
| 137 |
{ |
| 138 |
return left.m_components == right.m_components; |
| 139 |
} |
| 140 |
|
| 141 |
bool operator!=(const EPUBPath &left, const EPUBPath &right) |
| 142 |
{ |
| 143 |
return !(left == right); |
| 144 |
} |
| 145 |
|
| 146 |
const EPUBPath operator/(const EPUBPath &base, const EPUBPath &subpath) |
| 147 |
{ |
| 148 |
EPUBPath path(base); |
| 149 |
path.append(subpath); |
| 150 |
return path; |
| 151 |
} |
| 152 |
|
| 153 |
const EPUBPath operator/(const EPUBPath &base, const std::string &pathComponent) |
| 154 |
{ |
| 155 |
EPUBPath path(base); |
| 156 |
path.appendComponent(pathComponent); |
| 157 |
return path; |
| 158 |
} |
| 159 |
|
| 160 |
void swap(EPUBPath &left, EPUBPath &right) |
| 161 |
{ |
| 162 |
left.swap(right); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |