blob: dbe8c8542185f81ea269019ce6edc76f4bc091ca [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#ifndef STRING_POOL_H
8#define STRING_POOL_H
9
10#include "Main.h"
11#include "AaptAssets.h"
12
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080013#include <androidfw/ResourceTypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014#include <utils/String16.h>
Jeff Brown8a9cfcc2012-03-16 15:24:32 -070015#include <utils/TypeHelpers.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <ctype.h>
21#include <errno.h>
22
Elliott Hughesee15e152012-09-09 14:45:32 -070023#include <libexpat/expat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25using namespace android;
26
27#define PRINT_STRING_METRICS 0
28
Adam Lesinski4bf58102014-11-03 11:21:19 -080029#if __cplusplus >= 201103L
Dan Albertf348c152014-09-08 18:28:00 -070030void strcpy16_htod(char16_t* dst, const char16_t* src);
Adam Lesinski4bf58102014-11-03 11:21:19 -080031#endif
32void strcpy16_htod(uint16_t* dst, const char16_t* src);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34void printStringPool(const ResStringPool* pool);
35
36/**
37 * The StringPool class is used as an intermediate representation for
38 * generating the string pool resource data structure that can be parsed with
39 * ResStringPool in include/utils/ResourceTypes.h.
40 */
41class StringPool
42{
43public:
44 struct entry {
45 entry() : offset(0) { }
Dianne Hackborn6c997a92012-01-31 11:27:43 -080046 entry(const String16& _value) : value(_value), offset(0), hasStyles(false) { }
47 entry(const entry& o) : value(o.value), offset(o.offset),
48 hasStyles(o.hasStyles), indices(o.indices),
49 configTypeName(o.configTypeName), configs(o.configs) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51 String16 value;
52 size_t offset;
Dianne Hackborn6c997a92012-01-31 11:27:43 -080053 bool hasStyles;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 Vector<size_t> indices;
Dianne Hackborn6c997a92012-01-31 11:27:43 -080055 String8 configTypeName;
56 Vector<ResTable_config> configs;
57
58 String8 makeConfigsString() const;
59
60 int compare(const entry& o) const;
61
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; }
65 inline bool operator!=(const entry& o) const { return compare(o) != 0; }
66 inline bool operator>=(const entry& o) const { return compare(o) >= 0; }
67 inline bool operator>(const entry& o) const { return compare(o) > 0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 };
69
70 struct entry_style_span {
71 String16 name;
72 ResStringPool_span span;
73 };
74
75 struct entry_style {
76 entry_style() : offset(0) { }
77
78 entry_style(const entry_style& o) : offset(o.offset), spans(o.spans) { }
79
80 size_t offset;
81 Vector<entry_style_span> spans;
82 };
83
84 /**
Kenny Root19138462009-12-04 09:38:48 -080085 * If 'utf8' is true, strings will be encoded with UTF-8 instead of
86 * left in Java's native UTF-16.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
Jeff Brown345b7eb2012-03-16 15:25:17 -070088 explicit StringPool(bool utf8 = false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 /**
91 * Add a new string to the pool. If mergeDuplicates is true, thenif
92 * the string already exists the existing entry for it will be used;
93 * otherwise, or if the value doesn't already exist, a new entry is
94 * created.
95 *
Jeff Brown345b7eb2012-03-16 15:25:17 -070096 * Returns the index in the entry array of the new string entry.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 */
Dianne Hackborn6c997a92012-01-31 11:27:43 -080098 ssize_t add(const String16& value, bool mergeDuplicates = false,
99 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800101 ssize_t add(const String16& value, const Vector<entry_style_span>& spans,
102 const String8* configTypeName = NULL, const ResTable_config* config = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 status_t addStyleSpan(size_t idx, const String16& name,
105 uint32_t start, uint32_t end);
106 status_t addStyleSpans(size_t idx, const Vector<entry_style_span>& spans);
107 status_t addStyleSpan(size_t idx, const entry_style_span& span);
108
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800109 // Sort the contents of the string block by the configuration associated
110 // with each item. After doing this you can use mapOriginalPosToNewPos()
Jeff Brown345b7eb2012-03-16 15:25:17 -0700111 // to find out the new position given the position originally returned by
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800112 // add().
113 void sortByConfig();
114
115 // For use after sortByConfig() to map from the original position of
116 // a string to its new sorted position.
117 size_t mapOriginalPosToNewPos(size_t originalPos) const {
118 return mOriginalPosToNewPos.itemAt(originalPos);
119 }
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 sp<AaptFile> createStringBlock();
122
123 status_t writeStringBlock(const sp<AaptFile>& pool);
124
125 /**
126 * Find out an offset in the pool for a particular string. If the string
127 * pool is sorted, this can not be called until after createStringBlock()
128 * or writeStringBlock() has been called
129 * (which determines the offsets). In the case of a string that appears
130 * multiple times in the pool, the first offset will be returned. Returns
131 * -1 if the string does not exist.
132 */
133 ssize_t offsetForString(const String16& val) const;
134
135 /**
136 * Find all of the offsets in the pool for a particular string. If the
137 * string pool is sorted, this can not be called until after
138 * createStringBlock() or writeStringBlock() has been called
139 * (which determines the offsets). Returns NULL if the string does not exist.
140 */
141 const Vector<size_t>* offsetsForString(const String16& val) const;
142
143private:
Dan Albert0de19ad2014-10-01 11:34:17 -0700144 class ConfigSorter
145 {
146 public:
147 explicit ConfigSorter(const StringPool&);
148 bool operator()(size_t l, size_t r);
149 private:
150 const StringPool& pool;
151 };
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800152
Kenny Root19138462009-12-04 09:38:48 -0800153 const bool mUTF8;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800154
155 // The following data structures represent the actual structures
156 // that will be generated for the final string pool.
157
158 // Raw array of unique strings, in some arbitrary order. This is the
159 // actual strings that appear in the final string pool, in the order
160 // that they will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 Vector<entry> mEntries;
162 // Array of indices into mEntries, in the order they were
163 // added to the pool. This can be different than mEntries
164 // if the same string was added multiple times (it will appear
165 // once in mEntries, with multiple occurrences in this array).
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800166 // This is the lookup array that will be written for finding
167 // the string for each offset/position in the string pool.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 Vector<size_t> mEntryArray;
169 // Optional style span information associated with each index of
170 // mEntryArray.
171 Vector<entry_style> mEntryStyleArray;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800172
173 // The following data structures are used for book-keeping as the
174 // string pool is constructed.
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 // Unique set of all the strings added to the pool, mapped to
177 // the first index of mEntryArray where the value was added.
178 DefaultKeyedVector<String16, ssize_t> mValues;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800179 // This array maps from the original position a string was placed at
180 // in mEntryArray to its new position after being sorted with sortByConfig().
181 Vector<size_t> mOriginalPosToNewPos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182};
183
Jeff Brown8a9cfcc2012-03-16 15:24:32 -0700184// The entry types are trivially movable because all fields they contain, including
185// the vectors and strings, are trivially movable.
186namespace android {
187 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry);
188 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span);
189 ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style);
190};
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192#endif
193