Home | History | Annotate | Download | only in aapt
      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 <androidfw/ResourceTypes.h>
     14 #include <utils/String16.h>
     15 #include <utils/TypeHelpers.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 <libexpat/expat.h>
     24 
     25 using namespace android;
     26 
     27 #define PRINT_STRING_METRICS 0
     28 
     29 #if __cplusplus >= 201103L
     30 void strcpy16_htod(char16_t* dst, const char16_t* src);
     31 #endif
     32 void strcpy16_htod(uint16_t* dst, const char16_t* src);
     33 
     34 void 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  */
     41 class StringPool
     42 {
     43 public:
     44     struct entry {
     45         entry() : offset(0) { }
     46         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) { }
     50 
     51         String16 value;
     52         size_t offset;
     53         bool hasStyles;
     54         Vector<size_t> indices;
     55         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; }
     68     };
     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     /**
     85      * If 'utf8' is true, strings will be encoded with UTF-8 instead of
     86      * left in Java's native UTF-16.
     87      */
     88     explicit StringPool(bool utf8 = false);
     89 
     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      *
     96      * Returns the index in the entry array of the new string entry.
     97      */
     98     ssize_t add(const String16& value, bool mergeDuplicates = false,
     99             const String8* configTypeName = NULL, const ResTable_config* config = NULL);
    100 
    101     ssize_t add(const String16& value, const Vector<entry_style_span>& spans,
    102             const String8* configTypeName = NULL, const ResTable_config* config = NULL);
    103 
    104     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 
    109     // Sort the contents of the string block by the configuration associated
    110     // with each item.  After doing this you can use mapOriginalPosToNewPos()
    111     // to find out the new position given the position originally returned by
    112     // 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 
    121     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 
    143 private:
    144     static int config_sort(void* state, const void* lhs, const void* rhs);
    145 
    146     const bool                              mUTF8;
    147 
    148     // The following data structures represent the actual structures
    149     // that will be generated for the final string pool.
    150 
    151     // Raw array of unique strings, in some arbitrary order.  This is the
    152     // actual strings that appear in the final string pool, in the order
    153     // that they will be written.
    154     Vector<entry>                           mEntries;
    155     // Array of indices into mEntries, in the order they were
    156     // added to the pool.  This can be different than mEntries
    157     // if the same string was added multiple times (it will appear
    158     // once in mEntries, with multiple occurrences in this array).
    159     // This is the lookup array that will be written for finding
    160     // the string for each offset/position in the string pool.
    161     Vector<size_t>                          mEntryArray;
    162     // Optional style span information associated with each index of
    163     // mEntryArray.
    164     Vector<entry_style>                     mEntryStyleArray;
    165 
    166     // The following data structures are used for book-keeping as the
    167     // string pool is constructed.
    168 
    169     // Unique set of all the strings added to the pool, mapped to
    170     // the first index of mEntryArray where the value was added.
    171     DefaultKeyedVector<String16, ssize_t>   mValues;
    172     // This array maps from the original position a string was placed at
    173     // in mEntryArray to its new position after being sorted with sortByConfig().
    174     Vector<size_t>                          mOriginalPosToNewPos;
    175 };
    176 
    177 // The entry types are trivially movable because all fields they contain, including
    178 // the vectors and strings, are trivially movable.
    179 namespace android {
    180     ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry);
    181     ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span);
    182     ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style);
    183 };
    184 
    185 #endif
    186 
    187