blob: 86044ed1ad2b2bb0a278294ca3a7d13d68623b84 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001//
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 Agopianb13b9bd2012-02-17 18:27:36 -080013#include <androidfw/ResourceTypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014#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) { }
Dianne Hackborn6c997a92012-01-31 11:27:43 -080043 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) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48 String16 value;
49 size_t offset;
Dianne Hackborn6c997a92012-01-31 11:27:43 -080050 bool hasStyles;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 Vector<size_t> indices;
Dianne Hackborn6c997a92012-01-31 11:27:43 -080052 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; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 };
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).
Kenny Root19138462009-12-04 09:38:48 -080087 *
88 * If 'utf8' is true, strings will be encoded with UTF-8 instead of
89 * left in Java's native UTF-16.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Kenny Root19138462009-12-04 09:38:48 -080091 explicit StringPool(bool sorted = false, bool utf8 = false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
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 */
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800103 ssize_t add(const String16& value, bool mergeDuplicates = false,
104 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800106 ssize_t add(const String16& value, const Vector<entry_style_span>& spans,
107 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 ssize_t add(const String16& ident, const String16& value,
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800110 bool mergeDuplicates = false,
111 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
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
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800124 // 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 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:
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800159 static int config_sort(const size_t* lhs, const size_t* rhs, void* state);
160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 const bool mSorted;
Kenny Root19138462009-12-04 09:38:48 -0800162 const bool mUTF8;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800163
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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 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).
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800175 // This is the lookup array that will be written for finding
176 // the string for each offset/position in the string pool.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 Vector<size_t> mEntryArray;
178 // Optional style span information associated with each index of
179 // mEntryArray.
180 Vector<entry_style> mEntryStyleArray;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800181
182 // The following data structures are used for book-keeping as the
183 // string pool is constructed.
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 // 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;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800191 // 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194};
195
196#endif
197