Home | History | Annotate | Download | only in toolutil
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 2005-2009, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *   file name:  package.h
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2005aug25
     14 *   created by: Markus W. Scherer
     15 *
     16 *   Read, modify, and write ICU .dat data package files.
     17 */
     18 
     19 #ifndef __PACKAGE_H__
     20 #define __PACKAGE_H__
     21 
     22 #include "unicode/utypes.h"
     23 
     24 #include <stdio.h>
     25 
     26 // .dat package file representation ---------------------------------------- ***
     27 
     28 #define STRING_STORE_SIZE 100000
     29 #define MAX_FILE_COUNT 2000
     30 #define MAX_PKG_NAME_LENGTH 32
     31 
     32 typedef void CheckDependency(void *context, const char *itemName, const char *targetName);
     33 
     34 U_NAMESPACE_BEGIN
     35 
     36 struct Item {
     37     char *name;
     38     uint8_t *data;
     39     int32_t length;
     40     UBool isDataOwned;
     41     char type;
     42 };
     43 
     44 class U_TOOLUTIL_API Package {
     45 public:
     46     /*
     47      * Constructor.
     48      * Prepare this object for a new, empty package.
     49      */
     50     Package();
     51 
     52     /* Destructor. */
     53     ~Package();
     54 
     55     /*
     56      * Read an existing .dat package file.
     57      * The header and item name strings are swapped into this object,
     58      * but the items are left unswapped.
     59      */
     60     void readPackage(const char *filename);
     61     /*
     62      * Write a .dat package file with the items in this object.
     63      * Swap all pieces to the desired output platform properties.
     64      * The package becomes unusable:
     65      * The item names are swapped and sorted in the outCharset rather than the local one.
     66      * Also, the items themselves are swapped in-place
     67      */
     68     void writePackage(const char *filename, char outType, const char *comment);
     69 
     70     /*
     71      * Return the input data type letter (l, b, or e).
     72      */
     73     char getInType();
     74 
     75     // find the item in items[], return the non-negative index if found, else the binary-not of the insertion point
     76     int32_t findItem(const char *name, int32_t length=-1) const;
     77 
     78     /*
     79      * Set internal state for following calls to findNextItem() which will return
     80      * indexes for items whose names match the pattern.
     81      */
     82     void findItems(const char *pattern);
     83     int32_t findNextItem();
     84     /*
     85      * Set the match mode for findItems() & findNextItem().
     86      * @param mode 0=default
     87      *             MATCH_NOSLASH * does not match a '/'
     88      */
     89     void setMatchMode(uint32_t mode);
     90 
     91     enum {
     92         MATCH_NOSLASH=1
     93     };
     94 
     95     void addItem(const char *name);
     96     void addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOwned, char type);
     97     void addFile(const char *filesPath, const char *name);
     98     void addItems(const Package &listPkg);
     99 
    100     void removeItem(int32_t itemIndex);
    101     void removeItems(const char *pattern);
    102     void removeItems(const Package &listPkg);
    103 
    104     /* The extractItem() functions accept outputType=0 to mean "don't swap the item". */
    105     void extractItem(const char *filesPath, int32_t itemIndex, char outType);
    106     void extractItems(const char *filesPath, const char *pattern, char outType);
    107     void extractItems(const char *filesPath, const Package &listPkg, char outType);
    108 
    109     /* This variant extracts an item to a specific filename. */
    110     void extractItem(const char *filesPath, const char *outName, int32_t itemIndex, char outType);
    111 
    112     int32_t getItemCount() const;
    113     const Item *getItem(int32_t idx) const;
    114 
    115     /*
    116      * Check dependencies and return TRUE if all dependencies are fulfilled.
    117      */
    118     UBool checkDependencies();
    119 
    120     /*
    121      * Enumerate all the dependencies and give the results to context and check
    122      */
    123     void enumDependencies(void *context, CheckDependency check);
    124 
    125 private:
    126     void enumDependencies(Item *pItem, void *context, CheckDependency check);
    127 
    128     static void checkDependency(void *context, const char *itemName, const char *targetName);
    129 
    130     /*
    131      * Allocate a string in inStrings or outStrings.
    132      * The length does not include the terminating NUL.
    133      */
    134     char *allocString(UBool in, int32_t length);
    135 
    136     void sortItems();
    137 
    138     // data fields
    139     char inPkgName[MAX_PKG_NAME_LENGTH];
    140 
    141     uint8_t *inData;
    142     uint8_t header[1024];
    143     int32_t inLength, headerLength;
    144     uint8_t inCharset;
    145     UBool inIsBigEndian;
    146 
    147     int32_t itemCount;
    148     Item items[MAX_FILE_COUNT];
    149 
    150     int32_t inStringTop, outStringTop;
    151     char inStrings[STRING_STORE_SIZE], outStrings[STRING_STORE_SIZE];
    152 
    153     // match mode for findItems(pattern) and findNextItem()
    154     uint32_t matchMode;
    155 
    156     // state for findItems(pattern) and findNextItem()
    157     const char *findPrefix, *findSuffix;
    158     int32_t findPrefixLength, findSuffixLength;
    159     int32_t findNextIndex;
    160 
    161     // state for checkDependencies()
    162     UBool isMissingItems;
    163 };
    164 
    165 U_NAMESPACE_END
    166 
    167 #endif
    168