]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/datamap.cpp
1 /********************************************************************
3 * Copyright (c) 2002-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 /* Created by weiv 05/09/2002 */
9 #include "unicode/datamap.h"
10 #include "unicode/resbund.h"
14 DataMap::~DataMap() {}
18 DataMap::utoi(const UnicodeString
&s
) const
21 const UChar
*u
= s
.getBuffer();
22 int32_t len
= s
.length();
23 u_UCharsToChars(u
, ch
, len
);
24 ch
[len
] = 0; /* include terminating \0 */
30 deleteResBund(void *obj
) {
31 delete (ResourceBundle
*)obj
;
36 RBDataMap::~RBDataMap()
41 RBDataMap::RBDataMap()
43 UErrorCode status
= U_ZERO_ERROR
;
44 fData
= new Hashtable(TRUE
, status
);
45 fData
->setValueDeleter(deleteResBund
);
48 // init from table resource
49 // will put stuff in hashtable according to
51 RBDataMap::RBDataMap(UResourceBundle
*data
, UErrorCode
&status
)
53 fData
= new Hashtable(TRUE
, status
);
54 fData
->setValueDeleter(deleteResBund
);
58 // init from headers and resource
59 // with checking the whether the size of resource matches
61 RBDataMap::RBDataMap(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
63 fData
= new Hashtable(TRUE
, status
);
64 fData
->setValueDeleter(deleteResBund
);
65 init(headers
, data
, status
);
69 void RBDataMap::init(UResourceBundle
*data
, UErrorCode
&status
) {
72 UResourceBundle
*t
= NULL
;
73 for(i
= 0; i
< ures_getSize(data
); i
++) {
74 t
= ures_getByIndex(data
, i
, t
, &status
);
75 fData
->put(UnicodeString(ures_getKey(t
), -1, US_INV
), new ResourceBundle(t
, status
), status
);
80 void RBDataMap::init(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
84 UResourceBundle
*t
= NULL
;
85 const UChar
*key
= NULL
;
87 if(ures_getSize(headers
) == ures_getSize(data
)) {
88 for(i
= 0; i
< ures_getSize(data
); i
++) {
89 t
= ures_getByIndex(data
, i
, t
, &status
);
90 key
= ures_getStringByIndex(headers
, i
, &keyLen
, &status
);
91 fData
->put(UnicodeString(key
, keyLen
), new ResourceBundle(t
, status
), status
);
95 status
= U_INVALID_FORMAT_ERROR
;
100 const ResourceBundle
*RBDataMap::getItem(const char* key
, UErrorCode
&status
) const
102 if(U_FAILURE(status
)) {
106 UnicodeString
hashKey(key
, -1, US_INV
);
107 const ResourceBundle
*r
= (ResourceBundle
*)fData
->get(hashKey
);
111 status
= U_MISSING_RESOURCE_ERROR
;
116 const UnicodeString
RBDataMap::getString(const char* key
, UErrorCode
&status
) const
118 const ResourceBundle
*r
= getItem(key
, status
);
119 if(U_SUCCESS(status
)) {
120 return r
->getString(status
);
122 return UnicodeString();
127 RBDataMap::getInt28(const char* key
, UErrorCode
&status
) const
129 const ResourceBundle
*r
= getItem(key
, status
);
130 if(U_SUCCESS(status
)) {
131 return r
->getInt(status
);
138 RBDataMap::getUInt28(const char* key
, UErrorCode
&status
) const
140 const ResourceBundle
*r
= getItem(key
, status
);
141 if(U_SUCCESS(status
)) {
142 return r
->getUInt(status
);
149 RBDataMap::getIntVector(int32_t &length
, const char *key
, UErrorCode
&status
) const {
150 const ResourceBundle
*r
= getItem(key
, status
);
151 if(U_SUCCESS(status
)) {
152 return r
->getIntVector(length
, status
);
159 RBDataMap::getBinary(int32_t &length
, const char *key
, UErrorCode
&status
) const {
160 const ResourceBundle
*r
= getItem(key
, status
);
161 if(U_SUCCESS(status
)) {
162 return r
->getBinary(length
, status
);
168 int32_t RBDataMap::getInt(const char* key
, UErrorCode
&status
) const
170 UnicodeString r
= this->getString(key
, status
);
171 if(U_SUCCESS(status
)) {
178 const UnicodeString
* RBDataMap::getStringArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
180 const ResourceBundle
*r
= getItem(key
, status
);
181 if(U_SUCCESS(status
)) {
184 count
= r
->getSize();
189 UnicodeString
*result
= new UnicodeString
[count
];
190 for(i
= 0; i
<count
; i
++) {
191 result
[i
] = r
->getStringEx(i
, status
);
199 const int32_t* RBDataMap::getIntArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
201 const ResourceBundle
*r
= getItem(key
, status
);
202 if(U_SUCCESS(status
)) {
205 count
= r
->getSize();
210 int32_t *result
= new int32_t[count
];
211 UnicodeString stringRes
;
212 for(i
= 0; i
<count
; i
++) {
213 stringRes
= r
->getStringEx(i
, status
);
214 result
[i
] = utoi(stringRes
);