package.h 5.7 KB

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