]>
git.saurik.com Git - android/aapt.git/blob - StringPool.h
2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
11 #include "AaptAssets.h"
13 #include <utils/ResourceTypes.h>
14 #include <utils/String16.h>
15 #include <utils/TextOutput.h>
17 #include <sys/types.h>
25 using namespace android
;
27 #define PRINT_STRING_METRICS 0
29 void strcpy16_htod(uint16_t* dst
, const uint16_t* src
);
31 void printStringPool(const ResStringPool
* pool
);
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.
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
) { }
51 Vector
<size_t> indices
;
52 String8 configTypeName
;
53 Vector
<ResTable_config
> configs
;
55 String8
makeConfigsString() const;
57 int compare(const entry
& o
) const;
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; }
67 struct entry_style_span
{
69 ResStringPool_span span
;
73 entry_style() : offset(0) { }
75 entry_style(const entry_style
& o
) : offset(o
.offset
), spans(o
.spans
) { }
78 Vector
<entry_style_span
> spans
;
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).
88 * If 'utf8' is true, strings will be encoded with UTF-8 instead of
89 * left in Java's native UTF-16.
91 explicit StringPool(bool sorted
= false, bool utf8
= false);
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
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.
103 ssize_t
add(const String16
& value
, bool mergeDuplicates
= false,
104 const String8
* configTypeName
= NULL
, const ResTable_config
* config
= NULL
);
106 ssize_t
add(const String16
& value
, const Vector
<entry_style_span
>& spans
,
107 const String8
* configTypeName
= NULL
, const ResTable_config
* config
= NULL
);
109 ssize_t
add(const String16
& ident
, const String16
& value
,
110 bool mergeDuplicates
= false,
111 const String8
* configTypeName
= NULL
, const ResTable_config
* config
= NULL
);
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
);
120 const entry
& entryAt(size_t idx
) const;
122 size_t countIdentifiers() const;
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
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
);
136 sp
<AaptFile
> createStringBlock();
138 status_t
writeStringBlock(const sp
<AaptFile
>& pool
);
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.
148 ssize_t
offsetForString(const String16
& val
) const;
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.
156 const Vector
<size_t>* offsetsForString(const String16
& val
) const;
159 static int config_sort(const size_t* lhs
, const size_t* rhs
, void* state
);
164 // The following data structures represent the actual structures
165 // that will be generated for the final string pool.
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
180 Vector
<entry_style
> mEntryStyleArray
;
182 // The following data structures are used for book-keeping as the
183 // string pool is constructed.
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
;