std::filesystem::relative, std::filesystem::proximate
From cppreference.com
< cpp | filesystem
| ヘッダー <filesystem> で定義 |
||
| path relative( const std::filesystem::path& p, std::error_code& ec ); |
(1) | (C++17以降) |
| path relative( const std::filesystem::path& p, const std::filesystem::path& base = std::filesystem::current_path() ); |
(2) | (C++17以降) |
| path relative( const std::filesystem::path& p, const std::filesystem::path& base, |
(3) | (C++17以降) |
| path proximate( const std::filesystem::path& p, std::error_code& ec ); |
(4) | (C++17以降) |
| path proximate( const std::filesystem::path& p, const std::filesystem::path& base = std::filesystem::current_path() ); |
(5) | (C++17以降) |
| path proximate( const std::filesystem::path& p, const std::filesystem::path& base, |
(6) | (C++17以降) |
1) relative(p, current_path(), ec) を返します。
2,3) base に対して p を相対パスに変換して返します。シンボリックリンクを解決し、他の処理の前に p と base の両方を正規化します。実質的には、エラーコード形式を除き、std::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base)) または std::filesystem::weakly_canonical(p, ec).lexically_relative(std::filesystem::weakly_canonical(base, ec)) を返しますが、エラーコード形式では、最初のエラー発生時に path() を返します。
4) proximate(p, current_path(), ec) を返します。
5,6) エラーコード形式を除き、実質的には std::filesystem::weakly_canonical(p).lexically_proximate(std::filesystem::weakly_canonical(base)) または std::filesystem::weakly_canonical(p, ec).lexically_proximate(std::filesystem::weakly_canonical(base, ec)) を返します。エラーコード形式では、最初のエラー発生時に path() を返します。
目次 |
[編集] パラメータ
| p | - | 既存のパス |
| base | - | p が相対パス/近接パスになる基準となるパス |
| エラーコード | - | エラー状態を格納するためのエラーコード。 |
[編集] 返り値
1) current_path() に対して p を相対パスにしたもの。
2,3) base に対して p を相対パスにしたもの。
4) current_path() に対して p を近接パスにしたもの。
5,6) base に対して p を近接パスにしたもの。
[編集] 例外
noexcept とマークされていないオーバーロードは、メモリ割り当てが失敗した場合に std::bad_alloc をスローする可能性があります。
2,5) 下位 OS API エラーの場合、p を最初のパス引数、base を 2 番目のパス引数、OS エラーコードをエラーコード引数として構築された std::filesystem::filesystem_error をスローします。
1,3,4,6) OS API の呼び出しが失敗した場合、std::error_code& パラメータに OS API エラーコードを設定します。エラーが発生しなかった場合は ec.clear() を実行します。
[編集] 例
このコードを実行
#include <filesystem> #include <iostream> void show(std::filesystem::path x, std::filesystem::path y) { std::cout << "x:\t\t " << x << "\ny:\t\t " << y << '\n' << "relative(x, y): " << std::filesystem::relative(x, y) << '\n' << "proximate(x, y): " << std::filesystem::proximate(x, y) << "\n\n"; } int main() { show("/a/b/c", "/a/b"); show("/a/c", "/a/b"); show("c", "/a/b"); show("/a/b", "c"); }
実行結果の例
x: "/a/b/c" y: "/a/b" relative(x, y): "c" proximate(x, y): "c" x: "/a/c" y: "/a/b" relative(x, y): "../c" proximate(x, y): "../c" x: "c" y: "/a/b" relative(x, y): "" proximate(x, y): "c" x: "/a/b" y: "c" relative(x, y): "" proximate(x, y): "/a/b"
[編集] 関連項目
| (C++17) |
パスを表現する (クラス) |
| (C++17) |
絶対パスを構成する (関数) |
| (C++17) |
正規パスを構成する (関数) |
| パスを正規形に変換する パスを相対形式に変換する パスを近接形式に変換する ( std::filesystem::path の public メンバ関数) |