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