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