]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | #pragma once | |
19 | ||
20 | #include <string> | |
21 | #include <utility> | |
22 | #include <vector> | |
23 | ||
24 | #include "arrow/type_fwd.h" | |
25 | #include "arrow/util/optional.h" | |
26 | #include "arrow/util/string_view.h" | |
27 | ||
28 | namespace arrow { | |
29 | namespace fs { | |
30 | namespace internal { | |
31 | ||
32 | constexpr char kSep = '/'; | |
33 | ||
34 | // Computations on abstract paths (not local paths with system-dependent behaviour). | |
35 | // Abstract paths are typically used in URIs. | |
36 | ||
37 | // Split an abstract path into its individual components. | |
38 | ARROW_EXPORT | |
39 | std::vector<std::string> SplitAbstractPath(const std::string& s); | |
40 | ||
41 | // Return the extension of the file | |
42 | ARROW_EXPORT | |
43 | std::string GetAbstractPathExtension(const std::string& s); | |
44 | ||
45 | // Return the parent directory and basename of an abstract path. Both values may be | |
46 | // empty. | |
47 | ARROW_EXPORT | |
48 | std::pair<std::string, std::string> GetAbstractPathParent(const std::string& s); | |
49 | ||
50 | // Validate the components of an abstract path. | |
51 | ARROW_EXPORT | |
52 | Status ValidateAbstractPathParts(const std::vector<std::string>& parts); | |
53 | ||
54 | // Append a non-empty stem to an abstract path. | |
55 | ARROW_EXPORT | |
56 | std::string ConcatAbstractPath(const std::string& base, const std::string& stem); | |
57 | ||
58 | // Make path relative to base, if it starts with base. Otherwise error out. | |
59 | ARROW_EXPORT | |
60 | Result<std::string> MakeAbstractPathRelative(const std::string& base, | |
61 | const std::string& path); | |
62 | ||
63 | ARROW_EXPORT | |
64 | std::string EnsureLeadingSlash(util::string_view s); | |
65 | ||
66 | ARROW_EXPORT | |
67 | util::string_view RemoveLeadingSlash(util::string_view s); | |
68 | ||
69 | ARROW_EXPORT | |
70 | std::string EnsureTrailingSlash(util::string_view s); | |
71 | ||
72 | ARROW_EXPORT | |
73 | util::string_view RemoveTrailingSlash(util::string_view s); | |
74 | ||
75 | ARROW_EXPORT | |
76 | bool IsAncestorOf(util::string_view ancestor, util::string_view descendant); | |
77 | ||
78 | ARROW_EXPORT | |
79 | util::optional<util::string_view> RemoveAncestor(util::string_view ancestor, | |
80 | util::string_view descendant); | |
81 | ||
82 | /// Return a vector of ancestors between a base path and a descendant. | |
83 | /// For example, | |
84 | /// | |
85 | /// AncestorsFromBasePath("a/b", "a/b/c/d/e") -> ["a/b/c", "a/b/c/d"] | |
86 | ARROW_EXPORT | |
87 | std::vector<std::string> AncestorsFromBasePath(util::string_view base_path, | |
88 | util::string_view descendant); | |
89 | ||
90 | /// Given a vector of paths of directories which must be created, produce a the minimal | |
91 | /// subset for passing to CreateDir(recursive=true) by removing redundant parent | |
92 | /// directories | |
93 | ARROW_EXPORT | |
94 | std::vector<std::string> MinimalCreateDirSet(std::vector<std::string> dirs); | |
95 | ||
96 | // Join the components of an abstract path. | |
97 | template <class StringIt> | |
98 | std::string JoinAbstractPath(StringIt it, StringIt end) { | |
99 | std::string path; | |
100 | for (; it != end; ++it) { | |
101 | if (it->empty()) continue; | |
102 | ||
103 | if (!path.empty()) { | |
104 | path += kSep; | |
105 | } | |
106 | path += *it; | |
107 | } | |
108 | return path; | |
109 | } | |
110 | ||
111 | template <class StringRange> | |
112 | std::string JoinAbstractPath(const StringRange& range) { | |
113 | return JoinAbstractPath(range.begin(), range.end()); | |
114 | } | |
115 | ||
116 | /// Convert slashes to backslashes, on all platforms. Mostly useful for testing. | |
117 | ARROW_EXPORT | |
118 | std::string ToBackslashes(util::string_view s); | |
119 | ||
120 | /// Ensure a local path is abstract, by converting backslashes to regular slashes | |
121 | /// on Windows. Return the path unchanged on other systems. | |
122 | ARROW_EXPORT | |
123 | std::string ToSlashes(util::string_view s); | |
124 | ||
125 | ARROW_EXPORT | |
126 | bool IsEmptyPath(util::string_view s); | |
127 | ||
128 | } // namespace internal | |
129 | } // namespace fs | |
130 | } // namespace arrow |