Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 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 | |
| 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 | // |
| 66 | inline details::CStrWrapper c_str(std::string_view sv) { |
| 67 | return {sv}; |
| 68 | } |
| 69 | |
| 70 | bool isAbsolute(std::string_view path); |
| 71 | std::string normalize(std::string_view path); |
| 72 | std::string_view dirname(std::string_view path); |
| 73 | std::string_view basename(std::string_view path); |
| 74 | std::optional<bool> isEmptyDir(std::string_view dir); |
| 75 | bool startsWith(std::string_view path, std::string_view prefix); |
| 76 | |
| 77 | template <class... Paths> |
| 78 | std::string join(std::string_view first, std::string_view second, Paths&&... paths) { |
| 79 | std::string result; |
| 80 | { |
| 81 | using std::size; |
| 82 | result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths))); |
| 83 | } |
| 84 | result.assign(first); |
| 85 | (details::append_next_path(result, second), ..., details::append_next_path(result, paths)); |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | } // namespace android::incremental::path |