2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
11 #include "AaptAssets.h"
13 #include <androidfw/ResourceTypes.h>
14 #include <utils/String16.h>
15 #include <utils/TextOutput.h>
16 #include <utils/TypeHelpers.h>
18 #include <sys/types.h>
24 #include <libexpat/expat.h>
26 using namespace android;
28 #define PRINT_STRING_METRICS 0
30 void strcpy16_htod(uint16_t* dst, const uint16_t* src);
32 void printStringPool(const ResStringPool* pool);
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.
43 entry() : offset(0) { }
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) { }
52 Vector<size_t> indices;
53 String8 configTypeName;
54 Vector<ResTable_config> configs;
56 String8 makeConfigsString() const;
58 int compare(const entry& o) const;
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; }
68 struct entry_style_span {
70 ResStringPool_span span;
74 entry_style() : offset(0) { }
76 entry_style(const entry_style& o) : offset(o.offset), spans(o.spans) { }
79 Vector<entry_style_span> spans;
83 * If 'utf8' is true, strings will be encoded with UTF-8 instead of
84 * left in Java's native UTF-16.
86 explicit StringPool(bool utf8 = false);
89 * Add a new string to the pool. If mergeDuplicates is true, thenif
90 * the string already exists the existing entry for it will be used;
91 * otherwise, or if the value doesn't already exist, a new entry is
94 * Returns the index in the entry array of the new string entry.
96 ssize_t add(const String16& value, bool mergeDuplicates = false,
97 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
99 ssize_t add(const String16& value, const Vector<entry_style_span>& spans,
100 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
102 status_t addStyleSpan(size_t idx, const String16& name,
103 uint32_t start, uint32_t end);
104 status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans);
105 status_t addStyleSpan(size_t idx, const entry_style_span& span);
107 // Sort the contents of the string block by the configuration associated
108 // with each item. After doing this you can use mapOriginalPosToNewPos()
109 // to find out the new position given the position originally returned by
113 // For use after sortByConfig() to map from the original position of
114 // a string to its new sorted position.
115 size_t mapOriginalPosToNewPos(size_t originalPos) const {
116 return mOriginalPosToNewPos.itemAt(originalPos);
119 sp<AaptFile> createStringBlock();
121 status_t writeStringBlock(const sp<AaptFile>& pool);
124 * Find out an offset in the pool for a particular string. If the string
125 * pool is sorted, this can not be called until after createStringBlock()
126 * or writeStringBlock() has been called
127 * (which determines the offsets). In the case of a string that appears
128 * multiple times in the pool, the first offset will be returned. Returns
129 * -1 if the string does not exist.
131 ssize_t offsetForString(const String16& val) const;
134 * Find all of the offsets in the pool for a particular string. If the
135 * string pool is sorted, this can not be called until after
136 * createStringBlock() or writeStringBlock() has been called
137 * (which determines the offsets). Returns NULL if the string does not exist.
139 const Vector<size_t>* offsetsForString(const String16& val) const;
142 static int config_sort(void* state, const void* lhs, const void* rhs);
146 // The following data structures represent the actual structures
147 // that will be generated for the final string pool.
149 // Raw array of unique strings, in some arbitrary order. This is the
150 // actual strings that appear in the final string pool, in the order
151 // that they will be written.
152 Vector<entry> mEntries;
153 // Array of indices into mEntries, in the order they were
154 // added to the pool. This can be different than mEntries
155 // if the same string was added multiple times (it will appear
156 // once in mEntries, with multiple occurrences in this array).
157 // This is the lookup array that will be written for finding
158 // the string for each offset/position in the string pool.
159 Vector<size_t> mEntryArray;
160 // Optional style span information associated with each index of
162 Vector<entry_style> mEntryStyleArray;
164 // The following data structures are used for book-keeping as the
165 // string pool is constructed.
167 // Unique set of all the strings added to the pool, mapped to
168 // the first index of mEntryArray where the value was added.
169 DefaultKeyedVector<String16, ssize_t> mValues;
170 // This array maps from the original position a string was placed at
171 // in mEntryArray to its new position after being sorted with sortByConfig().
172 Vector<size_t> mOriginalPosToNewPos;
175 // The entry types are trivially movable because all fields they contain, including
176 // the vectors and strings, are trivially movable.
178 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry);
179 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span);
180 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style);