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