The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 13 | #include <androidfw/ResourceTypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14 | #include <utils/String16.h> |
Jeff Brown | 8a9cfcc | 2012-03-16 15:24:32 -0700 | [diff] [blame] | 15 | #include <utils/TypeHelpers.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | |
Elliott Hughes | ee15e15 | 2012-09-09 14:45:32 -0700 | [diff] [blame] | 23 | #include <libexpat/expat.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace android; |
| 26 | |
| 27 | #define PRINT_STRING_METRICS 0 |
| 28 | |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 29 | #if __cplusplus >= 201103L |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 30 | void strcpy16_htod(char16_t* dst, const char16_t* src); |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 31 | #endif |
| 32 | void strcpy16_htod(uint16_t* dst, const char16_t* src); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | void printStringPool(const ResStringPool* pool); |
| 35 | |
| 36 | /** |
| 37 | * The StringPool class is used as an intermediate representation for |
| 38 | * generating the string pool resource data structure that can be parsed with |
| 39 | * ResStringPool in include/utils/ResourceTypes.h. |
| 40 | */ |
| 41 | class StringPool |
| 42 | { |
| 43 | public: |
| 44 | struct entry { |
| 45 | entry() : offset(0) { } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 46 | entry(const String16& _value) : value(_value), offset(0), hasStyles(false) { } |
| 47 | entry(const entry& o) : value(o.value), offset(o.offset), |
| 48 | hasStyles(o.hasStyles), indices(o.indices), |
| 49 | configTypeName(o.configTypeName), configs(o.configs) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | String16 value; |
| 52 | size_t offset; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 53 | bool hasStyles; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | Vector<size_t> indices; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 55 | String8 configTypeName; |
| 56 | Vector<ResTable_config> configs; |
| 57 | |
| 58 | String8 makeConfigsString() const; |
| 59 | |
| 60 | int compare(const entry& o) const; |
| 61 | |
| 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; } |
| 66 | inline bool operator>=(const entry& o) const { return compare(o) >= 0; } |
| 67 | inline bool operator>(const entry& o) const { return compare(o) > 0; } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct entry_style_span { |
| 71 | String16 name; |
| 72 | ResStringPool_span span; |
| 73 | }; |
| 74 | |
| 75 | struct entry_style { |
| 76 | entry_style() : offset(0) { } |
| 77 | |
| 78 | entry_style(const entry_style& o) : offset(o.offset), spans(o.spans) { } |
| 79 | |
| 80 | size_t offset; |
| 81 | Vector<entry_style_span> spans; |
| 82 | }; |
| 83 | |
| 84 | /** |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 85 | * If 'utf8' is true, strings will be encoded with UTF-8 instead of |
| 86 | * left in Java's native UTF-16. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | */ |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 88 | explicit StringPool(bool utf8 = false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Add a new string to the pool. If mergeDuplicates is true, thenif |
| 92 | * the string already exists the existing entry for it will be used; |
| 93 | * otherwise, or if the value doesn't already exist, a new entry is |
| 94 | * created. |
| 95 | * |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 96 | * Returns the index in the entry array of the new string entry. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | */ |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 98 | ssize_t add(const String16& value, bool mergeDuplicates = false, |
| 99 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 101 | ssize_t add(const String16& value, const Vector<entry_style_span>& spans, |
| 102 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | status_t addStyleSpan(size_t idx, const String16& name, |
| 105 | uint32_t start, uint32_t end); |
| 106 | status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans); |
| 107 | status_t addStyleSpan(size_t idx, const entry_style_span& span); |
| 108 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 109 | // Sort the contents of the string block by the configuration associated |
| 110 | // with each item. After doing this you can use mapOriginalPosToNewPos() |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 111 | // to find out the new position given the position originally returned by |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 112 | // add(). |
| 113 | void sortByConfig(); |
| 114 | |
| 115 | // For use after sortByConfig() to map from the original position of |
| 116 | // a string to its new sorted position. |
| 117 | size_t mapOriginalPosToNewPos(size_t originalPos) const { |
| 118 | return mOriginalPosToNewPos.itemAt(originalPos); |
| 119 | } |
| 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | sp<AaptFile> createStringBlock(); |
| 122 | |
| 123 | status_t writeStringBlock(const sp<AaptFile>& pool); |
| 124 | |
| 125 | /** |
| 126 | * Find out an offset in the pool for a particular string. If the string |
| 127 | * pool is sorted, this can not be called until after createStringBlock() |
| 128 | * or writeStringBlock() has been called |
| 129 | * (which determines the offsets). In the case of a string that appears |
| 130 | * multiple times in the pool, the first offset will be returned. Returns |
| 131 | * -1 if the string does not exist. |
| 132 | */ |
| 133 | ssize_t offsetForString(const String16& val) const; |
| 134 | |
| 135 | /** |
| 136 | * Find all of the offsets in the pool for a particular string. If the |
| 137 | * string pool is sorted, this can not be called until after |
| 138 | * createStringBlock() or writeStringBlock() has been called |
| 139 | * (which determines the offsets). Returns NULL if the string does not exist. |
| 140 | */ |
| 141 | const Vector<size_t>* offsetsForString(const String16& val) const; |
| 142 | |
| 143 | private: |
Dan Albert | 0de19ad | 2014-10-01 11:34:17 -0700 | [diff] [blame] | 144 | class ConfigSorter |
| 145 | { |
| 146 | public: |
| 147 | explicit ConfigSorter(const StringPool&); |
| 148 | bool operator()(size_t l, size_t r); |
| 149 | private: |
| 150 | const StringPool& pool; |
| 151 | }; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 152 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 153 | const bool mUTF8; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 154 | |
| 155 | // The following data structures represent the actual structures |
| 156 | // that will be generated for the final string pool. |
| 157 | |
| 158 | // Raw array of unique strings, in some arbitrary order. This is the |
| 159 | // actual strings that appear in the final string pool, in the order |
| 160 | // that they will be written. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | Vector<entry> mEntries; |
| 162 | // Array of indices into mEntries, in the order they were |
| 163 | // added to the pool. This can be different than mEntries |
| 164 | // if the same string was added multiple times (it will appear |
| 165 | // once in mEntries, with multiple occurrences in this array). |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 166 | // This is the lookup array that will be written for finding |
| 167 | // the string for each offset/position in the string pool. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | Vector<size_t> mEntryArray; |
| 169 | // Optional style span information associated with each index of |
| 170 | // mEntryArray. |
| 171 | Vector<entry_style> mEntryStyleArray; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 172 | |
| 173 | // The following data structures are used for book-keeping as the |
| 174 | // string pool is constructed. |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | // Unique set of all the strings added to the pool, mapped to |
| 177 | // the first index of mEntryArray where the value was added. |
| 178 | DefaultKeyedVector<String16, ssize_t> mValues; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 179 | // This array maps from the original position a string was placed at |
| 180 | // in mEntryArray to its new position after being sorted with sortByConfig(). |
| 181 | Vector<size_t> mOriginalPosToNewPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
Jeff Brown | 8a9cfcc | 2012-03-16 15:24:32 -0700 | [diff] [blame] | 184 | // The entry types are trivially movable because all fields they contain, including |
| 185 | // the vectors and strings, are trivially movable. |
| 186 | namespace android { |
| 187 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry); |
| 188 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span); |
| 189 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style); |
| 190 | }; |
| 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | #endif |
| 193 | |