std::experimental::filesystem::u8path
From cppreference.com
< cpp | experimental | fs | path
| ヘッダー <experimental/filesystem> で定義されています |
||
| template< class Source > path u8path( const Source& source ); |
(1) | (filesystem TS) |
| template< class InputIt > path u8path( InputIt first, InputIt last ); |
(2) | (filesystem TS) |
UTF-8エンコードされたcharのシーケンスからパスpを構築します。このシーケンスは、std::string、null終端マルチバイト文字列、または[first, last)イテレータペアのいずれかとして提供されます。
path::value_typeがcharであり、ネイティブエンコーディングがUTF-8である場合、パスはpath(source)またはpath(first, last)のように直接構築されます。注意:これは、LinuxのようなUnicodeを使用するPOSIXシステムの典型的な状況です。- それ以外の場合、
path::value_typeがwchar_tであり、ネイティブエンコーディングがUTF-16(これはWindowsの状況です)、またはpath::value_typeがchar16_t(ネイティブエンコーディングはUTF-16が保証されています)またはchar32_t(ネイティブエンコーディングはUTF-32が保証されています)である場合、まずUTF-8文字シーケンスをpath::string_type型のテンポラリストリングtmpに変換し、次に新しいパスはpath(tmp)のように構築されます。 - それ以外の場合(UTF-8以外のナロー文字エンコーディングおよびUTF-16以外のwchar_tの場合)、まずUTF-8文字シーケンスをstd::u32string型のテンポラリUTF-32エンコード文字列
tmpに変換し、次に新しいパスはpath(tmp)のように構築されます(これは、Unicode以外のマルチバイトまたはシングルバイトエンコードファイルシステムを持つPOSIXシステムで取られるパスです)。
目次 |
[編集] パラメータ
| source | - | UTF-8エンコードされたstd::string、null終端マルチバイト文字列へのポインタ、またはnull終端マルチバイト文字列を指すchar値型の入力イテレータ |
| first, last | - | UTF-8エンコードされた文字シーケンスを指定する、LegacyInputIteratorのペア |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-InputItの値型はcharである必要があります。 | ||
[編集] 戻り値
入力文字列から、ファイルシステムのネイティブ文字エンコーディングへのUTF-8変換後、構築されたパス。
[編集] 例外
基盤となるOS APIエラーで filesystem_error がスローされるか、メモリ割り当てに失敗した場合は std::bad_alloc がスローされる可能性があります。
[編集] 注記
ネイティブパス形式が汎用パス形式と異なるシステム(WindowsまたはPOSIXシステムはいずれもそのようなOSの例ではありません)では、この関数の引数が汎用形式を使用している場合、ネイティブ形式に変換されます。
[編集] 例
このコードを実行
#include <clocale> #include <cstdio> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::filesystem; int main() { std::setlocale(LC_ALL, "en_US.utf8"); std::locale::global(std::locale("en_US.utf8")); fs::path p = fs::u8path(u8"要らない.txt"); // native string representation can be used with OS APIs std::ofstream(p) << "File contents"; // this uses operator string() if (std::FILE* f = std::fopen(p.c_str(), "r")) { int ch; while ((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // multibyte and wide representation can be used for output std::cout.imbue(std::locale()); std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; std::wcerr.imbue(std::locale()); std::wcerr << "File name in wide encoding: " << p.wstring() << '\n'; fs::remove(p); }
実行結果の例
File contents File name in narrow multibyte encoding: 要らない.txt File name in wide encoding: 要らない.txt
[編集] 関連項目
| パスを表現する (クラス) |