michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2005-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: package.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2005aug25 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Read, modify, and write ICU .dat data package files. michael@0: */ michael@0: michael@0: #ifndef __PACKAGE_H__ michael@0: #define __PACKAGE_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #include michael@0: michael@0: // .dat package file representation ---------------------------------------- *** michael@0: michael@0: #define STRING_STORE_SIZE 100000 michael@0: #define MAX_PKG_NAME_LENGTH 32 michael@0: michael@0: typedef void CheckDependency(void *context, const char *itemName, const char *targetName); michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: struct Item { michael@0: char *name; michael@0: uint8_t *data; michael@0: int32_t length; michael@0: UBool isDataOwned; michael@0: char type; michael@0: }; michael@0: michael@0: class U_TOOLUTIL_API Package { michael@0: public: michael@0: /* michael@0: * Constructor. michael@0: * Prepare this object for a new, empty package. michael@0: */ michael@0: Package(); michael@0: michael@0: /* Destructor. */ michael@0: ~Package(); michael@0: michael@0: /** michael@0: * Uses the prefix of the first entry of the package in readPackage(), michael@0: * rather than the package basename. michael@0: */ michael@0: void setAutoPrefix() { doAutoPrefix=TRUE; } michael@0: /** michael@0: * Same as setAutoPrefix(), plus the prefix must end with the platform type letter. michael@0: */ michael@0: void setAutoPrefixWithType() { michael@0: doAutoPrefix=TRUE; michael@0: prefixEndsWithType=TRUE; michael@0: } michael@0: void setPrefix(const char *p); michael@0: michael@0: /* michael@0: * Read an existing .dat package file. michael@0: * The header and item name strings are swapped into this object, michael@0: * but the items are left unswapped. michael@0: */ michael@0: void readPackage(const char *filename); michael@0: /* michael@0: * Write a .dat package file with the items in this object. michael@0: * Swap all pieces to the desired output platform properties. michael@0: * The package becomes unusable: michael@0: * The item names are swapped and sorted in the outCharset rather than the local one. michael@0: * Also, the items themselves are swapped in-place michael@0: */ michael@0: void writePackage(const char *filename, char outType, const char *comment); michael@0: michael@0: /* michael@0: * Return the input data type letter (l, b, or e). michael@0: */ michael@0: char getInType(); michael@0: michael@0: // find the item in items[], return the non-negative index if found, else the binary-not of the insertion point michael@0: int32_t findItem(const char *name, int32_t length=-1) const; michael@0: michael@0: /* michael@0: * Set internal state for following calls to findNextItem() which will return michael@0: * indexes for items whose names match the pattern. michael@0: */ michael@0: void findItems(const char *pattern); michael@0: int32_t findNextItem(); michael@0: /* michael@0: * Set the match mode for findItems() & findNextItem(). michael@0: * @param mode 0=default michael@0: * MATCH_NOSLASH * does not match a '/' michael@0: */ michael@0: void setMatchMode(uint32_t mode); michael@0: michael@0: enum { michael@0: MATCH_NOSLASH=1 michael@0: }; michael@0: michael@0: void addItem(const char *name); michael@0: void addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOwned, char type); michael@0: void addFile(const char *filesPath, const char *name); michael@0: void addItems(const Package &listPkg); michael@0: michael@0: void removeItem(int32_t itemIndex); michael@0: void removeItems(const char *pattern); michael@0: void removeItems(const Package &listPkg); michael@0: michael@0: /* The extractItem() functions accept outputType=0 to mean "don't swap the item". */ michael@0: void extractItem(const char *filesPath, int32_t itemIndex, char outType); michael@0: void extractItems(const char *filesPath, const char *pattern, char outType); michael@0: void extractItems(const char *filesPath, const Package &listPkg, char outType); michael@0: michael@0: /* This variant extracts an item to a specific filename. */ michael@0: void extractItem(const char *filesPath, const char *outName, int32_t itemIndex, char outType); michael@0: michael@0: int32_t getItemCount() const; michael@0: const Item *getItem(int32_t idx) const; michael@0: michael@0: /* michael@0: * Check dependencies and return TRUE if all dependencies are fulfilled. michael@0: */ michael@0: UBool checkDependencies(); michael@0: michael@0: /* michael@0: * Enumerate all the dependencies and give the results to context and call CheckDependency callback michael@0: * @param context user context (will be passed to check function) michael@0: * @param check will be called with context and any missing items michael@0: */ michael@0: void enumDependencies(void *context, CheckDependency check); michael@0: michael@0: private: michael@0: void enumDependencies(Item *pItem, void *context, CheckDependency check); michael@0: michael@0: /** michael@0: * Default CheckDependency function used by checkDependencies() michael@0: */ michael@0: static void checkDependency(void *context, const char *itemName, const char *targetName); michael@0: michael@0: /* michael@0: * Allocate a string in inStrings or outStrings. michael@0: * The length does not include the terminating NUL. michael@0: */ michael@0: char *allocString(UBool in, int32_t length); michael@0: michael@0: void sortItems(); michael@0: michael@0: // data fields michael@0: char inPkgName[MAX_PKG_NAME_LENGTH]; michael@0: char pkgPrefix[MAX_PKG_NAME_LENGTH]; michael@0: michael@0: uint8_t *inData; michael@0: uint8_t header[1024]; michael@0: int32_t inLength, headerLength; michael@0: uint8_t inCharset; michael@0: UBool inIsBigEndian; michael@0: UBool doAutoPrefix; michael@0: UBool prefixEndsWithType; michael@0: michael@0: int32_t itemCount; michael@0: int32_t itemMax; michael@0: Item *items; michael@0: michael@0: int32_t inStringTop, outStringTop; michael@0: char inStrings[STRING_STORE_SIZE], outStrings[STRING_STORE_SIZE]; michael@0: michael@0: // match mode for findItems(pattern) and findNextItem() michael@0: uint32_t matchMode; michael@0: michael@0: // state for findItems(pattern) and findNextItem() michael@0: const char *findPrefix, *findSuffix; michael@0: int32_t findPrefixLength, findSuffixLength; michael@0: int32_t findNextIndex; michael@0: michael@0: // state for checkDependencies() michael@0: UBool isMissingItems; michael@0: michael@0: /** michael@0: * Grow itemMax to new value michael@0: */ michael@0: void setItemCapacity(int32_t max); michael@0: michael@0: /** michael@0: * Grow itemMax to at least itemCount+1 michael@0: */ michael@0: void ensureItemCapacity(); michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: michael@0: