2
*******************************************************************************
4
* Copyright (C) 1999-2001, International Business Machines Corporation *
5
* and others. All Rights Reserved. *
7
*******************************************************************************
8
* file name: uresdata.c
10
* tab size: 8 (not used)
13
* created on: 1999dec08
14
* created by: Markus W. Scherer
15
* Modification History:
17
* Date Name Description
18
* 06/20/2000 helena OS/400 port changes; mostly typecast.
21
#include "unicode/utypes.h"
23
#include "unicode/udata.h"
28
* Resource access helpers
31
/* get a const char* pointer to the key with the keyOffset byte offset from pRoot */
32
#define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
33
#define URESDATA_ITEM_NOT_FOUND 0xFFFF
36
* All the type-access functions assume that
37
* the resource is of the expected type.
45
_res_getArrayItem(Resource *pRoot, Resource res, int32_t indexR) {
46
int32_t *p=(int32_t *)RES_GET_POINTER(pRoot, res);
48
return ((Resource *)(p))[1+indexR];
50
return RES_BOGUS; /* indexR>itemCount */
57
* Important: the key offsets are 16-bit byte offsets from pRoot,
58
* and the itemCount is one more 16-bit, too.
59
* Thus, there are (count+1) uint16_t values.
60
* In order to 4-align the Resource item values, there is a padding
61
* word if count is even, i.e., there is exactly (~count&1)
62
* 16-bit padding words.
65
_res_getTableKey(const Resource *pRoot, const Resource res, uint16_t indexS) {
66
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
68
return RES_GET_KEY(pRoot, p[indexS+1]);
70
return NULL; /* indexS>itemCount */
75
_res_getTableItem(const Resource *pRoot, const Resource res, uint16_t indexR) {
76
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
79
return ((Resource *)(p+1+count+(~count&1)))[indexR];
81
return RES_BOGUS; /* indexR>itemCount */
86
_res_findTableItem(const Resource *pRoot, const Resource res, const char *key) {
87
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
88
uint16_t i, start, limit;
90
limit=*p++; /* number of entries */
92
if(limit == 0) { /* this table is empty */
96
/* do a binary search for the key */
98
while(start<limit-1) {
99
i=(uint16_t)((start+limit)/2);
100
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
107
/* did we really find it? */
108
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
109
limit=*(p-1); /* itemCount */
110
return ((Resource *)(p+limit+(~limit&1)))[start];
112
return RES_BOGUS; /* not found */
117
_res_findTableIndex(const Resource *pRoot, const Resource res, const char *key) {
118
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
119
uint16_t i, start, limit;
121
limit=*p++; /* number of entries */
123
if(limit == 0) { /* this table is empty */
124
return URESDATA_ITEM_NOT_FOUND;
127
/* do a binary search for the key */
129
while(start<limit-1) {
130
i=(uint16_t)((start+limit)/2);
131
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
138
/* did we really find it? */
139
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
140
limit=*(p-1); /* itemCount */
143
return URESDATA_ITEM_NOT_FOUND; /* not found */
147
/* helper for res_load() ---------------------------------------------------- */
150
isAcceptable(void *context,
151
const char *type, const char *name,
152
const UDataInfo *pInfo) {
155
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
156
pInfo->charsetFamily==U_CHARSET_FAMILY &&
157
pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
158
pInfo->dataFormat[0]==0x52 && /* dataFormat="ResB" */
159
pInfo->dataFormat[1]==0x65 &&
160
pInfo->dataFormat[2]==0x73 &&
161
pInfo->dataFormat[3]==0x42 &&
162
pInfo->formatVersion[0]==1);
165
/* semi-public functions ---------------------------------------------------- */
168
res_load(ResourceData *pResData,
169
const char *path, const char *name, UErrorCode *errorCode) {
170
/* load the ResourceBundle file */
171
pResData->data=udata_openChoice(path, "res", name, isAcceptable, NULL, errorCode);
172
if(U_FAILURE(*errorCode)) {
176
/* get its memory and root resource */
177
pResData->pRoot=(Resource *)udata_getMemory(pResData->data);
178
pResData->rootRes=*pResData->pRoot;
180
/* currently, we accept only resources that have a Table as their roots */
181
if(RES_GET_TYPE(pResData->rootRes)!=RES_TABLE) {
182
udata_close(pResData->data);
191
res_unload(ResourceData *pResData) {
192
if(pResData->data!=NULL) {
193
udata_close(pResData->data);
198
U_CFUNC const UChar *
199
res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength) {
200
if(res!=RES_BOGUS && RES_GET_TYPE(res)==RES_STRING) {
201
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
214
U_CFUNC const uint8_t *
215
res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength) {
217
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
230
U_CFUNC const int32_t *
231
res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength) {
232
if(res!=RES_BOGUS && RES_GET_TYPE(res)==RES_INT_VECTOR) {
233
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
238
return (const int32_t *)p;
246
res_countArrayItems(const ResourceData *pResData, const Resource res) {
248
if(RES_GET_TYPE(res)==RES_STRING) {
250
} else if(RES_GET_TYPE(res)==RES_ARRAY) {
251
Resource *p=RES_GET_POINTER(pResData->pRoot, res);
252
int32_t count=*(int32_t *)p;
254
} else if(RES_GET_TYPE(res)==RES_TABLE) {
255
return res_getTableSize(pResData, res);
262
res_getResource(const ResourceData *pResData, const char *key) {
263
return _res_findTableItem(pResData->pRoot, pResData->rootRes, key);
267
res_getArrayItem(const ResourceData *pResData, const Resource array, const int32_t indexR) {
268
return _res_getArrayItem(pResData->pRoot, array, indexR);
272
res_getTableItemByKey(const ResourceData *pResData, const Resource table, int32_t* indexR, const char* * key) {
275
tempIndex = _res_findTableIndex(pResData->pRoot, table, *key);
276
if(tempIndex != URESDATA_ITEM_NOT_FOUND) {
277
*key = _res_getTableKey(pResData->pRoot, table, tempIndex);
279
return _res_getTableItem(pResData->pRoot, table, tempIndex);
289
res_getTableItemByIndex(const ResourceData *pResData, const Resource table, int32_t indexR, const char * * key) {
292
*key = _res_getTableKey(pResData->pRoot, table, (uint16_t)indexR);
294
return _res_getTableItem(pResData->pRoot, table, (uint16_t)indexR);
301
res_getTableSize(const ResourceData *pResData, Resource table) {
302
uint16_t *p=(uint16_t *)RES_GET_POINTER(pResData->pRoot, table);