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