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