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