]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/common/uresdata.h
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / common / uresdata.h
... / ...
CommitLineData
1/*
2******************************************************************************
3* *
4* Copyright (C) 1999-2006, International Business Machines *
5* Corporation and others. All Rights Reserved. *
6* *
7******************************************************************************
8* file name: uresdata.h
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 1999dec08
14* created by: Markus W. Scherer
15* 06/24/02 weiv Added support for resource sharing
16*/
17
18#ifndef __RESDATA_H__
19#define __RESDATA_H__
20
21#include "unicode/utypes.h"
22#include "unicode/udata.h"
23#include "udataswp.h"
24
25/*
26 * A Resource is a 32-bit value that has 2 bit fields:
27 * 31..28 4-bit type, see enum below
28 * 27..0 28-bit four-byte-offset or value according to the type
29 */
30typedef uint32_t Resource;
31
32#define RES_BOGUS 0xffffffff
33
34#define RES_GET_TYPE(res) ((UResType)((res)>>28UL))
35#define RES_GET_OFFSET(res) ((res)&0x0fffffff)
36#define RES_GET_POINTER(pRoot, res) ((pRoot)+RES_GET_OFFSET(res))
37
38/* get signed and unsigned integer values directly from the Resource handle */
39#define RES_GET_INT(res) (((int32_t)((res)<<4L))>>4L)
40#define RES_GET_UINT(res) ((res)&0x0fffffff)
41
42/* indexes[] value names; indexes are generally 32-bit (Resource) indexes */
43enum {
44 URES_INDEX_LENGTH, /* [0] contains URES_INDEX_TOP==the length of indexes[] */
45 URES_INDEX_STRINGS_TOP, /* [1] contains the top of the strings, */
46 /* same as the bottom of resources, rounded up */
47 URES_INDEX_RESOURCES_TOP, /* [2] contains the top of all resources */
48 URES_INDEX_BUNDLE_TOP, /* [3] contains the top of the bundle, */
49 /* in case it were ever different from [2] */
50 URES_INDEX_MAX_TABLE_LENGTH,/* [4] max. length of any table */
51 URES_INDEX_ATTRIBUTES, /* [5] attributes bit set, see URES_ATT_* (new in formatVersion 1.2) */
52 URES_INDEX_TOP
53};
54
55/* number of bytes at the beginning of the bundle before the strings start */
56enum {
57 URES_STRINGS_BOTTOM=(1+URES_INDEX_TOP)*4
58};
59
60/*
61 * Nofallback attribute, attribute bit 0 in indexes[URES_INDEX_ATTRIBUTES].
62 * New in formatVersion 1.2 (ICU 3.6).
63 *
64 * If set, then this resource bundle is a standalone bundle.
65 * If not set, then the bundle participates in locale fallback, eventually
66 * all the way to the root bundle.
67 * If indexes[] is missing or too short, then the attribute cannot be determined
68 * reliably. Dependency checking should ignore such bundles, and loading should
69 * use fallbacks.
70 */
71#define URES_ATT_NO_FALLBACK 1
72
73/*
74 * File format for .res resource bundle files (formatVersion=1.2)
75 *
76 * An ICU4C resource bundle file (.res) is a binary, memory-mappable file
77 * with nested, hierarchical data structures.
78 * It physically contains the following:
79 *
80 * Resource root; -- 32-bit Resource item, root item for this bundle's tree;
81 * currently, the root item must be a table or table32 resource item
82 * int32_t indexes[indexes[0]]; -- array of indexes for friendly
83 * reading and swapping; see URES_INDEX_* above
84 * new in formatVersion 1.1 (ICU 2.8)
85 * char keys[]; -- characters for key strings
86 * (formatVersion 1.0: up to 65k of characters; 1.1: <2G)
87 * (minus the space for root and indexes[]),
88 * which consist of invariant characters (ASCII/EBCDIC) and are NUL-terminated;
89 * padded to multiple of 4 bytes for 4-alignment of the following data
90 * data; -- data directly and indirectly indexed by the root item;
91 * the structure is determined by walking the tree
92 *
93 * Each resource bundle item has a 32-bit Resource handle (see typedef above)
94 * which contains the item type number in its upper 4 bits (31..28) and either
95 * an offset or a direct value in its lower 28 bits (27..0).
96 * The order of items is undefined and only determined by walking the tree.
97 * Leaves of the tree may be stored first or last or anywhere in between,
98 * and it is in theory possible to have unreferenced holes in the file.
99 *
100 * Direct values:
101 * - Empty Unicode strings have an offset value of 0 in the Resource handle itself.
102 * - Integer values are 28-bit values stored in the Resource handle itself;
103 * the interpretation of unsigned vs. signed integers is up to the application.
104 *
105 * All other types and values use 28-bit offsets to point to the item's data.
106 * The offset is an index to the first 32-bit word of the value, relative to the
107 * start of the resource data (i.e., the root item handle is at offset 0).
108 * To get byte offsets, the offset is multiplied by 4 (or shifted left by 2 bits).
109 * All resource item values are 4-aligned.
110 *
111 * The structures (memory layouts) for the values for each item type are listed
112 * in the table above.
113 *
114 * Nested, hierarchical structures: -------------
115 *
116 * Table items contain key-value pairs where the keys are 16-bit offsets to char * key strings.
117 * Key string offsets are also relative to the start of the resource data (of the root handle),
118 * i.e., the first string has an offset of 4 (after the 4-byte root handle).
119 *
120 * The values of these pairs are Resource handles.
121 *
122 * Array items are simple vectors of Resource handles.
123 *
124 * An alias item is special (and new in ICU 2.4): --------------
125 *
126 * Its memory layout is just like for a UnicodeString, but at runtime it resolves to
127 * another resource bundle's item according to the path in the string.
128 * This is used to share items across bundles that are in different lookup/fallback
129 * chains (e.g., large collation data among zh_TW and zh_HK).
130 * This saves space (for large items) and maintenance effort (less duplication of data).
131 *
132 * --------------------------------------------------------------------------
133 *
134 * Resource types:
135 *
136 * Most resources have their values stored at four-byte offsets from the start
137 * of the resource data. These values are at least 4-aligned.
138 * Some resource values are stored directly in the offset field of the Resource itself.
139 * See UResType in unicode/ures.h for enumeration constants for Resource types.
140 *
141 * Type Name Memory layout of values
142 * (in parentheses: scalar, non-offset values)
143 *
144 * 0 Unicode String: int32_t length, UChar[length], (UChar)0, (padding)
145 * or (empty string ("") if offset==0)
146 * 1 Binary: int32_t length, uint8_t[length], (padding)
147 * - this value should be 32-aligned -
148 * 2 Table: uint16_t count, uint16_t keyStringOffsets[count], (uint16_t padding), Resource[count]
149 * 3 Alias: (physically same value layout as string, new in ICU 2.4)
150 * 4 Table32: int32_t count, int32_t keyStringOffsets[count], Resource[count]
151 * (new in formatVersion 1.1/ICU 2.8)
152 *
153 * 7 Integer: (28-bit offset is integer value)
154 * 8 Array: int32_t count, Resource[count]
155 *
156 * 14 Integer Vector: int32_t length, int32_t[length]
157 * 15 Reserved: This value denotes special purpose resources and is for internal use.
158 *
159 * Note that there are 3 types with data vector values:
160 * - Vectors of 8-bit bytes stored as type Binary.
161 * - Vectors of 16-bit words stored as type Unicode String
162 * (no value restrictions, all values 0..ffff allowed!).
163 * - Vectors of 32-bit words stored as type Integer Vector.
164 */
165
166/*
167 * Structure for a single, memory-mapped ResourceBundle.
168 */
169typedef struct {
170 UDataMemory *data;
171 Resource *pRoot;
172 Resource rootRes;
173 UBool noFallback; /* see URES_ATT_NO_FALLBACK */
174} ResourceData;
175
176/*
177 * Load a resource bundle file.
178 * The ResourceData structure must be allocated externally.
179 */
180U_CFUNC UBool
181res_load(ResourceData *pResData,
182 const char *path, const char *name, UErrorCode *errorCode);
183
184/*
185 * Release a resource bundle file.
186 * This does not release the ResourceData structure itself.
187 */
188U_CFUNC void
189res_unload(ResourceData *pResData);
190
191/*
192 * Return a pointer to a zero-terminated, const UChar* string
193 * and set its length in *pLength.
194 * Returns NULL if not found.
195 */
196U_CFUNC const UChar *
197res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength);
198
199U_CFUNC const UChar *
200res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength);
201
202U_CFUNC const uint8_t *
203res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength);
204
205U_CFUNC const int32_t *
206res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength);
207
208U_CFUNC Resource
209res_getResource(const ResourceData *pResData, const char *key);
210
211U_CFUNC int32_t
212res_countArrayItems(const ResourceData *pResData, const Resource res);
213
214U_CFUNC Resource res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexS);
215U_CFUNC Resource res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key);
216U_CFUNC Resource res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key);
217
218/*
219 * Modifies the contents of *path (replacing separators with NULs),
220 * and also moves *path forward while it finds items.
221 */
222U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, char** path, const char** key);
223
224/**
225 * Swap an ICU resource bundle. See udataswp.h.
226 * @internal
227 */
228U_CAPI int32_t U_EXPORT2
229ures_swap(const UDataSwapper *ds,
230 const void *inData, int32_t length, void *outData,
231 UErrorCode *pErrorCode);
232
233#endif