Skip to content

Filesystem path #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vs
CMakeSettings.json
cmake-build-*
_build
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ matrix:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7']
env: COMPILER='g++-7'
env: [COMPILER='g++-7', BUILD_FILESYSTEM_PATH_FUNCTION='OFF']

- os: linux
compiler: gcc
addons: &gcc8
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-8']
env: COMPILER='g++-8'
env: [COMPILER='g++-8', BUILD_FILESYSTEM_PATH_FUNCTION='ON']

# 3/ OSX Clang Builds
- os: osx
osx_image: xcode10
compiler: clang
env: [BUILD_FILESYSTEM_PATH_FUNCTION='OFF']

install:
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps_"
Expand All @@ -68,7 +69,7 @@ before_script:
script:
- mkdir _build
- cd _build
- cmake -DSkyr_BUILD_TESTS=ON -DSkyr_BUILD_DOCS=OFF -DSkyr_BUILD_EXAMPLES=OFF ..
- cmake -DSkyr_BUILD_TESTS=ON -DSkyr_BUILD_DOCS=OFF -DSkyr_BUILD_EXAMPLES=OFF -DSkyr_BUILD_FILESYSTEM_PATH_FUNCTIONS=$BUILD_FILESYSTEM_PATH_FUNCTIONS ..
- make -j 8
- make test

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ option(Skyr_BUILD_EXAMPLES "Build the URL examples." OFF)
option(Skyr_FULL_WARNINGS "Build the library with all warnings turned on." ON)
option(Skyr_WARNINGS_AS_ERRORS "Treat warnings as errors." ON)
option(Skyr_USE_STATIC_CRT "Use static C Runtime library (/MT or MTd)." ON)
option(Skyr_BUILD_FILESYSTEM_PATH_FUNCTIONS "Build the filesystem path functions" ON)

find_package(Threads REQUIRED)

Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ This library provides:
This project requires the availability of a C++17 compliant compiler
and standard library.

### Building with `CMake` and `Make`
### Building with `CMake` and `Ninja`

From a terminal, execute the following sequence of commands:

```bash
> mkdir _build
> cd _build
> cmake ..
> make -j4
> cmake .. -G "Ninja"
> ninja
```

To run the tests, run `ctest` from the terminal while in the
To run the tests, run `ninja test` from the terminal while in the
`_build` directory:

```bash
> ctest
> ninja test
```

### Building with `CMake` and `Visual Studio 2017`
Expand All @@ -63,7 +63,7 @@ These examples are based on the
To build the examples, run `cmake` as follows:

```bash
> cmake .. -DSkyr_BUILD_EXAMPLES=ON
> cmake .. -G "Ninja" -DSkyr_BUILD_EXAMPLES=ON
```

