1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <optional>
20 #include <string>
21 #include <string_view>
22
23 namespace android::incremental::path {
24
25 namespace details {
26
27 class CStrWrapper {
28 public:
29 CStrWrapper(std::string_view sv);
30
31 CStrWrapper(const CStrWrapper&) = delete;
32 void operator=(const CStrWrapper&) = delete;
33 CStrWrapper(CStrWrapper&&) = delete;
34 void operator=(CStrWrapper&&) = delete;
35
get()36 const char* get() const { return mCstr; }
37 operator const char*() const { return get(); }
38
39 private:
40 const char* mCstr;
41 std::optional<std::string> mCopy;
42 };
43
44 void append_next_path(std::string& res, std::string_view c);
45
46 } // namespace details
47
48 //
49 // An std::map<> comparator that makes all nested paths to be ordered before the parents.
50 //
51
52 struct PathCharsLess {
53 bool operator()(char l, char r) const;
54 };
55
56 struct PathLess {
57 using is_transparent = void;
58 bool operator()(std::string_view l, std::string_view r) const;
59 };
60
61 //
62 // Returns a zero-terminated version of a passed string view
63 // Only makes a copy if it wasn't zero-terminated already
64 // Useful for passing string view parameters to system functions.
65 //
c_str(std::string_view sv)66 inline details::CStrWrapper c_str(std::string_view sv) {
67 return {sv};
68 }
69
70 std::string_view relativize(std::string_view parent, std::string_view nested);
relativize(const char * parent,const char * nested)71 inline std::string_view relativize(const char* parent, const char* nested) {
72 return relativize(std::string_view(parent), std::string_view(nested));
73 }
relativize(std::string_view parent,const char * nested)74 inline std::string_view relativize(std::string_view parent, const char* nested) {
75 return relativize(parent, std::string_view(nested));
76 }
relativize(const char * parent,std::string_view nested)77 inline std::string_view relativize(const char* parent, std::string_view nested) {
78 return relativize(std::string_view(parent), nested);
79 }
80
81 std::string_view relativize(std::string&& parent, std::string_view nested) = delete;
82 std::string_view relativize(std::string_view parent, std::string&& nested) = delete;
83
84 bool isAbsolute(std::string_view path);
85 std::string normalize(std::string_view path);
86 std::string_view dirname(std::string_view path);
87 std::string_view basename(std::string_view path);
88 std::optional<bool> isEmptyDir(std::string_view dir);
89 bool startsWith(std::string_view path, std::string_view prefix);
90
91 template <class... Paths>
join(std::string && first,std::string_view second,Paths &&...paths)92 std::string join(std::string&& first, std::string_view second, Paths&&... paths) {
93 std::string& result = first;
94 {
95 using std::size;
96 result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
97 }
98 (details::append_next_path(result, second), ...,
99 details::append_next_path(result, std::forward<Paths>(paths)));
100 return std::move(result);
101 }
102
103 template <class... Paths>
join(std::string_view first,std::string_view second,Paths &&...paths)104 std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
105 return path::join(std::string(), first, second, std::forward<Paths>(paths)...);
106 }
107
108 template <class... Paths>
join(const char * first,std::string_view second,Paths &&...paths)109 std::string join(const char* first, std::string_view second, Paths&&... paths) {
110 return path::join(std::string_view(first), second, std::forward<Paths>(paths)...);
111 }
112
113 } // namespace android::incremental::path
114