Home | History | Annotate | Download | only in toolutil
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 2005-2010, 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_PKG_NAME_LENGTH 32
     30 
     31 typedef void CheckDependency(void *context, const char *itemName, const char *targetName);
     32 
     33 U_NAMESPACE_BEGIN
     34 
     35 struct Item {
     36     char *name;
     37     uint8_t *data;
     38     int32_t length;
     39     UBool isDataOwned;
     40     char type;
     41 };
     42 
     43 class U_TOOLUTIL_API Package {
     44 public:
     45     /*
     46      * Constructor.
     47      * Prepare this object for a new, empty package.
     48      */
     49     Package();
     50 
     51     /* Destructor. */
     52     ~Package();
     53 
     54     /*
     55      * Read an existing .dat package file.
     56      * The header and item name strings are swapped into this object,
     57      * but the items are left unswapped.
     58      */
     59     void readPackage(const char *filename);
     60     /*
     61      * Write a .dat package file with the items in this object.
     62      * Swap all pieces to the desired output platform properties.
     63      * The package becomes unusable:
     64      * The item names are swapped and sorted in the outCharset rather than the local one.
     65      * Also, the items themselves are swapped in-place
     66      */
     67     void writePackage(const char *filename, char outType, const char *comment);
     68 
     69     /*
     70      * Return the input data type letter (l, b, or e).
     71      */
     72     char getInType();
     73 
     74     // find the item in items[], return the non-negative index if found, else the binary-not of the insertion point
     75     int32_t findItem(const char *name, int32_t length=-1) const;
     76 
     77     /*
     78      * Set internal state for following calls to findNextItem() which will return
     79      * indexes for items whose names match the pattern.
     80      */
     81     void findItems(const char *pattern);
     82     int32_t findNextItem();
     83     /*
     84      * Set the match mode for findItems() & findNextItem().
     85      * @param mode 0=default
     86      *             MATCH_NOSLASH * does not match a '/'
     87      */
     88     void setMatchMode(uint32_t mode);
     89 
     90     enum {
     91         MATCH_NOSLASH=1
     92     };
     93 
     94     void addItem(const char *name);
     95     void addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOwned, char type);
     96     void addFile(const char *filesPath, const char *name);
     97     void addItems(const Package &listPkg);
     98 
     99     void removeItem(int32_t itemIndex);
    100     void removeItems(const char *pattern);
    101     void removeItems(const Package &listPkg);
    102 
    103     /* The extractItem() functions accept outputType=0 to mean "don't swap the item". */
    104     void extractItem(const char *filesPath, int32_t itemIndex, char outType);
    105     void extractItems(const char *filesPath, const char *pattern, char outType);
    106     void extractItems(const char *filesPath, const Package &listPkg, char outType);
    107 
    108     /* This variant extracts an item to a specific filename. */
    109     void extractItem(const char *filesPath, const char *outName, int32_t itemIndex, char outType);
    110 
    111     int32_t getItemCount() const;
    112     const Item *getItem(int32_t idx) const;
    113 
    114     /*
    115      * Check dependencies and return TRUE if all dependencies are fulfilled.
    116      */
    117     UBool checkDependencies();
    118 
    119     /*
    120      * Enumerate all the dependencies and give the results to context and call CheckDependency callback
    121      * @param context user context (will be passed to check function)
    122      * @param check will be called with context and any missing items
    123      */
    124     void enumDependencies(void *context, CheckDependency check);
    125 
    126 private:
    127     void enumDependencies(Item *pItem, void *context, CheckDependency check);
    128 
    129     /**
    130      * Default CheckDependency function used by checkDependencies()
    131      */
    132     static void checkDependency(void *context, const char *itemName, const char *targetName);
    133 
    134     /*
    135      * Allocate a string in inStrings or outStrings.
    136      * The length does not include the terminating NUL.
    137      */
    138     char *allocString(UBool in, int32_t length);
    139 
    140     void sortItems();
    141 
    142     // data fields
    143     char inPkgName[MAX_PKG_NAME_LENGTH];
    144 
    145     uint8_t *inData;
    146     uint8_t header[1024];
    147     int32_t inLength, headerLength;
    148     uint8_t inCharset;
    149     UBool inIsBigEndian;
    150 
    151     int32_t itemCount;
    152     int32_t itemMax;
    153     Item   *items;
    154 
    155     int32_t inStringTop, outStringTop;
    156     char inStrings[STRING_STORE_SIZE], outStrings[STRING_STORE_SIZE];
    157 
    158     // match mode for findItems(pattern) and findNextItem()
    159     uint32_t matchMode;
    160 
    161     // state for findItems(pattern) and findNextItem()
    162     const char *findPrefix, *findSuffix;
    163     int32_t findPrefixLength, findSuffixLength;
    164     int32_t findNextIndex;
    165 
    166     // state for checkDependencies()
    167     UBool isMissingItems;
    168 
    169     /**
    170      * Grow itemMax to new value
    171      */
    172     void setItemCapacity(int32_t max);
    173 
    174     /**
    175      * Grow itemMax to at least itemCount+1
    176      */
    177     void ensureItemCapacity();
    178 };
    179 
    180 U_NAMESPACE_END
    181 
    182 #endif
    183 
    184 
    185