/src/WasmEdge/include/common/filesystem.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/common/filesystem.h - std::filesystem selection ----------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains std::filesystem linkage handling for various |
12 | | /// compilers and the UTF-8 path conversion helpers. |
13 | | /// |
14 | | //===----------------------------------------------------------------------===// |
15 | | #pragma once |
16 | | |
17 | | #if defined(__cpp_lib_filesystem) |
18 | | #define EXPERIMENTAL 0 |
19 | | #elif defined(__cpp_lib_experimental_filesystem) |
20 | | #define EXPERIMENTAL 1 |
21 | | #elif !defined(__has_include) |
22 | | #define EXPERIMENTAL 1 |
23 | | #elif __has_include(<filesystem>) |
24 | | #ifdef _MSC_VER |
25 | | #if __has_include(<yvals_core.h>) |
26 | | #include <yvals_core.h> |
27 | | #if defined(_HAS_CXX17) && _HAS_CXX17 |
28 | | #define EXPERIMENTAL 0 |
29 | | #else |
30 | | #define EXPERIMENTAL 1 |
31 | | #endif |
32 | | #else |
33 | | #define EXPERIMENTAL 1 |
34 | | #endif |
35 | | #else |
36 | | #define EXPERIMENTAL 0 |
37 | | #endif |
38 | | #elif __has_include(<experimental/filesystem>) |
39 | | #define EXPERIMENTAL 1 |
40 | | #else |
41 | | #error Could not find system header "<filesystem>" or "<experimental/filesystem>" |
42 | | #endif |
43 | | |
44 | | #if EXPERIMENTAL |
45 | | #include <experimental/filesystem> |
46 | | namespace std { |
47 | | namespace filesystem = experimental::filesystem; |
48 | | } |
49 | | #else |
50 | | #include <filesystem> |
51 | | #endif |
52 | | |
53 | | #undef EXPERIMENTAL |
54 | | |
55 | | #include <string> |
56 | | #include <string_view> |
57 | | |
58 | | namespace WasmEdge { |
59 | | |
60 | | /// Build a path from UTF-8 encoded text. |
61 | 0 | inline std::filesystem::path u8path(std::string_view Source) { |
62 | 0 | #if defined(__cpp_lib_char8_t) |
63 | 0 | return std::filesystem::path(std::u8string(Source.begin(), Source.end())); |
64 | 0 | #else |
65 | 0 | return std::filesystem::u8path(Source); |
66 | 0 | #endif |
67 | 0 | } |
68 | | |
69 | | /// Return the UTF-8 encoded text of a path. |
70 | 4.09k | inline std::string u8string(const std::filesystem::path &Path) { |
71 | | #if defined(__cpp_lib_char8_t) |
72 | | const auto Str = Path.u8string(); |
73 | | return std::string(Str.begin(), Str.end()); |
74 | | #else |
75 | 4.09k | return Path.u8string(); |
76 | 4.09k | #endif |
77 | 4.09k | } |
78 | | |
79 | | } // namespace WasmEdge |