]> git.saurik.com Git - android/aapt.git/blob - StringPool.h
donut snapshot
[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 explicit StringPool(bool sorted = false);
73
74 /**
75 * Add a new string to the pool. If mergeDuplicates is true, thenif
76 * the string already exists the existing entry for it will be used;
77 * otherwise, or if the value doesn't already exist, a new entry is
78 * created.
79 *
80 * Returns the index in the entry array of the new string entry. Note that
81 * if this string pool is sorted, the returned index will not be valid
82 * when the pool is finally written.
83 */
84 ssize_t add(const String16& value, bool mergeDuplicates = false);
85
86 ssize_t add(const String16& value, const Vector<entry_style_span>& spans);
87
88 ssize_t add(const String16& ident, const String16& value,
89 bool mergeDuplicates = false);
90
91 status_t addStyleSpan(size_t idx, const String16& name,
92 uint32_t start, uint32_t end);
93 status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans);
94 status_t addStyleSpan(size_t idx, const entry_style_span& span);
95
96 size_t size() const;
97
98 const entry& entryAt(size_t idx) const;
99
100 size_t countIdentifiers() const;
101
102 sp<AaptFile> createStringBlock();
103
104 status_t writeStringBlock(const sp<AaptFile>& pool);
105
106 /**
107 * Find out an offset in the pool for a particular string. If the string
108 * pool is sorted, this can not be called until after createStringBlock()
109 * or writeStringBlock() has been called
110 * (which determines the offsets). In the case of a string that appears
111 * multiple times in the pool, the first offset will be returned. Returns
112 * -1 if the string does not exist.
113 */
114 ssize_t offsetForString(const String16& val) const;
115
116 /**
117 * Find all of the offsets in the pool for a particular string. If the
118 * string pool is sorted, this can not be called until after
119 * createStringBlock() or writeStringBlock() has been called
120 * (which determines the offsets). Returns NULL if the string does not exist.
121 */
122 const Vector<size_t>* offsetsForString(const String16& val) const;
123
124 private:
125 const bool mSorted;
126 // Raw array of unique strings, in some arbitrary order.
127 Vector<entry> mEntries;
128 // Array of indices into mEntries, in the order they were
129 // added to the pool. This can be different than mEntries
130 // if the same string was added multiple times (it will appear
131 // once in mEntries, with multiple occurrences in this array).
132 Vector<size_t> mEntryArray;
133 // Optional style span information associated with each index of
134 // mEntryArray.
135 Vector<entry_style> mEntryStyleArray;
136 // Mapping from indices in mEntryArray to indices in mValues.
137 Vector<size_t> mEntryArrayToValues;
138 // Unique set of all the strings added to the pool, mapped to
139 // the first index of mEntryArray where the value was added.
140 DefaultKeyedVector<String16, ssize_t> mValues;
141 // Unique set of all (optional) identifiers of strings in the
142 // pool, mapping to indices in mEntries.
143 DefaultKeyedVector<String16, ssize_t> mIdents;
144
145 };
146
147 #endif
148