encapsulate URIs with a class and improve path interface

pull/67/head
Thomas Fussell 2016-08-10 00:09:54 -10:00
parent 2fff29ba86
commit 20b9217cd8
3 changed files with 138 additions and 112 deletions

View File

@ -26,6 +26,7 @@
#include <string>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/uri.hpp>
#include <xlnt/utils/path.hpp>
namespace xlnt {
@ -102,7 +103,7 @@ public:
relationship();
relationship(const std::string &id, type t, const path &target, target_mode mode);
relationship(const std::string &id, type t, const uri &source, const uri &target, target_mode mode);
/// <summary>
/// Returns a string of the form rId# that identifies the relationship.
@ -122,7 +123,12 @@ public:
/// <summary>
/// Returns the URI of the package part this relationship points to.
/// </summary>
path get_target_uri() const;
uri get_source() const;
/// <summary>
/// Returns the URI of the package part this relationship points to.
/// </summary>
uri get_target() const;
/// <summary>
/// Returns true if and only if rhs is equal to this relationship.
@ -137,7 +143,8 @@ public:
private:
std::string id_;
type type_;
path target_uri_;
uri source_;
uri target_;
target_mode target_mode_;
};

View File

@ -0,0 +1,81 @@
// 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
#pragma once
#include <string>
#include <xlnt/xlnt_config.hpp>
namespace xlnt {
/// <summary>
///
/// </summary>
class XLNT_CLASS uri
{
public:
uri();
uri(const uri &base, const uri &relative);
uri(const uri &base, const path &relative);
uri(const std::string &uri_string);
bool is_relative() const;
bool is_absolute() const;
std::string get_scheme() const;
std::string get_authority() const;
bool has_authentication() const;
std::string get_authentication() const;
std::string get_username() const;
std::string get_password() const;
std::string get_host() const;
bool has_port() const;
std::size_t get_port() const;
path get_path() const;
bool has_query() const;
std::string get_query() const;
bool has_fragment() const;
std::string get_fragment() const;
std::string to_string() const;
uri make_absolute(const uri &base);
uri make_reference(const uri &base);
private:
bool absolute_;
std::string scheme_;
bool has_authentication_;
std::string username_;
std::string password_;
std::string host_;
bool has_port_;
std::size_t port_;
bool has_query_;
std::string query_;
bool has_fragment_;
std::string fragment_;
path path_;
};
} // namespace xlnt

View File

@ -33,37 +33,13 @@ namespace xlnt {
/// <summary>
/// Encapsulates a path that points to location in a filesystem.
/// </summary>
class XLNT_CLASS path : public hashable
class XLNT_CLASS path
{
public:
/// <summary>
/// The parts of this path are held in a container of this type.
/// </summary>
using container = std::vector<std::string>;
/// <summary>
/// Expose the container's iterator as the iterator for this class.
/// </summary>
using iterator = container::iterator;
/// <summary>
/// Expose the container's const_iterator as the const_iterator for this class.
/// </summary>
using const_iterator = container::const_iterator;
/// <summary>
/// Expose the container's reverse_iterator as the reverse_iterator for this class.
/// </summary>
using reverse_iterator = container::reverse_iterator;
/// <summary>
/// Expose the container's const_reverse_iterator as the const_reverse_iterator for this class.
/// </summary>
using const_reverse_iterator = container::const_reverse_iterator;
/// <summary>
/// The system-specific path separator character (e.g. '/' or '\').
/// </summary>
static char separator();
static char system_separator();
/// <summary>
/// Construct an empty path.
@ -93,7 +69,7 @@ public:
bool is_absolute() const;
/// <summary>
/// Return true iff this path is the root of the host's filesystem.
/// Return true iff this path is the root directory.
/// </summary>
bool is_root() const;
@ -106,27 +82,38 @@ public:
/// <summary>
/// Return the last component of this path.
/// </summary>
std::string basename() const;
std::string filename() const;
/// <summary>
/// Return the part of the path following the last . in the basename.
/// Return the part of the path following the last dot in the filename.
/// </summary>
std::string extension() const;
/// <summary>
/// Return a pair of strings resulting from splitting the filename on the last dot.
/// </summary>
std::pair<std::string> split_extension() const;
// conversion
/// <summary>
/// Create a string representing this path separated by the provided
/// separator or the system-default separator if not provided.
/// </summary>
std::string to_string(char sep = separator()) const;
std::vector<std::string> split() const;
/// <summary>
/// Create a string representing this path separated by the provided
/// separator or the system-default separator if not provided.
/// </summary>
std::string string() const;
/// <summary>
/// If this path is relative, append each component of this path
/// to base_path and return the resulting absolute path. Otherwise,
/// the the current path will be returned and base_path will be ignored.
/// </summary>
path make_absolute(const path &base_path) const;
path resolve(const path &base_path) const;
// filesystem attributes
@ -158,95 +145,46 @@ public:
// mutators
/// <summary>
/// Append the provided part to this path and return a reference to this same path
/// so calls can be chained.
/// </summary>
path &append(const std::string &to_append);
/// <summary>
/// Append the provided part to this path and return the result.
/// </summary>
path append(const std::string &to_append) const;
/// <summary>
/// Append the provided part to this path and return a reference to this same path
/// so calls can be chained.
/// </summary>
path &append(const path &to_append);
/// <summary>
/// Append the provided part to this path and return the result.
/// </summary>
path append(const path &to_append) const;
// iterators
/// <summary>
/// An iterator to the first element in this path.
/// </summary>
iterator begin();
/// <summary>
/// An iterator to one past the last element in this path.
/// </summary>
iterator end();
/// <summary>
/// An iterator to the first element in this path.
/// </summary>
const_iterator begin() const;
/// <summary>
/// A const iterator to one past the last element in this path.
/// </summary>
const_iterator end() const;
/// <summary>
/// An iterator to the first element in this path.
/// </summary>
const_iterator cbegin() const;
/// <summary>
/// A const iterator to one past the last element in this path.
/// </summary>
const_iterator cend() const;
/// <summary>
/// A reverse iterator to the last element in this path.
/// </summary>
reverse_iterator rbegin();
/// <summary>
/// A reverse iterator to one before the first element in this path.
/// </summary>
reverse_iterator rend();
/// <summary>
/// A const reverse iterator to the last element in this path.
/// </summary>
const_reverse_iterator rbegin() const;
/// <summary>
/// A const reverse iterator to one before the first element in this path.
/// </summary>
const_reverse_iterator rend() const;
/// <summary>
/// A const reverse iterator to the last element in this path.
/// </summary>
const_reverse_iterator crbegin() const;
/// <summary>
/// A const reverse iterator to one before the first element in this path.
/// </summary>
const_reverse_iterator crend() const;
protected:
std::string to_hash_string() const override;
private:
container parts_;
/// <summary>
/// Returns the character that separates directories in the path.
/// On POSIX style filesystems, this is always '/'.
/// On Windows, this is the character that separates the drive letter from
/// the rest of the path for absolute paths with a drive letter, '/' if the path
/// is absolute and starts with '/', and '/' or '\' for relative paths
/// depending on which splits the path into more directory components.
/// </summary>
char guess_separator() const;
/// <summary>
/// A string that represents this path.
/// </summary>
std::string internal_;
};
} // namespace xlnt
namespace std {
template <>
struct hash<xlnt::path>
{
size_t operator()(const path &p) const
{
return hasher(p);
}
hash<string> hasher;
};
} // namespace std