1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2000-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
13 * Modification History:
15 * Date Name Description
16 * 02/21/00 weiv Creation.
17 *******************************************************************************
23 #define KEY_SPACE_SIZE 65536
24 #define RESLIST_MAX_INT_VECTOR 2048
26 #include "unicode/utypes.h"
27 #include "unicode/unistr.h"
28 #include "unicode/ures.h"
29 #include "unicode/ustring.h"
39 class PseudoListResource
;
43 : fBytes(NULL
), fIndexes(NULL
),
44 fKeys(NULL
), fKeysLength(0), fKeysCount(0),
45 fStrings(NULL
), fStringIndexLimit(0),
47 ~ResFile() { close(); }
52 const int32_t *fIndexes
;
57 PseudoListResource
*fStrings
;
58 int32_t fStringIndexLimit
;
65 typedef struct KeyMapEntry
{
66 int32_t oldpos
, newpos
;
69 /* Resource bundle root table */
71 SRBRoot(const UString
*comment
, UBool isPoolBundle
, UErrorCode
&errorCode
);
74 void write(const char *outputDir
, const char *outputPkg
,
75 char *writtenFilename
, int writtenFilenameLen
, UErrorCode
&errorCode
);
77 void setLocale(UChar
*locale
, UErrorCode
&errorCode
);
78 int32_t addTag(const char *tag
, UErrorCode
&errorCode
);
80 const char *getKeyString(int32_t key
) const;
81 const char *getKeyBytes(int32_t *pLength
) const;
83 int32_t addKeyBytes(const char *keyBytes
, int32_t length
, UErrorCode
&errorCode
);
85 void compactKeys(UErrorCode
&errorCode
);
87 int32_t makeRes16(uint32_t resWord
) const;
88 int32_t mapKey(int32_t oldpos
) const;
91 void compactStringsV2(UHashtable
*stringSet
, UErrorCode
&errorCode
);
96 SResource
*fRoot
; // Normally a TableResource.
99 int32_t fMaxTableLength
;
100 UBool fNoFallback
; /* see URES_ATT_NO_FALLBACK */
101 int8_t fStringsForm
; /* default STRINGS_UTF16_V1 */
105 KeyMapEntry
*fKeyMap
;
106 int32_t fKeysBottom
, fKeysTop
;
107 int32_t fKeysCapacity
;
109 int32_t fLocalKeyLimit
; /* key offset < limit fits into URES_TABLE */
111 icu::UnicodeString f16BitUnits
;
112 int32_t f16BitStringsLength
;
114 const ResFile
*fUsePoolBundle
;
115 int32_t fPoolStringIndexLimit
;
116 int32_t fPoolStringIndex16Limit
;
117 int32_t fLocalStringIndexLimit
;
118 SRBRoot
*fWritePoolBundle
;
121 /* write a java resource file */
123 void bundle_write_java(struct SRBRoot
*bundle
, const char *outputDir
, const char* outputEnc
, char *writtenFilename
,
124 int writtenFilenameLen
, const char* packageName
, const char* bundleName
, UErrorCode
*status
);
126 /* write a xml resource file */
128 void bundle_write_xml(struct SRBRoot
*bundle
, const char *outputDir
,const char* outputEnc
, const char* rbname
,
129 char *writtenFilename
, int writtenFilenameLen
, const char* language
, const char* package
, UErrorCode
*status
);
131 /* Various resource types */
134 * Return a unique pointer to a dummy object,
135 * for use in non-error cases when no resource is to be added to the bundle.
136 * (NULL is used in error cases.)
138 struct SResource
* res_none(void);
142 class IntVectorResource
;
144 TableResource
*table_open(struct SRBRoot
*bundle
, const char *tag
, const struct UString
* comment
, UErrorCode
*status
);
146 ArrayResource
*array_open(struct SRBRoot
*bundle
, const char *tag
, const struct UString
* comment
, UErrorCode
*status
);
148 struct SResource
*string_open(struct SRBRoot
*bundle
, const char *tag
, const UChar
*value
, int32_t len
, const struct UString
* comment
, UErrorCode
*status
);
150 struct SResource
*alias_open(struct SRBRoot
*bundle
, const char *tag
, UChar
*value
, int32_t len
, const struct UString
* comment
, UErrorCode
*status
);
152 IntVectorResource
*intvector_open(struct SRBRoot
*bundle
, const char *tag
, const struct UString
* comment
, UErrorCode
*status
);
154 struct SResource
*int_open(struct SRBRoot
*bundle
, const char *tag
, int32_t value
, const struct UString
* comment
, UErrorCode
*status
);
156 struct SResource
*bin_open(struct SRBRoot
*bundle
, const char *tag
, uint32_t length
, uint8_t *data
, const char* fileName
, const struct UString
* comment
, UErrorCode
*status
);
158 /* Resource place holder */
162 SResource(SRBRoot
*bundle
, const char *tag
, int8_t type
, const UString
* comment
,
163 UErrorCode
&errorCode
);
164 virtual ~SResource();
166 UBool
isTable() const { return fType
== URES_TABLE
; }
167 UBool
isString() const { return fType
== URES_STRING
; }
169 const char *getKeyString(const SRBRoot
*bundle
) const;
172 * Preflights strings.
173 * Finds duplicates and counts the total number of string code units
174 * so that they can be written first to the 16-bit array,
175 * for minimal string and container storage.
177 * We walk the final parse tree, rather than collecting this information while building it,
178 * so that we need not deal with changes to the parse tree (especially removing resources).
180 void preflightStrings(SRBRoot
*bundle
, UHashtable
*stringSet
, UErrorCode
&errorCode
);
181 virtual void handlePreflightStrings(SRBRoot
*bundle
, UHashtable
*stringSet
, UErrorCode
&errorCode
);
184 * Writes resource values into f16BitUnits
185 * and determines the resource item word, if possible.
187 void write16(SRBRoot
*bundle
);
188 virtual void handleWrite16(SRBRoot
*bundle
);
191 * Calculates ("preflights") and advances the *byteOffset
192 * by the size of the resource's data in the binary file and
193 * determines the resource item word.
195 * Most handlePreWrite() functions may add any number of bytes, but preWrite()
196 * will always pad it to a multiple of 4.
197 * The resource item type may be a related subtype of the fType.
199 * The preWrite() and write() functions start and end at the same
201 * Prewriting allows bundle.write() to determine the root resource item word,
202 * before actually writing the bundle contents to the file,
203 * which is necessary because the root item is stored at the beginning.
205 void preWrite(uint32_t *byteOffset
);
206 virtual void handlePreWrite(uint32_t *byteOffset
);
209 * Writes the resource's data to mem and updates the byteOffset
212 void write(UNewDataMemory
*mem
, uint32_t *byteOffset
);
213 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
215 int8_t fType
; /* nominal type: fRes (when != 0xffffffff) may use subtype */
216 UBool fWritten
; /* res_write() can exit early */
217 uint32_t fRes
; /* resource item word; RES_BOGUS=0xffffffff if not known yet */
218 int32_t fRes16
; /* Res16 version of fRes for Table, Table16, Array16; -1 if it does not fit. */
219 int32_t fKey
; /* Index into bundle->fKeys; -1 if no key. */
220 int32_t fKey16
; /* Key16 version of fKey for Table & Table16; -1 if no key or it does not fit. */
221 int line
; /* used internally to report duplicate keys in tables */
222 SResource
*fNext
; /* This is for internal chaining while building */
223 struct UString fComment
;
226 class ContainerResource
: public SResource
{
228 ContainerResource(SRBRoot
*bundle
, const char *tag
, int8_t type
,
229 const UString
* comment
, UErrorCode
&errorCode
)
230 : SResource(bundle
, tag
, type
, comment
, errorCode
),
231 fCount(0), fFirst(NULL
) {}
232 virtual ~ContainerResource();
234 virtual void handlePreflightStrings(SRBRoot
*bundle
, UHashtable
*stringSet
, UErrorCode
&errorCode
);
236 void writeAllRes16(SRBRoot
*bundle
);
237 void preWriteAllRes(uint32_t *byteOffset
);
238 void writeAllRes(UNewDataMemory
*mem
, uint32_t *byteOffset
);
239 void writeAllRes32(UNewDataMemory
*mem
, uint32_t *byteOffset
);
242 // TODO: private with getter?
247 class TableResource
: public ContainerResource
{
249 TableResource(SRBRoot
*bundle
, const char *tag
,
250 const UString
* comment
, UErrorCode
&errorCode
)
251 : ContainerResource(bundle
, tag
, URES_TABLE
, comment
, errorCode
),
252 fTableType(URES_TABLE
), fRoot(bundle
) {}
253 virtual ~TableResource();
255 void add(SResource
*res
, int linenumber
, UErrorCode
&errorCode
);
257 virtual void handleWrite16(SRBRoot
*bundle
);
258 virtual void handlePreWrite(uint32_t *byteOffset
);
259 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
261 int8_t fTableType
; // determined by table_write16() for table_preWrite() & table_write()
265 class ArrayResource
: public ContainerResource
{
267 ArrayResource(SRBRoot
*bundle
, const char *tag
,
268 const UString
* comment
, UErrorCode
&errorCode
)
269 : ContainerResource(bundle
, tag
, URES_ARRAY
, comment
, errorCode
),
271 virtual ~ArrayResource();
273 void add(SResource
*res
);
275 virtual void handleWrite16(SRBRoot
*bundle
);
276 virtual void handlePreWrite(uint32_t *byteOffset
);
277 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
283 * List of resources for a pool bundle.
284 * Writes an empty table resource, rather than a container structure.
286 class PseudoListResource
: public ContainerResource
{
288 PseudoListResource(SRBRoot
*bundle
, UErrorCode
&errorCode
)
289 : ContainerResource(bundle
, NULL
, URES_TABLE
, NULL
, errorCode
) {}
290 virtual ~PseudoListResource();
292 void add(SResource
*res
);
294 virtual void handleWrite16(SRBRoot
*bundle
);
297 class StringBaseResource
: public SResource
{
299 StringBaseResource(SRBRoot
*bundle
, const char *tag
, int8_t type
,
300 const UChar
*value
, int32_t len
,
301 const UString
* comment
, UErrorCode
&errorCode
);
302 StringBaseResource(SRBRoot
*bundle
, int8_t type
,
303 const icu::UnicodeString
&value
, UErrorCode
&errorCode
);
304 StringBaseResource(int8_t type
, const UChar
*value
, int32_t len
, UErrorCode
&errorCode
);
305 virtual ~StringBaseResource();
307 const UChar
*getBuffer() const { return icu::toUCharPtr(fString
.getBuffer()); }
308 int32_t length() const { return fString
.length(); }
310 virtual void handlePreWrite(uint32_t *byteOffset
);
311 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
313 // TODO: private with getter?
314 icu::UnicodeString fString
;
317 class StringResource
: public StringBaseResource
{
319 StringResource(SRBRoot
*bundle
, const char *tag
, const UChar
*value
, int32_t len
,
320 const UString
* comment
, UErrorCode
&errorCode
)
321 : StringBaseResource(bundle
, tag
, URES_STRING
, value
, len
, comment
, errorCode
),
322 fSame(NULL
), fSuffixOffset(0),
323 fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(0) {}
324 StringResource(SRBRoot
*bundle
, const icu::UnicodeString
&value
, UErrorCode
&errorCode
)
325 : StringBaseResource(bundle
, URES_STRING
, value
, errorCode
),
326 fSame(NULL
), fSuffixOffset(0),
327 fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(0) {}
328 StringResource(int32_t poolStringIndex
, int8_t numCharsForLength
,
329 const UChar
*value
, int32_t length
,
330 UErrorCode
&errorCode
)
331 : StringBaseResource(URES_STRING
, value
, length
, errorCode
),
332 fSame(NULL
), fSuffixOffset(0),
333 fNumCopies(0), fNumUnitsSaved(0), fNumCharsForLength(numCharsForLength
) {
334 // v3 pool string encoded as string-v2 with low offset
335 fRes
= URES_MAKE_RESOURCE(URES_STRING_V2
, poolStringIndex
);
338 virtual ~StringResource();
340 int32_t get16BitStringsLength() const {
341 return fNumCharsForLength
+ length() + 1; // +1 for the NUL
344 virtual void handlePreflightStrings(SRBRoot
*bundle
, UHashtable
*stringSet
, UErrorCode
&errorCode
);
345 virtual void handleWrite16(SRBRoot
*bundle
);
347 void writeUTF16v2(int32_t base
, icu::UnicodeString
&dest
);
349 StringResource
*fSame
; // used for duplicates
350 int32_t fSuffixOffset
; // this string is a suffix of fSame at this offset
351 int32_t fNumCopies
; // number of equal strings represented by one stringSet element
352 int32_t fNumUnitsSaved
; // from not writing duplicates and suffixes
353 int8_t fNumCharsForLength
;
356 class AliasResource
: public StringBaseResource
{
358 AliasResource(SRBRoot
*bundle
, const char *tag
, const UChar
*value
, int32_t len
,
359 const UString
* comment
, UErrorCode
&errorCode
)
360 : StringBaseResource(bundle
, tag
, URES_ALIAS
, value
, len
, comment
, errorCode
) {}
361 virtual ~AliasResource();
364 class IntResource
: public SResource
{
366 IntResource(SRBRoot
*bundle
, const char *tag
, int32_t value
,
367 const UString
* comment
, UErrorCode
&errorCode
);
368 virtual ~IntResource();
370 // TODO: private with getter?
374 class IntVectorResource
: public SResource
{
376 IntVectorResource(SRBRoot
*bundle
, const char *tag
,
377 const UString
* comment
, UErrorCode
&errorCode
);
378 virtual ~IntVectorResource();
380 void add(int32_t value
, UErrorCode
&errorCode
);
382 virtual void handlePreWrite(uint32_t *byteOffset
);
383 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
390 class BinaryResource
: public SResource
{
392 BinaryResource(SRBRoot
*bundle
, const char *tag
,
393 uint32_t length
, uint8_t *data
, const char* fileName
,
394 const UString
* comment
, UErrorCode
&errorCode
);
395 virtual ~BinaryResource();
397 virtual void handlePreWrite(uint32_t *byteOffset
);
398 virtual void handleWrite(UNewDataMemory
*mem
, uint32_t *byteOffset
);
404 char* fFileName
; // file name for binary or import binary tags if any
407 // TODO: use LocalPointer or delete
408 void res_close(struct SResource
*res
);
410 void setIncludeCopyright(UBool val
);
411 UBool
getIncludeCopyright(void);
413 void setFormatVersion(int32_t formatVersion
);
415 int32_t getFormatVersion();
417 void setUsePoolBundle(UBool use
);
420 uint32_t computeCRC(const char *ptr
, uint32_t len
, uint32_t lastcrc
);
423 #endif /* #ifndef RESLIST_H */