blob: 13330b548d2c921b18e9c6738e72e364cfc931aa [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_RESOURCE_H
18#define AAPT_RESOURCE_H
19
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <iomanip>
Adam Lesinskica2fc352015-04-03 12:08:26 -070021#include <limits>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070022#include <sstream>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <string>
24#include <tuple>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include <vector>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "utils/JenkinsHash.h"
28
29#include "ConfigDescription.h"
30#include "Source.h"
31#include "util/StringPiece.h"
32
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
35/**
36 * The various types of resource types available. Corresponds
37 * to the 'type' in package:type/entry.
38 */
39enum class ResourceType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 kAnim,
41 kAnimator,
42 kArray,
43 kAttr,
44 kAttrPrivate,
45 kBool,
46 kColor,
47 kDimen,
48 kDrawable,
Adam Lesinskic0c36632016-10-19 18:37:53 -070049 kFont,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 kFraction,
51 kId,
52 kInteger,
53 kInterpolator,
54 kLayout,
55 kMenu,
56 kMipmap,
57 kPlurals,
58 kRaw,
59 kString,
60 kStyle,
61 kStyleable,
62 kTransition,
63 kXml,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064};
65
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066StringPiece ToString(ResourceType type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
68/**
69 * Returns a pointer to a valid ResourceType, or nullptr if
70 * the string was invalid.
71 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072const ResourceType* ParseResourceType(const StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
74/**
75 * A resource's name. This can uniquely identify
76 * a resource in the ResourceTable.
77 */
78struct ResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 std::string package;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 ResourceType type = ResourceType::kRaw;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 std::string entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 ResourceName() = default;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 ResourceName(const StringPiece& p, ResourceType t, const StringPiece& e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 bool is_valid() const;
89 std::string ToString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090};
91
92/**
93 * Same as ResourceName, but uses StringPieces instead.
94 * Use this if you need to avoid copying and know that
95 * the lifetime of this object is shorter than that
96 * of the original string.
97 */
98struct ResourceNameRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 StringPiece package;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 ResourceType type = ResourceType::kRaw;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceNameRef() = default;
104 ResourceNameRef(const ResourceNameRef&) = default;
105 ResourceNameRef(ResourceNameRef&&) = default;
106 ResourceNameRef(const ResourceName& rhs); // NOLINT(implicit)
107 ResourceNameRef(const StringPiece& p, ResourceType t, const StringPiece& e);
108 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
109 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
110 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 ResourceName ToResourceName() const;
113 bool is_valid() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114};
115
116/**
117 * A binary identifier representing a resource. Internally it
118 * is a 32bit integer split as follows:
119 *
120 * 0xPPTTEEEE
121 *
122 * PP: 8 bit package identifier. 0x01 is reserved for system
123 * and 0x7f is reserved for the running app.
124 * TT: 8 bit type identifier. 0x00 is invalid.
125 * EEEE: 16 bit entry identifier.
126 */
127struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 ResourceId();
131 ResourceId(const ResourceId& rhs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 ResourceId(uint32_t res_id); // NOLINT(implicit)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 bool is_valid() const;
136 uint8_t package_id() const;
137 uint8_t type_id() const;
138 uint16_t entry_id() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139};
140
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 ResourceName name;
143 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144};
145
146struct ResourceFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 // Name
148 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 // Configuration
151 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Source
154 Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 std::vector<SourcedResourceName> exported_symbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158};
159
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800160/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 * Useful struct used as a key to represent a unique resource in associative
162 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800163 */
164struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 ResourceName name;
166 ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800167};
168
169bool operator<(const ResourceKey& a, const ResourceKey& b);
170
171/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 * Useful struct used as a key to represent a unique resource in associative
173 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800174 * Holds a reference to the name, so that name better live longer than this key!
175 */
176struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 ResourceNameRef name;
178 ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 ResourceKeyRef() = default;
181 ResourceKeyRef(const ResourceNameRef& n, const ConfigDescription& c)
182 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 /**
185 * Prevent taking a reference to a temporary. This is bad.
186 */
187 ResourceKeyRef(ResourceName&& n, const ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800188};
189
190bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
191
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192//
193// ResourceId implementation.
194//
195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
203 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205inline bool ResourceId::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207}
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211}
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215}
216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219}
220
Adam Lesinski74605cd2016-03-03 15:39:50 -0800221inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223}
224
Adam Lesinski74605cd2016-03-03 15:39:50 -0800225inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700227}
228
Adam Lesinski74605cd2016-03-03 15:39:50 -0800229inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 return lhs.id == rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800231}
232
233inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 return lhs.id != rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800235}
236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237inline ::std::ostream& operator<<(::std::ostream& out,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 const ResourceId& res_id) {
239 std::ios_base::fmtflags old_flags = out.flags();
240 char old_fill = out.fill();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 out << "0x" << std::internal << std::setfill('0') << std::setw(8) << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 << res_id.id;
243 out.flags(old_flags);
244 out.fill(old_fill);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246}
247
248//
249// ResourceType implementation.
250//
251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252inline ::std::ostream& operator<<(::std::ostream& out,
253 const ResourceType& val) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 return out << ToString(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
257//
258// ResourceName implementation.
259//
260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261inline ResourceName::ResourceName(const StringPiece& p, ResourceType t,
262 const StringPiece& e)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 : package(p.ToString()), type(t), entry(e.ToString()) {}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700264
Adam Lesinski8197cc462016-08-19 12:16:49 -0700265inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 int cmp = package.compare(other.package);
267 if (cmp != 0) return cmp;
268 cmp = static_cast<int>(type) - static_cast<int>(other.type);
269 if (cmp != 0) return cmp;
270 cmp = entry.compare(other.entry);
271 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700272}
273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276}
277
Adam Lesinski74605cd2016-03-03 15:39:50 -0800278inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 return std::tie(lhs.package, lhs.type, lhs.entry) <
280 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinski74605cd2016-03-03 15:39:50 -0800283inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return std::tie(lhs.package, lhs.type, lhs.entry) ==
285 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286}
287
Adam Lesinski74605cd2016-03-03 15:39:50 -0800288inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return std::tie(lhs.package, lhs.type, lhs.entry) !=
290 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291}
292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293inline ::std::ostream& operator<<(::std::ostream& out,
294 const ResourceName& name) {
295 if (!name.package.empty()) {
296 out << name.package << ":";
297 }
298 return out << name.type << "/" << name.entry;
Adam Lesinski769de982015-04-10 19:43:55 -0700299}
300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301inline std::string ResourceName::ToString() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 std::stringstream stream;
303 stream << *this;
304 return stream.str();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700305}
Adam Lesinski769de982015-04-10 19:43:55 -0700306
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307//
308// ResourceNameRef implementation.
309//
310
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
312 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700314inline ResourceNameRef::ResourceNameRef(const StringPiece& p, ResourceType t,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 const StringPiece& e)
316 : package(p), type(t), entry(e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317
318inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 package = rhs.package;
320 type = rhs.type;
321 entry = rhs.entry;
322 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323}
324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331}
332
Adam Lesinski74605cd2016-03-03 15:39:50 -0800333inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 return std::tie(lhs.package, lhs.type, lhs.entry) <
335 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336}
337
Adam Lesinski74605cd2016-03-03 15:39:50 -0800338inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 return std::tie(lhs.package, lhs.type, lhs.entry) ==
340 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341}
342
Adam Lesinski74605cd2016-03-03 15:39:50 -0800343inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return std::tie(lhs.package, lhs.type, lhs.entry) !=
345 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346}
347
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348inline ::std::ostream& operator<<(::std::ostream& out,
349 const ResourceNameRef& name) {
350 if (!name.package.empty()) {
351 out << name.package << ":";
352 }
353 return out << name.type << "/" << name.entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354}
355
Adam Lesinski74605cd2016-03-03 15:39:50 -0800356inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800358}
359
360inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800362}
363
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364inline bool operator==(const SourcedResourceName& lhs,
365 const SourcedResourceName& rhs) {
366 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800367}
368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800370
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700371namespace std {
372
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373template <>
374struct hash<aapt::ResourceName> {
375 size_t operator()(const aapt::ResourceName& name) const {
376 android::hash_t h = 0;
377 h = android::JenkinsHashMix(h, hash<string>()(name.package));
378 h = android::JenkinsHashMix(h, static_cast<uint32_t>(name.type));
379 h = android::JenkinsHashMix(h, hash<string>()(name.entry));
380 return static_cast<size_t>(h);
381 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700382};
383
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700385
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386#endif // AAPT_RESOURCE_H