]> git.saurik.com Git - android/aapt.git/blob - StringPool.h
Fixes a few minor problems with AAPT
[android/aapt.git] / StringPool.h
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
25 using namespace android;
26
27 #define PRINT_STRING_METRICS 0
28
29 void strcpy16_htod(uint16_t* dst, const uint16_t* src);
30
31 void 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 */
38 class StringPool
39 {
40 public:
41 struct entry {
42 entry() : offset(0) { }
43 entry(const String16& _value) : value(_value), offset(0) { }
44 entry(const entry& o) : value(o.value), offset(o.offset), indices(o.indices) { }
45
46 String16 value;
47 size_t offset;
48 Vector<size_t> indices;
49 };
50
51 struct entry_style_span {
52 String16 name;
53 ResStringPool_span span;
54 };
55
56 struct entry_style {
57 entry_style() : offset(0) { }
58
59 entry_style(const entry_style& o) : offset(o.offset), spans(o.spans) { }
60
61 size_t offset;
62 Vector<entry_style_span> spans;
63 };
64
65 /**
66 * If 'sorted' is true, then the final strings in the resource data
67 * structure will be generated in sorted order. This allow for fast
68 * lookup with ResStringPool::indexOfString() (O(log n)), at the expense
69 * of support for styled string entries (which requires the same string
70 * be included multiple times in the pool).
71 *
72 * If 'utf8' is true, strings will be encoded with UTF-8 instead of
73 * left in Java's native UTF-16.
74 */
75 explicit StringPool(bool sorted = false, bool utf8 = false);
76
77 /**
78 * Add a new string to the pool. If mergeDuplicates is true, thenif
79 * the string already exists the existing entry for it will be used;
80 * otherwise, or if the value doesn't already exist, a new entry is
81 * created.
82 *
83 * Returns the index in the entry array of the new string entry. Note that
84 * if this string pool is sorted, the returned index will not be valid
85 * when the pool is finally written.
86 */
87 ssize_t add(const String16& value, bool mergeDuplicates = false);
88
89 ssize_t add(const String16& value, const Vector<entry_style_span>& spans);
90
91 ssize_t add(const String16& ident, const String16& value,
92 bool mergeDuplicates = false);
93
94 status_t addStyleSpan(size_t idx, const String16& name,
95 uint32_t start, uint32_t end);
96 status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans);
97 status_t addStyleSpan(size_t idx, const entry_style_span& span);
98
99 size_t size() const;
100
101 const entry& entryAt(size_t idx) const;
102
103 size_t countIdentifiers() const;
104
105 sp<AaptFile> createStringBlock();
106
107 status_t writeStringBlock(const sp<AaptFile>& pool);
108
109 /**
110 * Find out an offset in the pool for a particular string. If the string
111 * pool is sorted, this can not be called until after createStringBlock()
112 * or writeStringBlock() has been called
113 * (which determines the offsets). In the case of a string that appears
114 * multiple times in the pool, the first offset will be returned. Returns
115 * -1 if the string does not exist.
116 */
117 ssize_t offsetForString(const String16& val) const;
118
119 /**
120 * Find all of the offsets in the pool for a particular string. If the
121 * string pool is sorted, this can not be called until after
122 * createStringBlock() or writeStringBlock() has been called
123 * (which determines the offsets). Returns NULL if the string does not exist.
124 */
125 const Vector<size_t>* offsetsForString(const String16& val) const;
126
127 private:
128 const bool mSorted;
129 const bool mUTF8;
130 // Raw array of unique strings, in some arbitrary order.
131 Vector<entry> mEntries;
132 // Array of indices into mEntries, in the order they were
133 // added to the pool. This can be different than mEntries
134 // if the same string was added multiple times (it will appear
135 // once in mEntries, with multiple occurrences in this array).
136 Vector<size_t> mEntryArray;
137 // Optional style span information associated with each index of
138 // mEntryArray.
139 Vector<entry_style> mEntryStyleArray;
140 // Mapping from indices in mEntryArray to indices in mValues.
141 Vector<size_t> mEntryArrayToValues;
142 // Unique set of all the strings added to the pool, mapped to
143 // the first index of mEntryArray where the value was added.
144 DefaultKeyedVector<String16, ssize_t> mValues;
145 // Unique set of all (optional) identifiers of strings in the
146 // pool, mapping to indices in mEntries.
147 DefaultKeyedVector<String16, ssize_t> mIdents;
148
149 };
150
151 #endif
152