Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef AAPT_FILES_H |
| 18 | #define AAPT_FILES_H |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 20 | #include "Diagnostics.h" |
| 21 | #include "Maybe.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include "Source.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 24 | #include "util/StringPiece.h" |
| 25 | |
| 26 | #include <utils/FileMap.h> |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 27 | #include <cassert> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 28 | #include <memory> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace aapt { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 33 | namespace file { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | |
| 35 | #ifdef _WIN32 |
| 36 | constexpr const char sDirSep = '\\'; |
| 37 | #else |
| 38 | constexpr const char sDirSep = '/'; |
| 39 | #endif |
| 40 | |
| 41 | enum class FileType { |
| 42 | kUnknown = 0, |
| 43 | kNonexistant, |
| 44 | kRegular, |
| 45 | kDirectory, |
| 46 | kCharDev, |
| 47 | kBlockDev, |
| 48 | kFifo, |
| 49 | kSymlink, |
| 50 | kSocket, |
| 51 | }; |
| 52 | |
| 53 | FileType getFileType(const StringPiece& path); |
| 54 | |
| 55 | /* |
| 56 | * Lists files under the directory `root`. Files are listed |
| 57 | * with just their leaf (filename) names. |
| 58 | */ |
| 59 | std::vector<std::string> listFiles(const StringPiece& root); |
| 60 | |
| 61 | /* |
| 62 | * Appends a path to `base`, separated by the directory separator. |
| 63 | */ |
| 64 | void appendPath(std::string* base, const StringPiece& part); |
| 65 | |
| 66 | /* |
| 67 | * Appends a series of paths to `base`, separated by the |
| 68 | * system directory separator. |
| 69 | */ |
| 70 | template <typename... Ts > |
| 71 | void appendPath(std::string* base, const StringPiece& part, const Ts&... parts); |
| 72 | |
| 73 | /* |
| 74 | * Makes all the directories in `path`. The last element in the path |
| 75 | * is interpreted as a directory. |
| 76 | */ |
| 77 | bool mkdirs(const StringPiece& path); |
| 78 | |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Returns all but the last part of the path. |
| 81 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 82 | StringPiece getStem(const StringPiece& path); |
| 83 | |
| 84 | /** |
| 85 | * Returns the last part of the path with extension. |
| 86 | */ |
| 87 | StringPiece getFilename(const StringPiece& path); |
| 88 | |
| 89 | /** |
| 90 | * Returns the extension of the path. This is the entire string after |
| 91 | * the first '.' of the last part of the path. |
| 92 | */ |
| 93 | StringPiece getExtension(const StringPiece& path); |
| 94 | |
| 95 | /** |
| 96 | * Converts a package name (com.android.app) to a path: com/android/app |
| 97 | */ |
| 98 | std::string packageToPath(const StringPiece& package); |
| 99 | |
| 100 | /** |
| 101 | * Creates a FileMap for the file at path. |
| 102 | */ |
| 103 | Maybe<android::FileMap> mmapPath(const StringPiece& path, std::string* outError); |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Filter that determines which resource files/directories are |
| 107 | * processed by AAPT. Takes a pattern string supplied by the user. |
| 108 | * Pattern format is specified in the |
| 109 | * FileFilter::setPattern(const std::string&) method. |
| 110 | */ |
| 111 | class FileFilter { |
| 112 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 113 | FileFilter(IDiagnostics* diag) : mDiag(diag) { |
| 114 | } |
| 115 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Patterns syntax: |
| 118 | * - Delimiter is : |
| 119 | * - Entry can start with the flag ! to avoid printing a warning |
| 120 | * about the file being ignored. |
| 121 | * - Entry can have the flag "<dir>" to match only directories |
| 122 | * or <file> to match only files. Default is to match both. |
| 123 | * - Entry can be a simplified glob "<prefix>*" or "*<suffix>" |
| 124 | * where prefix/suffix must have at least 1 character (so that |
| 125 | * we don't match a '*' catch-all pattern.) |
| 126 | * - The special filenames "." and ".." are always ignored. |
| 127 | * - Otherwise the full string is matched. |
| 128 | * - match is not case-sensitive. |
| 129 | */ |
| 130 | bool setPattern(const StringPiece& pattern); |
| 131 | |
| 132 | /** |
| 133 | * Applies the filter, returning true for pass, false for fail. |
| 134 | */ |
| 135 | bool operator()(const std::string& filename, FileType type) const; |
| 136 | |
| 137 | private: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 138 | IDiagnostics* mDiag; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 139 | std::vector<std::string> mPatternTokens; |
| 140 | }; |
| 141 | |
| 142 | inline void appendPath(std::string* base, const StringPiece& part) { |
| 143 | assert(base); |
| 144 | *base += sDirSep; |
| 145 | base->append(part.data(), part.size()); |
| 146 | } |
| 147 | |
| 148 | template <typename... Ts > |
| 149 | void appendPath(std::string* base, const StringPiece& part, const Ts&... parts) { |
| 150 | assert(base); |
| 151 | *base += sDirSep; |
| 152 | base->append(part.data(), part.size()); |
| 153 | appendPath(base, parts...); |
| 154 | } |
| 155 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 156 | } // namespace file |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | } // namespace aapt |
| 158 | |
| 159 | #endif // AAPT_FILES_H |