]>
git.saurik.com Git - android/aapt.git/blob - StringPool.cpp
2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "StringPool.h"
9 #include <utils/ByteOrder.h>
13 void strcpy16_htod(uint16_t* dst
, const uint16_t* src
)
16 char16_t s
= htods(*src
);
23 void printStringPool(const ResStringPool
* pool
)
25 const size_t NS
= pool
->size();
26 for (size_t s
=0; s
<NS
; s
++) {
28 printf("String #%d: %s\n", s
,
29 String8(pool
->stringAt(s
, &len
)).string());
33 StringPool::StringPool(bool sorted
)
34 : mSorted(sorted
), mValues(-1), mIdents(-1)
38 ssize_t
StringPool::add(const String16
& value
, bool mergeDuplicates
)
40 return add(String16(), value
, mergeDuplicates
);
43 ssize_t
StringPool::add(const String16
& value
, const Vector
<entry_style_span
>& spans
)
45 ssize_t res
= add(String16(), value
, false);
47 addStyleSpans(res
, spans
);
52 ssize_t
StringPool::add(const String16
& ident
, const String16
& value
,
55 if (ident
.size() > 0) {
56 ssize_t idx
= mIdents
.valueFor(ident
);
58 fprintf(stderr
, "ERROR: Duplicate string identifier %s\n",
59 String8(mEntries
[idx
].value
).string());
64 ssize_t vidx
= mValues
.indexOfKey(value
);
65 ssize_t pos
= vidx
>= 0 ? mValues
.valueAt(vidx
) : -1;
66 ssize_t eidx
= pos
>= 0 ? mEntryArray
.itemAt(pos
) : -1;
68 eidx
= mEntries
.add(entry(value
));
70 fprintf(stderr
, "Failure adding string %s\n", String8(value
).string());
75 const bool first
= vidx
< 0;
76 if (first
|| !mergeDuplicates
) {
77 pos
= mEntryArray
.add(eidx
);
79 vidx
= mValues
.add(value
, pos
);
80 const size_t N
= mEntryArrayToValues
.size();
81 for (size_t i
=0; i
<N
; i
++) {
82 size_t& e
= mEntryArrayToValues
.editItemAt(i
);
83 if ((ssize_t
)e
>= vidx
) {
88 mEntryArrayToValues
.add(vidx
);
90 entry
& ent
= mEntries
.editItemAt(eidx
);
95 if (ident
.size() > 0) {
96 mIdents
.add(ident
, vidx
);
99 NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
100 String8(value
).string(), pos
, eidx
, vidx
));
105 status_t
StringPool::addStyleSpan(size_t idx
, const String16
& name
,
106 uint32_t start
, uint32_t end
)
108 entry_style_span span
;
110 span
.span
.firstChar
= start
;
111 span
.span
.lastChar
= end
;
112 return addStyleSpan(idx
, span
);
115 status_t
StringPool::addStyleSpans(size_t idx
, const Vector
<entry_style_span
>& spans
)
117 const size_t N
=spans
.size();
118 for (size_t i
=0; i
<N
; i
++) {
119 status_t err
= addStyleSpan(idx
, spans
[i
]);
120 if (err
!= NO_ERROR
) {
127 status_t
StringPool::addStyleSpan(size_t idx
, const entry_style_span
& span
)
129 LOG_ALWAYS_FATAL_IF(mSorted
, "Can't use styles with sorted string pools.");
131 // Place blank entries in the span array up to this index.
132 while (mEntryStyleArray
.size() <= idx
) {
133 mEntryStyleArray
.add();
136 entry_style
& style
= mEntryStyleArray
.editItemAt(idx
);
137 style
.spans
.add(span
);
141 size_t StringPool::size() const
143 return mSorted
? mValues
.size() : mEntryArray
.size();
146 const StringPool::entry
& StringPool::entryAt(size_t idx
) const
149 return mEntries
[mEntryArray
[idx
]];
151 return mEntries
[mEntryArray
[mValues
.valueAt(idx
)]];
155 size_t StringPool::countIdentifiers() const
157 return mIdents
.size();
160 sp
<AaptFile
> StringPool::createStringBlock()
162 sp
<AaptFile
> pool
= new AaptFile(String8(), AaptGroupEntry(),
164 status_t err
= writeStringBlock(pool
);
165 return err
== NO_ERROR
? pool
: NULL
;
168 status_t
StringPool::writeStringBlock(const sp
<AaptFile
>& pool
)
170 // Allow appending. Sorry this is a little wacky.
171 if (pool
->getSize() > 0) {
172 sp
<AaptFile
> block
= createStringBlock();
174 return UNKNOWN_ERROR
;
176 ssize_t res
= pool
->writeData(block
->getData(), block
->getSize());
177 return (res
>= 0) ? (status_t
)NO_ERROR
: res
;
180 // First we need to add all style span names to the string pool.
181 // We do this now (instead of when the span is added) so that these
182 // will appear at the end of the pool, not disrupting the order
183 // our client placed their own strings in it.
185 const size_t STYLES
= mEntryStyleArray
.size();
188 for (i
=0; i
<STYLES
; i
++) {
189 entry_style
& style
= mEntryStyleArray
.editItemAt(i
);
190 const size_t N
= style
.spans
.size();
191 for (size_t i
=0; i
<N
; i
++) {
192 entry_style_span
& span
= style
.spans
.editItemAt(i
);
193 ssize_t idx
= add(span
.name
, true);
195 fprintf(stderr
, "Error adding span for style tag '%s'\n",
196 String8(span
.name
).string());
199 span
.span
.name
.index
= (uint32_t)idx
;
203 const size_t ENTRIES
= size();
205 // Now build the pool of unique strings.
207 const size_t STRINGS
= mEntries
.size();
208 const size_t preSize
= sizeof(ResStringPool_header
)
209 + (sizeof(uint32_t)*ENTRIES
)
210 + (sizeof(uint32_t)*STYLES
);
211 if (pool
->editData(preSize
) == NULL
) {
212 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
217 for (i
=0; i
<STRINGS
; i
++) {
218 entry
& ent
= mEntries
.editItemAt(i
);
219 const size_t strSize
= (ent
.value
.size());
220 const size_t lenSize
= strSize
> 0x7fff ? sizeof(uint32_t) : sizeof(uint16_t);
221 const size_t totalSize
= lenSize
+ ((strSize
+1)*sizeof(uint16_t));
224 uint16_t* dat
= (uint16_t*)pool
->editData(preSize
+ strPos
+ totalSize
);
226 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
229 dat
+= (preSize
+strPos
)/sizeof(uint16_t);
230 if (lenSize
> sizeof(uint16_t)) {
231 *dat
= htods(0x8000 | ((strSize
>>16)&0x7fff));
234 *dat
++ = htods(strSize
);
235 strcpy16_htod(dat
, ent
.value
);
237 strPos
+= lenSize
+ (strSize
+1)*sizeof(uint16_t);
240 // Pad ending string position up to a uint32_t boundary.
243 size_t padPos
= ((strPos
+3)&~0x3);
244 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ padPos
);
246 fprintf(stderr
, "ERROR: Out of memory padding string pool\n");
249 memset(dat
+preSize
+strPos
, 0, padPos
-strPos
);
253 // Build the pool of style spans.
255 size_t styPos
= strPos
;
256 for (i
=0; i
<STYLES
; i
++) {
257 entry_style
& ent
= mEntryStyleArray
.editItemAt(i
);
258 const size_t N
= ent
.spans
.size();
259 const size_t totalSize
= (N
*sizeof(ResStringPool_span
))
260 + sizeof(ResStringPool_ref
);
262 ent
.offset
= styPos
-strPos
;
263 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ totalSize
);
265 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
268 ResStringPool_span
* span
= (ResStringPool_span
*)(dat
+preSize
+styPos
);
269 for (size_t i
=0; i
<N
; i
++) {
270 span
->name
.index
= htodl(ent
.spans
[i
].span
.name
.index
);
271 span
->firstChar
= htodl(ent
.spans
[i
].span
.firstChar
);
272 span
->lastChar
= htodl(ent
.spans
[i
].span
.lastChar
);
275 span
->name
.index
= htodl(ResStringPool_span::END
);
281 // Add full terminator at the end (when reading we validate that
282 // the end of the pool is fully terminated to simplify error
284 size_t extra
= sizeof(ResStringPool_span
)-sizeof(ResStringPool_ref
);
285 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ extra
);
287 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
290 uint32_t* p
= (uint32_t*)(dat
+preSize
+styPos
);
292 *p
++ = htodl(ResStringPool_span::END
);
293 extra
-= sizeof(uint32_t);
300 ResStringPool_header
* header
=
301 (ResStringPool_header
*)pool
->padData(sizeof(uint32_t));
302 if (header
== NULL
) {
303 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
306 memset(header
, 0, sizeof(*header
));
307 header
->header
.type
= htods(RES_STRING_POOL_TYPE
);
308 header
->header
.headerSize
= htods(sizeof(*header
));
309 header
->header
.size
= htodl(pool
->getSize());
310 header
->stringCount
= htodl(ENTRIES
);
311 header
->styleCount
= htodl(STYLES
);
313 header
->flags
|= htodl(ResStringPool_header::SORTED_FLAG
);
315 header
->stringsStart
= htodl(preSize
);
316 header
->stylesStart
= htodl(STYLES
> 0 ? (preSize
+strPos
) : 0);
318 // Write string index array.
320 uint32_t* index
= (uint32_t*)(header
+1);
322 for (i
=0; i
<ENTRIES
; i
++) {
323 entry
& ent
= const_cast<entry
&>(entryAt(i
));
326 *index
++ = htodl(ent
.offset
);
329 for (i
=0; i
<ENTRIES
; i
++) {
330 entry
& ent
= mEntries
.editItemAt(mEntryArray
[i
]);
331 *index
++ = htodl(ent
.offset
);
332 NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i
,
333 String8(ent
.value
).string(),
334 mEntryArray
[i
], ent
.offset
));
338 // Write style index array.
341 for (i
=0; i
<STYLES
; i
++) {
342 LOG_ALWAYS_FATAL("Shouldn't be here!");
345 for (i
=0; i
<STYLES
; i
++) {
346 *index
++ = htodl(mEntryStyleArray
[i
].offset
);
353 ssize_t
StringPool::offsetForString(const String16
& val
) const
355 const Vector
<size_t>* indices
= offsetsForString(val
);
356 ssize_t res
= indices
!= NULL
&& indices
->size() > 0 ? indices
->itemAt(0) : -1;
357 NOISY(printf("Offset for string %s: %d (%s)\n", String8(val
).string(), res
,
358 res
>= 0 ? String8(mEntries
[mEntryArray
[res
]].value
).string() : String8()));
362 const Vector
<size_t>* StringPool::offsetsForString(const String16
& val
) const
364 ssize_t pos
= mValues
.valueFor(val
);
368 return &mEntries
[mEntryArray
[pos
]].indices
;