blob: e5e196ed4af451774c380d9c26f404127ce18bcc [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
20#include "Logger.h"
21#include "Source.h"
22#include "StringPiece.h"
23
24#include <string>
25#include <vector>
26
27namespace aapt {
28
29#ifdef _WIN32
30constexpr const char sDirSep = '\\';
31#else
32constexpr const char sDirSep = '/';
33#endif
34
35enum class FileType {
36 kUnknown = 0,
37 kNonexistant,
38 kRegular,
39 kDirectory,
40 kCharDev,
41 kBlockDev,
42 kFifo,
43 kSymlink,
44 kSocket,
45};
46
47FileType getFileType(const StringPiece& path);
48
49/*
50 * Lists files under the directory `root`. Files are listed
51 * with just their leaf (filename) names.
52 */
53std::vector<std::string> listFiles(const StringPiece& root);
54
55/*
56 * Appends a path to `base`, separated by the directory separator.
57 */
58void appendPath(std::string* base, const StringPiece& part);
59
60/*
61 * Appends a series of paths to `base`, separated by the
62 * system directory separator.
63 */
64template <typename... Ts >
65void appendPath(std::string* base, const StringPiece& part, const Ts&... parts);
66
67/*
68 * Makes all the directories in `path`. The last element in the path
69 * is interpreted as a directory.
70 */
71bool mkdirs(const StringPiece& path);
72
73/*
74 * Filter that determines which resource files/directories are
75 * processed by AAPT. Takes a pattern string supplied by the user.
76 * Pattern format is specified in the
77 * FileFilter::setPattern(const std::string&) method.
78 */
79class FileFilter {
80public:
81 /*
82 * Patterns syntax:
83 * - Delimiter is :
84 * - Entry can start with the flag ! to avoid printing a warning
85 * about the file being ignored.
86 * - Entry can have the flag "<dir>" to match only directories
87 * or <file> to match only files. Default is to match both.
88 * - Entry can be a simplified glob "<prefix>*" or "*<suffix>"
89 * where prefix/suffix must have at least 1 character (so that
90 * we don't match a '*' catch-all pattern.)
91 * - The special filenames "." and ".." are always ignored.
92 * - Otherwise the full string is matched.
93 * - match is not case-sensitive.
94 */
95 bool setPattern(const StringPiece& pattern);
96
97 /**
98 * Applies the filter, returning true for pass, false for fail.
99 */
100 bool operator()(const std::string& filename, FileType type) const;
101
102private:
103 std::vector<std::string> mPatternTokens;
104};
105
106inline void appendPath(std::string* base, const StringPiece& part) {
107 assert(base);
108 *base += sDirSep;
109 base->append(part.data(), part.size());
110}
111
112template <typename... Ts >
113void appendPath(std::string* base, const StringPiece& part, const Ts&... parts) {
114 assert(base);
115 *base += sDirSep;
116 base->append(part.data(), part.size());
117 appendPath(base, parts...);
118}
119
120} // namespace aapt
121
122#endif // AAPT_FILES_H