### Creating a URL without a base URL
Expand Down Expand Up @@ -125,13 +125,13 @@ This gives the output: `https://example.org/%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%F0%9F

## Installation

### Installing with `CMake` and `Make`
### Installing with `CMake` and `Ninja`

```bash
> cmake .. DCMAKE_INSTALL_PREFIX=$PREFIX
> make -j4
> make test # optional
> make install
> cmake .. -G "Ninja" -DCMAKE_INSTALL_PREFIX=$PREFIX
> ninja
> ninja test # optional
> ninja install
```

Where `$PREFIX` is the location where you want to install the
Expand Down
29 changes: 29 additions & 0 deletions include/skyr/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef SKYR_FILESYSTEM_PATH_HPP
#define SKYR_FILESYSTEM_PATH_HPP

#include <system_error>
#include <filesystem>
#include <skyr/expected.hpp>
#include <skyr/url.hpp>

namespace skyr {
namespace filesystem {
enum class path_errc {
invalid_path=1,
percent_decoding_error=2,
};

std::error_code make_error_code(path_errc error);

expected<url, std::error_code> from_path(const std::filesystem::path &path);

expected<std::filesystem::path, std::error_code> to_path(const url &input);
} // namespace filesystem
} // namespace skyr

#endif // SKYR_FILESYSTEM_PATH_HPP
51 changes: 40 additions & 11 deletions include/skyr/url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class url {
/// \returns The underlying URL string
string_type href() const;

///
/// @{
/// \tparam Source The input string type
/// \param input The input string
/// \returns An error on failure to parse the new URL
Expand All @@ -155,6 +155,9 @@ class url {
return set_href(std::move(bytes.value()));
}

expected<void, std::error_code> set_href(string_type &&href);
/// @}

/// The URL string
///
/// Equivalent to `skyr::serialize(url_).value()`
Expand All @@ -168,6 +171,7 @@ class url {
/// \returns The [URL protocol](https://url.spec.whatwg.org/#dom-url-protocol)
string_type protocol() const;

/// @{
/// Sets the [URL protocol](https://url.spec.whatwg.org/#dom-url-protocol)
///
/// \param protocol The new URL protocol
Expand All @@ -186,9 +190,13 @@ class url {
return set_protocol(std::move(bytes.value()));
}

expected<void, std::error_code> set_protocol(string_type &&protocol);
/// @}

/// \returns The [URL username](https://url.spec.whatwg.org/#dom-url-username)
string_type username() const;

/// @{
/// Sets the [URL username](https://url.spec.whatwg.org/#dom-url-username)
///
/// \param username The new username
Expand All @@ -207,13 +215,17 @@ class url {
return set_username(std::move(bytes.value()));
}

expected<void, std::error_code> set_username(string_type &&username);
/// @}

/// The [URL password](https://url.spec.whatwg.org/#dom-url-password)
///
/// Equivalent to: `url_.password? url_.password.value() : string_type()`
///
/// \returns The URL password
string_type password() const;

/// @{
/// Sets the [URL password](https://url.spec.whatwg.org/#dom-url-password)
///
/// \param password The new password
Expand All @@ -231,10 +243,14 @@ class url {
}
return set_password(std::move(bytes.value()));
}

expected<void, std::error_code> set_password(string_type &&password);
/// @}

/// \returns The [URL host](https://url.spec.whatwg.org/#dom-url-host)
string_type host() const;

/// @{
/// Sets the [URL host](https://url.spec.whatwg.org/#dom-url-host)
///
/// \param host The new URL host
Expand All @@ -253,9 +269,13 @@ class url {
return set_host(std::move(bytes.value()));
}

expected<void, std::error_code> set_host(string_type &&host);
/// @}

/// \returns The [URL hostname](https://url.spec.whatwg.org/#dom-url-hostname)
string_type hostname() const;

/// @{
/// Sets the [URL hostname](https://url.spec.whatwg.org/#dom-url-hostname)
///
/// \param hostname The new URL host name
Expand All @@ -274,6 +294,9 @@ class url {
return set_hostname(std::move(bytes.value()));
}

expected<void, std::error_code> set_hostname(string_type &&hostname);
/// @}

/// \returns The [URL port](https://url.spec.whatwg.org/#dom-url-port)
string_type port() const;

Expand All @@ -287,6 +310,7 @@ class url {
std::strtoul(port_first, &port_last, 10));
}

/// @{
/// Sets the [URL port](https://url.spec.whatwg.org/#dom-url-port)
///
/// \param port The new port
Expand All @@ -296,11 +320,15 @@ class url {
return set_port_impl(port);
}

expected<void, std::error_code> set_port(string_type &&port);
/// @}

/// Returns the [URL pathname](https://url.spec.whatwg.org/#dom-url-pathname)
///
/// \returns The URL pathname
string_type pathname() const;

/// @{
/// Sets the [URL pathname](https://url.spec.whatwg.org/#dom-url-pathname)
///
/// \param pathname The new pathname
Expand All @@ -319,11 +347,15 @@ class url {
return set_pathname(std::move(bytes.value()));
}

expected<void, std::error_code> set_pathname(string_type &&pathname);
/// @}

/// Returns the [URL search string](https://url.spec.whatwg.org/#dom-url-search)
///
/// \returns The URL search string
string_type search() const;

/// @{
/// Sets the [URL search string](https://url.spec.whatwg.org/#dom-url-search)
///
/// \param search The new search string
Expand All @@ -342,6 +374,9 @@ class url {
return set_search(std::move(bytes.value()));
}

expected<void, std::error_code> set_search(string_type &&search);
/// @}

/// \returns A reference to the search parameters
url_search_parameters &search_parameters();

Expand All @@ -350,6 +385,7 @@ class url {
/// \returns The URL hash string
string_type hash() const;

/// @{
/// Sets the [URL hash string](https://url.spec.whatwg.org/#dom-url-hash)
///
/// \param hash The new hash string
Expand All @@ -368,6 +404,9 @@ class url {
return set_hash(std::move(bytes.value()));
}

expected<void, std::error_code> set_hash(string_type &&hash);
/// @}

/// \returns A copy to the underlying `url_record` implementation.
url_record record() const;

Expand Down Expand Up @@ -441,16 +480,6 @@ class url {
string_type &&input,
optional<url_record> base = nullopt);
void update_record(url_record &&record);
expected<void, std::error_code> set_href(string_type &&href);
expected<void, std::error_code> set_protocol(string_type &&protocol);
expected<void, std::error_code> set_username(string_type &&username);
expected<void, std::error_code> set_password(string_type &&password);
expected<void, std::error_code> set_host(string_type &&host);
expected<void, std::error_code> set_hostname(string_type &&hostname);
expected<void, std::error_code> set_port(string_type &&port);
expected<void, std::error_code> set_pathname(string_type &&pathname);
expected<void, std::error_code> set_search(string_type &&search);
expected<void, std::error_code> set_hash(string_type &&hash);

template <class Source>
expected<void, std::error_code> set_port_impl(
Expand Down
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ set(Skyr_SRCS
${CMAKE_SOURCE_DIR}/include/skyr/url_error.hpp
${CMAKE_SOURCE_DIR}/include/skyr/url_search_parameters.hpp)

if (Skyr_BUILD_FILESYSTEM_PATH_FUNCTIONS)
LIST(APPEND Skyr_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/filesystem.cpp)
LIST(APPEND Skyr_SRCS ${CMAKE_SOURCE_DIR}/include/skyr/filesystem.hpp)
endif()

add_library(skyr-url ${Skyr_SRCS})
target_link_libraries(skyr-url)
if(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
Expand All @@ -52,6 +57,12 @@ if(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
endif()
endif()

if(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
if (Skyr_BUILD_FILESYSTEM_PATH_FUNCTIONS)
target_link_libraries(skyr-url "stdc++fs")
endif()
endif()

if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU OR
${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-declarations")
Expand Down
Loading