]>
Commit | Line | Data |
---|---|---|
dadd9c1f TAOSP |
1 | // |
2 | // Copyright 2006 The Android Open Source Project | |
3 | // | |
4 | // Build resource files from raw assets. | |
5 | // | |
6 | ||
7 | #ifndef STRING_POOL_H | |
8 | #define STRING_POOL_H | |
9 | ||
10 | #include "Main.h" | |
11 | #include "AaptAssets.h" | |
12 | ||
34ceac87 | 13 | #include <androidfw/ResourceTypes.h> |
dadd9c1f TAOSP |
14 | #include <utils/String16.h> |
15 | #include <utils/TextOutput.h> | |
65e2b85c | 16 | #include <utils/TypeHelpers.h> |
dadd9c1f TAOSP |
17 | |
18 | #include <sys/types.h> | |
19 | #include <sys/stat.h> | |
20 | #include <fcntl.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
23 | ||
24 | #include <expat.h> | |
25 | ||
26 | using namespace android; | |
27 | ||
28 | #define PRINT_STRING_METRICS 0 | |
29 | ||
30 | void strcpy16_htod(uint16_t* dst, const uint16_t* src); | |
31 | ||
32 | void printStringPool(const ResStringPool* pool); | |
33 | ||
34 | /** | |
35 | * The StringPool class is used as an intermediate representation for | |
36 | * generating the string pool resource data structure that can be parsed with | |
37 | * ResStringPool in include/utils/ResourceTypes.h. | |
38 | */ | |
39 | class StringPool | |
40 | { | |
41 | public: | |
42 | struct entry { | |
43 | entry() : offset(0) { } | |
4f41c7f7 DH |
44 | entry(const String16& _value) : value(_value), offset(0), hasStyles(false) { } |
45 | entry(const entry& o) : value(o.value), offset(o.offset), | |
46 | hasStyles(o.hasStyles), indices(o.indices), | |
47 | configTypeName(o.configTypeName), configs(o.configs) { } | |
dadd9c1f TAOSP |
48 | |
49 | String16 value; | |
50 | size_t offset; | |
4f41c7f7 | 51 | bool hasStyles; |
dadd9c1f | 52 | Vector<size_t> indices; |
4f41c7f7 DH |
53 | String8 configTypeName; |
54 | Vector<ResTable_config> configs; | |
55 | ||
56 | String8 makeConfigsString() const; | |
57 | ||
58 | int compare(const entry& o) const; | |
59 | ||
60 | inline bool operator<(const entry& o) const { return compare(o) < 0; } | |
61 | inline bool operator<=(const entry& o) const { return compare(o) <= 0; } | |
62 | inline bool operator==(const entry& o) const { return compare(o) == 0; } | |
63 | inline bool operator!=(const entry& o) const { return compare(o) != 0; } | |
64 | inline bool operator>=(const entry& o) const { return compare(o) >= 0; } | |
65 | inline bool operator>(const entry& o) const { return compare(o) > 0; } | |
dadd9c1f TAOSP |
66 | }; |
67 | ||
68 | struct entry_style_span { | |
69 | String16 name; | |
70 | ResStringPool_span span; | |
71 | }; | |
72 | ||
73 | struct entry_style { | |
74 | entry_style() : offset(0) { } | |
75 | ||
76 | entry_style(const entry_style& o) : offset(o.offset), spans(o.spans) { } | |
77 | ||
78 | size_t offset; | |
79 | Vector<entry_style_span> spans; | |
80 | }; | |
81 | ||
82 | /** | |
83 | * If 'sorted' is true, then the final strings in the resource data | |
84 | * structure will be generated in sorted order. This allow for fast | |
85 | * lookup with ResStringPool::indexOfString() (O(log n)), at the expense | |
86 | * of support for styled string entries (which requires the same string | |
87 | * be included multiple times in the pool). | |
15c62a5b KR |
88 | * |
89 | * If 'utf8' is true, strings will be encoded with UTF-8 instead of | |
90 | * left in Java's native UTF-16. | |
dadd9c1f | 91 | */ |
15c62a5b | 92 | explicit StringPool(bool sorted = false, bool utf8 = false); |
dadd9c1f TAOSP |
93 | |
94 | /** | |
95 | * Add a new string to the pool. If mergeDuplicates is true, thenif | |
96 | * the string already exists the existing entry for it will be used; | |
97 | * otherwise, or if the value doesn't already exist, a new entry is | |
98 | * created. | |
99 | * | |
100 | * Returns the index in the entry array of the new string entry. Note that | |
101 | * if this string pool is sorted, the returned index will not be valid | |
102 | * when the pool is finally written. | |
103 | */ | |
4f41c7f7 DH |
104 | ssize_t add(const String16& value, bool mergeDuplicates = false, |
105 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); | |
dadd9c1f | 106 | |
4f41c7f7 DH |
107 | ssize_t add(const String16& value, const Vector<entry_style_span>& spans, |
108 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); | |
dadd9c1f TAOSP |
109 | |
110 | ssize_t add(const String16& ident, const String16& value, | |
4f41c7f7 DH |
111 | bool mergeDuplicates = false, |
112 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); | |
dadd9c1f TAOSP |
113 | |
114 | status_t addStyleSpan(size_t idx, const String16& name, | |
115 | uint32_t start, uint32_t end); | |
116 | status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans); | |
117 | status_t addStyleSpan(size_t idx, const entry_style_span& span); | |
118 | ||
119 | size_t size() const; | |
120 | ||
121 | const entry& entryAt(size_t idx) const; | |
122 | ||
123 | size_t countIdentifiers() const; | |
124 | ||
4f41c7f7 DH |
125 | // Sort the contents of the string block by the configuration associated |
126 | // with each item. After doing this you can use mapOriginalPosToNewPos() | |
127 | // to find out the new position given the position originall returned by | |
128 | // add(). | |
129 | void sortByConfig(); | |
130 | ||
131 | // For use after sortByConfig() to map from the original position of | |
132 | // a string to its new sorted position. | |
133 | size_t mapOriginalPosToNewPos(size_t originalPos) const { | |
134 | return mOriginalPosToNewPos.itemAt(originalPos); | |
135 | } | |
136 | ||
dadd9c1f TAOSP |
137 | sp<AaptFile> createStringBlock(); |
138 | ||
139 | status_t writeStringBlock(const sp<AaptFile>& pool); | |
140 | ||
141 | /** | |
142 | * Find out an offset in the pool for a particular string. If the string | |
143 | * pool is sorted, this can not be called until after createStringBlock() | |
144 | * or writeStringBlock() has been called | |
145 | * (which determines the offsets). In the case of a string that appears | |
146 | * multiple times in the pool, the first offset will be returned. Returns | |
147 | * -1 if the string does not exist. | |
148 | */ | |
149 | ssize_t offsetForString(const String16& val) const; | |
150 | ||
151 | /** | |
152 | * Find all of the offsets in the pool for a particular string. If the | |
153 | * string pool is sorted, this can not be called until after | |
154 | * createStringBlock() or writeStringBlock() has been called | |
155 | * (which determines the offsets). Returns NULL if the string does not exist. | |
156 | */ | |
157 | const Vector<size_t>* offsetsForString(const String16& val) const; | |
158 | ||
159 | private: | |
4f41c7f7 DH |
160 | static int config_sort(const size_t* lhs, const size_t* rhs, void* state); |
161 | ||
dadd9c1f | 162 | const bool mSorted; |
15c62a5b | 163 | const bool mUTF8; |
4f41c7f7 DH |
164 | |
165 | // The following data structures represent the actual structures | |
166 | // that will be generated for the final string pool. | |
167 | ||
168 | // Raw array of unique strings, in some arbitrary order. This is the | |
169 | // actual strings that appear in the final string pool, in the order | |
170 | // that they will be written. | |
dadd9c1f TAOSP |
171 | Vector<entry> mEntries; |
172 | // Array of indices into mEntries, in the order they were | |
173 | // added to the pool. This can be different than mEntries | |
174 | // if the same string was added multiple times (it will appear | |
175 | // once in mEntries, with multiple occurrences in this array). | |
4f41c7f7 DH |
176 | // This is the lookup array that will be written for finding |
177 | // the string for each offset/position in the string pool. | |
dadd9c1f TAOSP |
178 | Vector<size_t> mEntryArray; |
179 | // Optional style span information associated with each index of | |
180 | // mEntryArray. | |
181 | Vector<entry_style> mEntryStyleArray; | |
4f41c7f7 DH |
182 | |
183 | // The following data structures are used for book-keeping as the | |
184 | // string pool is constructed. | |
185 | ||
dadd9c1f TAOSP |
186 | // Unique set of all the strings added to the pool, mapped to |
187 | // the first index of mEntryArray where the value was added. | |
188 | DefaultKeyedVector<String16, ssize_t> mValues; | |
189 | // Unique set of all (optional) identifiers of strings in the | |
190 | // pool, mapping to indices in mEntries. | |
191 | DefaultKeyedVector<String16, ssize_t> mIdents; | |
4f41c7f7 DH |
192 | // This array maps from the original position a string was placed at |
193 | // in mEntryArray to its new position after being sorted with sortByConfig(). | |
194 | Vector<size_t> mOriginalPosToNewPos; | |
dadd9c1f TAOSP |
195 | }; |
196 | ||
65e2b85c JB |
197 | // The entry types are trivially movable because all fields they contain, including |
198 | // the vectors and strings, are trivially movable. | |
199 | namespace android { | |
200 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry); | |
201 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span); | |
202 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style); | |
203 | }; | |
204 | ||
dadd9c1f TAOSP |
205 | #endif |
206 |