blob: 5edc1448bc9bf12eb6e94fb23057e7d4b75252d3 [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#include "StringPool.h"
8
9#include <utils/ByteOrder.h>
Dianne Hackborn6c997a92012-01-31 11:27:43 -080010#include <utils/SortedVector.h>
Dan Albert0de19ad2014-10-01 11:34:17 -070011
12#include <algorithm>
13
14#include "ResourceTable.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015
Raphaelf51125d2011-10-27 17:01:31 -070016#if HAVE_PRINTF_ZD
17# define ZD "%zd"
18# define ZD_TYPE ssize_t
19#else
20# define ZD "%ld"
21# define ZD_TYPE long
22#endif
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#define NOISY(x) //x
25
Dan Albertf348c152014-09-08 18:28:00 -070026void strcpy16_htod(char16_t* dst, const char16_t* src)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027{
28 while (*src) {
29 char16_t s = htods(*src);
30 *dst++ = s;
31 src++;
32 }
33 *dst = 0;
34}
35
36void printStringPool(const ResStringPool* pool)
37{
Dianne Hackborn6c997a92012-01-31 11:27:43 -080038 SortedVector<const void*> uniqueStrings;
39 const size_t N = pool->size();
40 for (size_t i=0; i<N; i++) {
41 size_t len;
42 if (pool->isUTF8()) {
43 uniqueStrings.add(pool->string8At(i, &len));
44 } else {
45 uniqueStrings.add(pool->stringAt(i, &len));
46 }
47 }
48
49 printf("String pool of " ZD " unique %s %s strings, " ZD " entries and "
50 ZD " styles using " ZD " bytes:\n",
51 (ZD_TYPE)uniqueStrings.size(), pool->isUTF8() ? "UTF-8" : "UTF-16",
52 pool->isSorted() ? "sorted" : "non-sorted",
53 (ZD_TYPE)N, (ZD_TYPE)pool->styleCount(), (ZD_TYPE)pool->bytes());
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 const size_t NS = pool->size();
56 for (size_t s=0; s<NS; s++) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -080057 String8 str = pool->string8ObjectAt(s);
58 printf("String #" ZD ": %s\n", (ZD_TYPE) s, str.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
60}
61
Dianne Hackborn6c997a92012-01-31 11:27:43 -080062String8 StringPool::entry::makeConfigsString() const {
63 String8 configStr(configTypeName);
64 if (configStr.size() > 0) configStr.append(" ");
65 if (configs.size() > 0) {
66 for (size_t j=0; j<configs.size(); j++) {
67 if (j > 0) configStr.append(", ");
68 configStr.append(configs[j].toString());
69 }
70 } else {
71 configStr = "(none)";
72 }
73 return configStr;
74}
75
76int StringPool::entry::compare(const entry& o) const {
Jeff Brown61361f32012-03-16 15:25:17 -070077 // Strings with styles go first, to reduce the size of the styles array.
78 // We don't care about the relative order of these strings.
Dianne Hackborn6c997a92012-01-31 11:27:43 -080079 if (hasStyles) {
80 return o.hasStyles ? 0 : -1;
81 }
82 if (o.hasStyles) {
83 return 1;
84 }
Jeff Brown61361f32012-03-16 15:25:17 -070085
86 // Sort unstyled strings by type, then by logical configuration.
Dianne Hackborn6c997a92012-01-31 11:27:43 -080087 int comp = configTypeName.compare(o.configTypeName);
88 if (comp != 0) {
89 return comp;
90 }
91 const size_t LHN = configs.size();
92 const size_t RHN = o.configs.size();
93 size_t i=0;
94 while (i < LHN && i < RHN) {
95 comp = configs[i].compareLogical(o.configs[i]);
96 if (comp != 0) {
97 return comp;
98 }
99 i++;
100 }
101 if (LHN < RHN) return -1;
102 else if (LHN > RHN) return 1;
103 return 0;
104}
105
Jeff Brown345b7eb2012-03-16 15:25:17 -0700106StringPool::StringPool(bool utf8) :
107 mUTF8(utf8), mValues(-1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108{
109}
110
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800111ssize_t StringPool::add(const String16& value, const Vector<entry_style_span>& spans,
112 const String8* configTypeName, const ResTable_config* config)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113{
Jeff Brown345b7eb2012-03-16 15:25:17 -0700114 ssize_t res = add(value, false, configTypeName, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 if (res >= 0) {
116 addStyleSpans(res, spans);
117 }
118 return res;
119}
120
Jeff Brown345b7eb2012-03-16 15:25:17 -0700121ssize_t StringPool::add(const String16& value,
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800122 bool mergeDuplicates, const String8* configTypeName, const ResTable_config* config)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 ssize_t vidx = mValues.indexOfKey(value);
125 ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1;
126 ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1;
127 if (eidx < 0) {
128 eidx = mEntries.add(entry(value));
129 if (eidx < 0) {
130 fprintf(stderr, "Failure adding string %s\n", String8(value).string());
131 return eidx;
132 }
133 }
134
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800135 if (configTypeName != NULL) {
136 entry& ent = mEntries.editItemAt(eidx);
137 NOISY(printf("*** adding config type name %s, was %s\n",
138 configTypeName->string(), ent.configTypeName.string()));
139 if (ent.configTypeName.size() <= 0) {
140 ent.configTypeName = *configTypeName;
141 } else if (ent.configTypeName != *configTypeName) {
142 ent.configTypeName = " ";
143 }
144 }
145
146 if (config != NULL) {
147 // Add this to the set of configs associated with the string.
148 entry& ent = mEntries.editItemAt(eidx);
149 size_t addPos;
150 for (addPos=0; addPos<ent.configs.size(); addPos++) {
151 int cmp = ent.configs.itemAt(addPos).compareLogical(*config);
152 if (cmp >= 0) {
153 if (cmp > 0) {
154 NOISY(printf("*** inserting config: %s\n", config->toString().string()));
155 ent.configs.insertAt(*config, addPos);
156 }
157 break;
158 }
159 }
160 if (addPos >= ent.configs.size()) {
161 NOISY(printf("*** adding config: %s\n", config->toString().string()));
162 ent.configs.add(*config);
163 }
164 }
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 const bool first = vidx < 0;
Ben Gruverdb6e67d2012-03-07 21:19:16 -0800167 const bool styled = (pos >= 0 && (size_t)pos < mEntryStyleArray.size()) ?
168 mEntryStyleArray[pos].spans.size() : 0;
169 if (first || styled || !mergeDuplicates) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 pos = mEntryArray.add(eidx);
171 if (first) {
172 vidx = mValues.add(value, pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
Jeff Brown345b7eb2012-03-16 15:25:17 -0700174 entry& ent = mEntries.editItemAt(eidx);
175 ent.indices.add(pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
177
178 NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
179 String8(value).string(), pos, eidx, vidx));
180
181 return pos;
182}
183
184status_t StringPool::addStyleSpan(size_t idx, const String16& name,
185 uint32_t start, uint32_t end)
186{
187 entry_style_span span;
188 span.name = name;
189 span.span.firstChar = start;
190 span.span.lastChar = end;
191 return addStyleSpan(idx, span);
192}
193
194status_t StringPool::addStyleSpans(size_t idx, const Vector<entry_style_span>& spans)
195{
196 const size_t N=spans.size();
197 for (size_t i=0; i<N; i++) {
198 status_t err = addStyleSpan(idx, spans[i]);
199 if (err != NO_ERROR) {
200 return err;
201 }
202 }
203 return NO_ERROR;
204}
205
206status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span)
207{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 // Place blank entries in the span array up to this index.
209 while (mEntryStyleArray.size() <= idx) {
210 mEntryStyleArray.add();
211 }
212
213 entry_style& style = mEntryStyleArray.editItemAt(idx);
214 style.spans.add(span);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800215 mEntries.editItemAt(mEntryArray[idx]).hasStyles = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 return NO_ERROR;
217}
218
Dan Albert0de19ad2014-10-01 11:34:17 -0700219StringPool::ConfigSorter::ConfigSorter(const StringPool& pool) : pool(pool)
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800220{
Dan Albert0de19ad2014-10-01 11:34:17 -0700221}
222
223bool StringPool::ConfigSorter::operator()(size_t l, size_t r)
224{
225 const StringPool::entry& lhe = pool.mEntries[pool.mEntryArray[l]];
226 const StringPool::entry& rhe = pool.mEntries[pool.mEntryArray[r]];
227 return lhe.compare(rhe) < 0;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800228}
229
230void StringPool::sortByConfig()
231{
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800232 LOG_ALWAYS_FATAL_IF(mOriginalPosToNewPos.size() > 0, "Can't sort string pool after already sorted.");
233
234 const size_t N = mEntryArray.size();
235
236 // This is a vector that starts out with a 1:1 mapping to entries
237 // in the array, which we will sort to come up with the desired order.
238 // At that point it maps from the new position in the array to the
239 // original position the entry appeared.
240 Vector<size_t> newPosToOriginalPos;
Jeff Brownc9fd9262012-03-16 19:25:20 -0700241 newPosToOriginalPos.setCapacity(N);
242 for (size_t i=0; i < N; i++) {
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800243 newPosToOriginalPos.add(i);
244 }
245
246 // Sort the array.
247 NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n"));
Dan Albert0de19ad2014-10-01 11:34:17 -0700248 ConfigSorter sorter(*this);
249 std::sort(newPosToOriginalPos.begin(), newPosToOriginalPos.end(), sorter);
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800250 NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n"));
251
252 // Create the reverse mapping from the original position in the array
253 // to the new position where it appears in the sorted array. This is
254 // so that clients can re-map any positions they had previously stored.
255 mOriginalPosToNewPos = newPosToOriginalPos;
256 for (size_t i=0; i<N; i++) {
257 mOriginalPosToNewPos.editItemAt(newPosToOriginalPos[i]) = i;
258 }
259
260#if 0
261 SortedVector<entry> entries;
262
263 for (size_t i=0; i<N; i++) {
264 printf("#%d was %d: %s\n", i, newPosToOriginalPos[i],
265 mEntries[mEntryArray[newPosToOriginalPos[i]]].makeConfigsString().string());
266 entries.add(mEntries[mEntryArray[i]]);
267 }
268
269 for (size_t i=0; i<entries.size(); i++) {
270 printf("Sorted config #%d: %s\n", i,
271 entries[i].makeConfigsString().string());
272 }
273#endif
274
275 // Now we rebuild the arrays.
276 Vector<entry> newEntries;
277 Vector<size_t> newEntryArray;
278 Vector<entry_style> newEntryStyleArray;
279 DefaultKeyedVector<size_t, size_t> origOffsetToNewOffset;
280
281 for (size_t i=0; i<N; i++) {
282 // We are filling in new offset 'i'; oldI is where we can find it
283 // in the original data structure.
284 size_t oldI = newPosToOriginalPos[i];
285 // This is the actual entry associated with the old offset.
286 const entry& oldEnt = mEntries[mEntryArray[oldI]];
287 // This is the same entry the last time we added it to the
288 // new entry array, if any.
289 ssize_t newIndexOfOffset = origOffsetToNewOffset.indexOfKey(oldI);
290 size_t newOffset;
291 if (newIndexOfOffset < 0) {
292 // This is the first time we have seen the entry, so add
293 // it.
294 newOffset = newEntries.add(oldEnt);
295 newEntries.editItemAt(newOffset).indices.clear();
296 } else {
297 // We have seen this entry before, use the existing one
298 // instead of adding it again.
299 newOffset = origOffsetToNewOffset.valueAt(newIndexOfOffset);
300 }
301 // Update the indices to include this new position.
302 newEntries.editItemAt(newOffset).indices.add(i);
303 // And add the offset of the entry to the new entry array.
304 newEntryArray.add(newOffset);
305 // Add any old style to the new style array.
306 if (mEntryStyleArray.size() > 0) {
307 if (oldI < mEntryStyleArray.size()) {
308 newEntryStyleArray.add(mEntryStyleArray[oldI]);
309 } else {
310 newEntryStyleArray.add(entry_style());
311 }
312 }
313 }
314
315 // Now trim any entries at the end of the new style array that are
316 // not needed.
317 for (ssize_t i=newEntryStyleArray.size()-1; i>=0; i--) {
318 const entry_style& style = newEntryStyleArray[i];
319 if (style.spans.size() > 0) {
320 // That's it.
321 break;
322 }
323 // This one is not needed; remove.
324 newEntryStyleArray.removeAt(i);
325 }
326
327 // All done, install the new data structures and upate mValues with
328 // the new positions.
329 mEntries = newEntries;
330 mEntryArray = newEntryArray;
331 mEntryStyleArray = newEntryStyleArray;
332 mValues.clear();
333 for (size_t i=0; i<mEntries.size(); i++) {
334 const entry& ent = mEntries[i];
335 mValues.add(ent.value, ent.indices[0]);
336 }
337
338#if 0
339 printf("FINAL SORTED STRING CONFIGS:\n");
340 for (size_t i=0; i<mEntries.size(); i++) {
341 const entry& ent = mEntries[i];
342 printf("#" ZD " %s: %s\n", (ZD_TYPE)i, ent.makeConfigsString().string(),
343 String8(ent.value).string());
344 }
345#endif
346}
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348sp<AaptFile> StringPool::createStringBlock()
349{
350 sp<AaptFile> pool = new AaptFile(String8(), AaptGroupEntry(),
351 String8());
352 status_t err = writeStringBlock(pool);
353 return err == NO_ERROR ? pool : NULL;
354}
355
Kenny Root19138462009-12-04 09:38:48 -0800356#define ENCODE_LENGTH(str, chrsz, strSize) \
357{ \
358 size_t maxMask = 1 << ((chrsz*8)-1); \
359 size_t maxSize = maxMask-1; \
360 if (strSize > maxSize) { \
361 *str++ = maxMask | ((strSize>>(chrsz*8))&maxSize); \
362 } \
363 *str++ = strSize; \
364}
365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366status_t StringPool::writeStringBlock(const sp<AaptFile>& pool)
367{
368 // Allow appending. Sorry this is a little wacky.
369 if (pool->getSize() > 0) {
370 sp<AaptFile> block = createStringBlock();
371 if (block == NULL) {
372 return UNKNOWN_ERROR;
373 }
374 ssize_t res = pool->writeData(block->getData(), block->getSize());
375 return (res >= 0) ? (status_t)NO_ERROR : res;
376 }
377
378 // First we need to add all style span names to the string pool.
379 // We do this now (instead of when the span is added) so that these
380 // will appear at the end of the pool, not disrupting the order
381 // our client placed their own strings in it.
382
383 const size_t STYLES = mEntryStyleArray.size();
384 size_t i;
385
386 for (i=0; i<STYLES; i++) {
387 entry_style& style = mEntryStyleArray.editItemAt(i);
388 const size_t N = style.spans.size();
389 for (size_t i=0; i<N; i++) {
390 entry_style_span& span = style.spans.editItemAt(i);
391 ssize_t idx = add(span.name, true);
392 if (idx < 0) {
393 fprintf(stderr, "Error adding span for style tag '%s'\n",
394 String8(span.name).string());
395 return idx;
396 }
397 span.span.name.index = (uint32_t)idx;
398 }
399 }
400
Jeff Brown345b7eb2012-03-16 15:25:17 -0700401 const size_t ENTRIES = mEntryArray.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
403 // Now build the pool of unique strings.
404
405 const size_t STRINGS = mEntries.size();
406 const size_t preSize = sizeof(ResStringPool_header)
407 + (sizeof(uint32_t)*ENTRIES)
408 + (sizeof(uint32_t)*STYLES);
409 if (pool->editData(preSize) == NULL) {
410 fprintf(stderr, "ERROR: Out of memory for string pool\n");
411 return NO_MEMORY;
412 }
413
Kenny Root19138462009-12-04 09:38:48 -0800414 const size_t charSize = mUTF8 ? sizeof(uint8_t) : sizeof(char16_t);
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 size_t strPos = 0;
417 for (i=0; i<STRINGS; i++) {
418 entry& ent = mEntries.editItemAt(i);
419 const size_t strSize = (ent.value.size());
Kenny Root19138462009-12-04 09:38:48 -0800420 const size_t lenSize = strSize > (size_t)(1<<((charSize*8)-1))-1 ?
421 charSize*2 : charSize;
422
423 String8 encStr;
424 if (mUTF8) {
425 encStr = String8(ent.value);
426 }
427
428 const size_t encSize = mUTF8 ? encStr.size() : 0;
429 const size_t encLenSize = mUTF8 ?
430 (encSize > (size_t)(1<<((charSize*8)-1))-1 ?
431 charSize*2 : charSize) : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
433 ent.offset = strPos;
Kenny Root19138462009-12-04 09:38:48 -0800434
435 const size_t totalSize = lenSize + encLenSize +
436 ((mUTF8 ? encSize : strSize)+1)*charSize;
437
438 void* dat = (void*)pool->editData(preSize + strPos + totalSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 if (dat == NULL) {
440 fprintf(stderr, "ERROR: Out of memory for string pool\n");
441 return NO_MEMORY;
442 }
Kenny Root19138462009-12-04 09:38:48 -0800443 dat = (uint8_t*)dat + preSize + strPos;
444 if (mUTF8) {
445 uint8_t* strings = (uint8_t*)dat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
Kenny Root19138462009-12-04 09:38:48 -0800447 ENCODE_LENGTH(strings, sizeof(uint8_t), strSize)
448
449 ENCODE_LENGTH(strings, sizeof(uint8_t), encSize)
450
451 strncpy((char*)strings, encStr, encSize+1);
452 } else {
Dan Albertf348c152014-09-08 18:28:00 -0700453 char16_t* strings = (char16_t*)dat;
Kenny Root19138462009-12-04 09:38:48 -0800454
Dan Albertf348c152014-09-08 18:28:00 -0700455 ENCODE_LENGTH(strings, sizeof(char16_t), strSize)
Kenny Root19138462009-12-04 09:38:48 -0800456
457 strcpy16_htod(strings, ent.value);
458 }
459
460 strPos += totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
463 // Pad ending string position up to a uint32_t boundary.
464
465 if (strPos&0x3) {
466 size_t padPos = ((strPos+3)&~0x3);
467 uint8_t* dat = (uint8_t*)pool->editData(preSize + padPos);
468 if (dat == NULL) {
469 fprintf(stderr, "ERROR: Out of memory padding string pool\n");
470 return NO_MEMORY;
471 }
472 memset(dat+preSize+strPos, 0, padPos-strPos);
473 strPos = padPos;
474 }
475
476 // Build the pool of style spans.
477
478 size_t styPos = strPos;
479 for (i=0; i<STYLES; i++) {
480 entry_style& ent = mEntryStyleArray.editItemAt(i);
481 const size_t N = ent.spans.size();
482 const size_t totalSize = (N*sizeof(ResStringPool_span))
483 + sizeof(ResStringPool_ref);
484
485 ent.offset = styPos-strPos;
486 uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + totalSize);
487 if (dat == NULL) {
488 fprintf(stderr, "ERROR: Out of memory for string styles\n");
489 return NO_MEMORY;
490 }
491 ResStringPool_span* span = (ResStringPool_span*)(dat+preSize+styPos);
492 for (size_t i=0; i<N; i++) {
493 span->name.index = htodl(ent.spans[i].span.name.index);
494 span->firstChar = htodl(ent.spans[i].span.firstChar);
495 span->lastChar = htodl(ent.spans[i].span.lastChar);
496 span++;
497 }
498 span->name.index = htodl(ResStringPool_span::END);
499
500 styPos += totalSize;
501 }
502
503 if (STYLES > 0) {
504 // Add full terminator at the end (when reading we validate that
505 // the end of the pool is fully terminated to simplify error
506 // checking).
507 size_t extra = sizeof(ResStringPool_span)-sizeof(ResStringPool_ref);
508 uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + extra);
509 if (dat == NULL) {
510 fprintf(stderr, "ERROR: Out of memory for string styles\n");
511 return NO_MEMORY;
512 }
513 uint32_t* p = (uint32_t*)(dat+preSize+styPos);
514 while (extra > 0) {
515 *p++ = htodl(ResStringPool_span::END);
516 extra -= sizeof(uint32_t);
517 }
518 styPos += extra;
519 }
520
521 // Write header.
522
523 ResStringPool_header* header =
524 (ResStringPool_header*)pool->padData(sizeof(uint32_t));
525 if (header == NULL) {
526 fprintf(stderr, "ERROR: Out of memory for string pool\n");
527 return NO_MEMORY;
528 }
529 memset(header, 0, sizeof(*header));
530 header->header.type = htods(RES_STRING_POOL_TYPE);
531 header->header.headerSize = htods(sizeof(*header));
532 header->header.size = htodl(pool->getSize());
533 header->stringCount = htodl(ENTRIES);
534 header->styleCount = htodl(STYLES);
Kenny Root19138462009-12-04 09:38:48 -0800535 if (mUTF8) {
536 header->flags |= htodl(ResStringPool_header::UTF8_FLAG);
537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 header->stringsStart = htodl(preSize);
539 header->stylesStart = htodl(STYLES > 0 ? (preSize+strPos) : 0);
540
541 // Write string index array.
542
543 uint32_t* index = (uint32_t*)(header+1);
Jeff Brown345b7eb2012-03-16 15:25:17 -0700544 for (i=0; i<ENTRIES; i++) {
545 entry& ent = mEntries.editItemAt(mEntryArray[i]);
546 *index++ = htodl(ent.offset);
547 NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i,
548 String8(ent.value).string(),
549 mEntryArray[i], ent.offset));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
552 // Write style index array.
553
Jeff Brown345b7eb2012-03-16 15:25:17 -0700554 for (i=0; i<STYLES; i++) {
555 *index++ = htodl(mEntryStyleArray[i].offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557
558 return NO_ERROR;
559}
560
561ssize_t StringPool::offsetForString(const String16& val) const
562{
563 const Vector<size_t>* indices = offsetsForString(val);
564 ssize_t res = indices != NULL && indices->size() > 0 ? indices->itemAt(0) : -1;
565 NOISY(printf("Offset for string %s: %d (%s)\n", String8(val).string(), res,
566 res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8()));
567 return res;
568}
569
570const Vector<size_t>* StringPool::offsetsForString(const String16& val) const
571{
572 ssize_t pos = mValues.valueFor(val);
573 if (pos < 0) {
574 return NULL;
575 }
576 return &mEntries[mEntryArray[pos]].indices;
577}