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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | using namespace android; |
| 24 | |
| 25 | #define PRINT_STRING_METRICS 0 |
| 26 | |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 27 | #if __cplusplus >= 201103L |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 28 | void strcpy16_htod(char16_t* dst, const char16_t* src); |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 29 | #endif |
| 30 | 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] | 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) { } |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 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) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | String16 value; |
| 50 | size_t offset; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 51 | bool hasStyles; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | Vector<size_t> indices; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 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; } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | /** |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 83 | * If 'utf8' is true, strings will be encoded with UTF-8 instead of |
| 84 | * left in Java's native UTF-16. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | */ |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 86 | explicit StringPool(bool utf8 = false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 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 |
| 92 | * created. |
| 93 | * |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 94 | * 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] | 95 | */ |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 96 | ssize_t add(const String16& value, bool mergeDuplicates = false, |
| 97 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 99 | ssize_t add(const String16& value, const Vector<entry_style_span>& spans, |
| 100 | const String8* configTypeName = NULL, const ResTable_config* config = NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
| 106 | |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 107 | // Sort the contents of the string block by the configuration associated |
| 108 | // with each item. After doing this you can use mapOriginalPosToNewPos() |
Jeff Brown | 345b7eb | 2012-03-16 15:25:17 -0700 | [diff] [blame] | 109 | // to find out the new position given the position originally returned by |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 110 | // add(). |
| 111 | void sortByConfig(); |
| 112 | |
| 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); |
| 117 | } |
| 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | sp<AaptFile> createStringBlock(); |
| 120 | |
| 121 | status_t writeStringBlock(const sp<AaptFile>& pool); |
| 122 | |
| 123 | /** |
| 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. |
| 130 | */ |
| 131 | ssize_t offsetForString(const String16& val) const; |
| 132 | |
| 133 | /** |
| 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. |
| 138 | */ |
| 139 | const Vector<size_t>* offsetsForString(const String16& val) const; |
| 140 | |
| 141 | private: |
Dan Albert | 0de19ad | 2014-10-01 11:34:17 -0700 | [diff] [blame] | 142 | class ConfigSorter |
| 143 | { |
| 144 | public: |
| 145 | explicit ConfigSorter(const StringPool&); |
| 146 | bool operator()(size_t l, size_t r); |
| 147 | private: |
| 148 | const StringPool& pool; |
| 149 | }; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 150 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 151 | const bool mUTF8; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 152 | |
| 153 | // The following data structures represent the actual structures |
| 154 | // that will be generated for the final string pool. |
| 155 | |
| 156 | // Raw array of unique strings, in some arbitrary order. This is the |
| 157 | // actual strings that appear in the final string pool, in the order |
| 158 | // that they will be written. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | Vector<entry> mEntries; |
| 160 | // Array of indices into mEntries, in the order they were |
| 161 | // added to the pool. This can be different than mEntries |
| 162 | // if the same string was added multiple times (it will appear |
| 163 | // once in mEntries, with multiple occurrences in this array). |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 164 | // This is the lookup array that will be written for finding |
| 165 | // 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] | 166 | Vector<size_t> mEntryArray; |
| 167 | // Optional style span information associated with each index of |
| 168 | // mEntryArray. |
| 169 | Vector<entry_style> mEntryStyleArray; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 170 | |
| 171 | // The following data structures are used for book-keeping as the |
| 172 | // string pool is constructed. |
| 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | // Unique set of all the strings added to the pool, mapped to |
| 175 | // the first index of mEntryArray where the value was added. |
| 176 | DefaultKeyedVector<String16, ssize_t> mValues; |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 177 | // This array maps from the original position a string was placed at |
| 178 | // in mEntryArray to its new position after being sorted with sortByConfig(). |
| 179 | Vector<size_t> mOriginalPosToNewPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Jeff Brown | 8a9cfcc | 2012-03-16 15:24:32 -0700 | [diff] [blame] | 182 | // The entry types are trivially movable because all fields they contain, including |
| 183 | // the vectors and strings, are trivially movable. |
| 184 | namespace android { |
| 185 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry); |
| 186 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span); |
| 187 | ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style); |
| 188 | }; |
| 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | #endif |
| 191 | |