1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2002-2006, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/* Created by weiv 05/09/2002 */
#include "unicode/datamap.h"
#include "unicode/resbund.h"
#include "unicode/unistr.h"
#include "hash.h"
#include <stdlib.h>
DataMap::~DataMap() {}
DataMap::DataMap() {}
int32_t
DataMap::utoi(const UnicodeString &s) const
{
char ch[256];
const char16_t *u = toUCharPtr(s.getBuffer());
int32_t len = s.length();
u_UCharsToChars(u, ch, len);
ch[len] = 0; /* include terminating \0 */
return atoi(ch);
}
U_CDECL_BEGIN
void U_CALLCONV
deleteResBund(void *obj) {
delete (ResourceBundle *)obj;
}
U_CDECL_END
RBDataMap::~RBDataMap()
{
delete fData;
}
RBDataMap::RBDataMap()
{
UErrorCode status = U_ZERO_ERROR;
fData = new Hashtable(true, status);
fData->setValueDeleter(deleteResBund);
}
// init from table resource
// will put stuff in hashtable according to
// keys.
RBDataMap::RBDataMap(UResourceBundle *data, UErrorCode &status)
{
fData = new Hashtable(true, status);
fData->setValueDeleter(deleteResBund);
init(data, status);
}
// init from headers and resource
// with checking the whether the size of resource matches
// header size
RBDataMap::RBDataMap(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status)
{
fData = new Hashtable(true, status);
fData->setValueDeleter(deleteResBund);
init(headers, data, status);
}
void RBDataMap::init(UResourceBundle *data, UErrorCode &status) {
int32_t i = 0;
fData->removeAll();
UResourceBundle *t = nullptr;
for(i = 0; i < ures_getSize(data); i++) {
t = ures_getByIndex(data, i, t, &status);
fData->put(UnicodeString(ures_getKey(t), -1, US_INV), new ResourceBundle(t, status), status);
}
ures_close(t);
}
void RBDataMap::init(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status)
{
int32_t i = 0;
fData->removeAll();
UResourceBundle *t = nullptr;
const char16_t *key = nullptr;
int32_t keyLen = 0;
if(ures_getSize(headers) == ures_getSize(data)) {
for(i = 0; i < ures_getSize(data); i++) {
t = ures_getByIndex(data, i, t, &status);
key = ures_getStringByIndex(headers, i, &keyLen, &status);
fData->put(UnicodeString(key, keyLen), new ResourceBundle(t, status), status);
}
} else {
// error
status = U_INVALID_FORMAT_ERROR;
}
ures_close(t);
}
const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const
{
if(U_FAILURE(status)) {
return nullptr;
}
UnicodeString hashKey(key, -1, US_INV);
const ResourceBundle *r = (ResourceBundle *)fData->get(hashKey);
if(r != nullptr) {
return r;
} else {
status = U_MISSING_RESOURCE_ERROR;
return nullptr;
}
}
const UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
return r->getString(status);
} else {
return UnicodeString();
}
}
int32_t
RBDataMap::getInt28(const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
return r->getInt(status);
} else {
return 0;
}
}
uint32_t
RBDataMap::getUInt28(const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
return r->getUInt(status);
} else {
return 0;
}
}
const int32_t *
RBDataMap::getIntVector(int32_t &length, const char *key, UErrorCode &status) const {
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
return r->getIntVector(length, status);
} else {
return nullptr;
}
}
const uint8_t *
RBDataMap::getBinary(int32_t &length, const char *key, UErrorCode &status) const {
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
return r->getBinary(length, status);
} else {
return nullptr;
}
}
int32_t RBDataMap::getInt(const char* key, UErrorCode &status) const
{
UnicodeString r = this->getString(key, status);
if(U_SUCCESS(status)) {
return utoi(r);
} else {
return 0;
}
}
const UnicodeString* RBDataMap::getStringArray(int32_t& count, const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
int32_t i = 0;
count = r->getSize();
if(count <= 0) {
return nullptr;
}
UnicodeString *result = new UnicodeString[count];
for(i = 0; i<count; i++) {
result[i] = r->getStringEx(i, status);
}
return result;
} else {
return nullptr;
}
}
const int32_t* RBDataMap::getIntArray(int32_t& count, const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
int32_t i = 0;
count = r->getSize();
if(count <= 0) {
return nullptr;
}
int32_t *result = new int32_t[count];
UnicodeString stringRes;
for(i = 0; i<count; i++) {
stringRes = r->getStringEx(i, status);
result[i] = utoi(stringRes);
}
return result;
} else {
return nullptr;
}
}
|