#include "unicode/utypes.h"
#include "cstring.h"
#include "unicode/udata.h"
#include "uresdata.h"
#include "uresimp.h"
#define RES_GET_KEY(pRoot, keyOffset) ((const char *)(pRoot)+(keyOffset))
#define URESDATA_ITEM_NOT_FOUND 0xFFFF
static Resource
_res_getArrayItem(Resource *pRoot, Resource res, int32_t indexR) {
int32_t *p=(int32_t *)RES_GET_POINTER(pRoot, res);
if(indexR<*p) {
return ((Resource *)(p))[1+indexR];
} else {
return RES_BOGUS;
}
}
static const char *
_res_getTableKey(const Resource *pRoot, const Resource res, uint16_t indexS) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
if(indexS<*p) {
return RES_GET_KEY(pRoot, p[indexS+1]);
} else {
return NULL;
}
}
static Resource
_res_getTableItem(const Resource *pRoot, const Resource res, uint16_t indexR) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
uint16_t count=*p;
if(indexR<count) {
return ((Resource *)(p+1+count+(~count&1)))[indexR];
} else {
return RES_BOGUS;
}
}
static Resource
_res_findTableItem(const Resource *pRoot, const Resource res, const char *key) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
uint16_t i, start, limit;
limit=*p++;
if(limit == 0) {
return RES_BOGUS;
}
start=0;
while(start<limit-1) {
i=(uint16_t)((start+limit)/2);
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
limit=i;
} else {
start=i;
}
}
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
limit=*(p-1);
return ((Resource *)(p+limit+(~limit&1)))[start];
} else {
return RES_BOGUS;
}
}
static Resource
_res_findTableItemN(const Resource *pRoot, const Resource res, const char *key, int32_t keyLen, const char **realKey) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
uint16_t i, start, limit;
limit=*p++;
if(limit == 0) {
return RES_BOGUS;
}
start=0;
while(start<limit-1) {
i=(uint16_t)((start+limit)/2);
if(uprv_strncmp(key, RES_GET_KEY(pRoot, p[i]), keyLen)<0) {
limit=i;
} else {
start=i;
}
}
if(uprv_strncmp(key, RES_GET_KEY(pRoot, p[start]), keyLen)==0) {
*realKey = RES_GET_KEY(pRoot, p[start]);
limit=*(p-1);
return ((Resource *)(p+limit+(~limit&1)))[start];
} else {
*realKey = NULL;
return RES_BOGUS;
}
}
static uint16_t
_res_findTableIndex(const Resource *pRoot, const Resource res, const char *key) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res);
uint16_t i, start, limit;
limit=*p++;
if(limit == 0) {
return URESDATA_ITEM_NOT_FOUND;
}
start=0;
while(start<limit-1) {
i=(uint16_t)((start+limit)/2);
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[i]))<0) {
limit=i;
} else {
start=i;
}
}
if(uprv_strcmp(key, RES_GET_KEY(pRoot, p[start]))==0) {
limit=*(p-1);
return start;
} else {
return URESDATA_ITEM_NOT_FOUND;
}
}
static UBool U_CALLCONV
isAcceptable(void *context,
const char *type, const char *name,
const UDataInfo *pInfo) {
return (UBool)(
pInfo->size>=20 &&
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
pInfo->charsetFamily==U_CHARSET_FAMILY &&
pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
pInfo->dataFormat[0]==0x52 &&
pInfo->dataFormat[1]==0x65 &&
pInfo->dataFormat[2]==0x73 &&
pInfo->dataFormat[3]==0x42 &&
pInfo->formatVersion[0]==1);
}
U_CFUNC UBool
res_load(ResourceData *pResData,
const char *path, const char *name, UErrorCode *errorCode) {
pResData->data=udata_openChoice(path, "res", name, isAcceptable, NULL, errorCode);
if(U_FAILURE(*errorCode)) {
return FALSE;
}
pResData->pRoot=(Resource *)udata_getMemory(pResData->data);
pResData->rootRes=*pResData->pRoot;
if(RES_GET_TYPE(pResData->rootRes)!=URES_TABLE) {
udata_close(pResData->data);
pResData->data=NULL;
return FALSE;
}
return TRUE;
}
U_CFUNC void
res_unload(ResourceData *pResData) {
if(pResData->data!=NULL) {
udata_close(pResData->data);
pResData->data=NULL;
}
}
U_CFUNC const UChar *
res_getString(const ResourceData *pResData, const Resource res, int32_t *pLength) {
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_STRING) {
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
if (pLength) {
*pLength=*p;
}
return (UChar *)++p;
} else {
if (pLength) {
*pLength=0;
}
return NULL;
}
}
U_CFUNC const UChar *
res_getAlias(const ResourceData *pResData, const Resource res, int32_t *pLength) {
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_ALIAS) {
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
if (pLength) {
*pLength=*p;
}
return (UChar *)++p;
} else {
if (pLength) {
*pLength=0;
}
return NULL;
}
}
U_CFUNC const uint8_t *
res_getBinary(const ResourceData *pResData, const Resource res, int32_t *pLength) {
if(res!=RES_BOGUS) {
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
*pLength=*p++;
if (*pLength == 0) {
p = NULL;
}
return (uint8_t *)p;
} else {
*pLength=0;
return NULL;
}
}
U_CFUNC const int32_t *
res_getIntVector(const ResourceData *pResData, const Resource res, int32_t *pLength) {
if(res!=RES_BOGUS && RES_GET_TYPE(res)==URES_INT_VECTOR) {
int32_t *p=(int32_t *)RES_GET_POINTER(pResData->pRoot, res);
*pLength=*p++;
if (*pLength == 0) {
p = NULL;
}
return (const int32_t *)p;
} else {
*pLength=0;
return NULL;
}
}
U_CFUNC int32_t
res_countArrayItems(const ResourceData *pResData, const Resource res) {
if(res!=RES_BOGUS) {
if(RES_GET_TYPE(res)==URES_STRING) {
return 1;
} else if(RES_GET_TYPE(res)==URES_ARRAY) {
Resource *p=RES_GET_POINTER(pResData->pRoot, res);
int32_t count=*(int32_t *)p;
return count;
} else if(RES_GET_TYPE(res)==URES_TABLE) {
return res_getTableSize(pResData, res);
}
}
return 0;
}
U_CFUNC Resource
res_getResource(const ResourceData *pResData, const char *key) {
return _res_findTableItem(pResData->pRoot, pResData->rootRes, key);
}
U_CFUNC Resource
res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexR) {
return _res_getArrayItem(pResData->pRoot, array, indexR);
}
U_CFUNC Resource
res_findResource(const ResourceData *pResData, Resource r, const char** path, const char** key) {
const char *pathP = *path, *nextSepP = *path;
char *closeIndex = NULL;
Resource t1 = r;
Resource t2;
int32_t indexR = 0, keyLen = 0;
UResType type = RES_GET_TYPE(t1);
while(nextSepP && *pathP && t1 != RES_BOGUS && (type == URES_TABLE || type == URES_ARRAY)) {
nextSepP = uprv_strchr(pathP, RES_PATH_SEPARATOR);
if(nextSepP != NULL) {
keyLen = nextSepP-pathP;
*path = nextSepP+1;
} else {
keyLen = uprv_strlen(pathP);
*path += keyLen;
}
if(type == URES_TABLE) {
t2 = _res_findTableItemN(pResData->pRoot, t1, pathP, keyLen, key);
if(t2 == RES_BOGUS) {
indexR = uprv_strtol(pathP, &closeIndex, 10);
if(closeIndex != pathP) {
t2 = res_getTableItemByIndex(pResData, t1, indexR, key);
}
}
} else if(type == URES_ARRAY) {
indexR = uprv_strtol(pathP, &closeIndex, 10);
if(closeIndex != pathP) {
t2 = _res_getArrayItem(pResData->pRoot, t1, indexR);
} else {
t2 = RES_BOGUS;
}
*key = NULL;
} else {
t2 = RES_BOGUS;
}
t1 = t2;
type = RES_GET_TYPE(t1);
pathP += keyLen+1;
}
return t1;
}
U_CFUNC Resource
res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t* indexR, const char* * key) {
uint16_t tempIndex;
if(key != NULL) {
tempIndex = _res_findTableIndex(pResData->pRoot, table, *key);
if(tempIndex != URESDATA_ITEM_NOT_FOUND) {
*key = _res_getTableKey(pResData->pRoot, table, tempIndex);
*indexR = tempIndex;
return _res_getTableItem(pResData->pRoot, table, tempIndex);
} else {
return RES_BOGUS;
}
} else {
return RES_BOGUS;
}
}
U_CFUNC Resource
res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexR, const char * * key) {
if(indexR>-1) {
if(key != NULL) {
*key = _res_getTableKey(pResData->pRoot, table, (uint16_t)indexR);
}
return _res_getTableItem(pResData->pRoot, table, (uint16_t)indexR);
} else {
return RES_BOGUS;
}
}
U_CFUNC int32_t
res_getTableSize(const ResourceData *pResData, Resource table) {
uint16_t *p=(uint16_t *)RES_GET_POINTER(pResData->pRoot, table);
return *p;
}