]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/datamap.cpp
1 /********************************************************************
3 * Copyright (c) 2002-2004, 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"
15 DataMap::utoi(const UnicodeString
&s
) const
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 */
27 deleteResBund(void *obj
) {
28 delete (ResourceBundle
*)obj
;
33 RBDataMap::~RBDataMap()
38 RBDataMap::RBDataMap()
40 UErrorCode status
= U_ZERO_ERROR
;
41 fData
= new Hashtable(TRUE
, status
);
42 fData
->setValueDeleter(deleteResBund
);
45 // init from table resource
46 // will put stuff in hashtable according to
48 RBDataMap::RBDataMap(UResourceBundle
*data
, UErrorCode
&status
)
50 fData
= new Hashtable(TRUE
, status
);
51 fData
->setValueDeleter(deleteResBund
);
55 // init from headers and resource
56 // with checking the whether the size of resource matches
58 RBDataMap::RBDataMap(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
60 fData
= new Hashtable(TRUE
, status
);
61 fData
->setValueDeleter(deleteResBund
);
62 init(headers
, data
, status
);
66 void RBDataMap::init(UResourceBundle
*data
, UErrorCode
&status
) {
69 UResourceBundle
*t
= NULL
;
70 for(i
= 0; i
< ures_getSize(data
); i
++) {
71 t
= ures_getByIndex(data
, i
, t
, &status
);
72 fData
->put(UnicodeString(ures_getKey(t
), ""), new ResourceBundle(t
, status
), status
);
77 void RBDataMap::init(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
81 UResourceBundle
*t
= NULL
;
82 const UChar
*key
= NULL
;
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
);
92 status
= U_INVALID_FORMAT_ERROR
;
97 const ResourceBundle
*RBDataMap::getItem(const char* key
, UErrorCode
&status
) const
99 if(U_FAILURE(status
)) {
103 UnicodeString
hashKey(key
, "");
104 const ResourceBundle
*r
= (ResourceBundle
*)fData
->get(hashKey
);
108 status
= U_MISSING_RESOURCE_ERROR
;
113 const UnicodeString
RBDataMap::getString(const char* key
, UErrorCode
&status
) const
115 const ResourceBundle
*r
= getItem(key
, status
);
116 if(U_SUCCESS(status
)) {
117 return r
->getString(status
);
119 status
= U_MISSING_RESOURCE_ERROR
;
120 return UnicodeString();
125 RBDataMap::getInt28(const char* key
, UErrorCode
&status
) const
127 const ResourceBundle
*r
= getItem(key
, status
);
128 if(U_SUCCESS(status
)) {
129 return r
->getInt(status
);
136 RBDataMap::getUInt28(const char* key
, UErrorCode
&status
) const
138 const ResourceBundle
*r
= getItem(key
, status
);
139 if(U_SUCCESS(status
)) {
140 return r
->getUInt(status
);
147 RBDataMap::getIntVector(int32_t &length
, const char *key
, UErrorCode
&status
) const {
148 const ResourceBundle
*r
= getItem(key
, status
);
149 if(U_SUCCESS(status
)) {
150 return r
->getIntVector(length
, status
);
157 RBDataMap::getBinary(int32_t &length
, const char *key
, UErrorCode
&status
) const {
158 const ResourceBundle
*r
= getItem(key
, status
);
159 if(U_SUCCESS(status
)) {
160 return r
->getBinary(length
, status
);
166 int32_t RBDataMap::getInt(const char* key
, UErrorCode
&status
) const
168 UnicodeString r
= this->getString(key
, status
);
169 if(U_SUCCESS(status
)) {
176 const UnicodeString
* RBDataMap::getStringArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
178 const ResourceBundle
*r
= getItem(key
, status
);
179 if(U_SUCCESS(status
)) {
182 count
= r
->getSize();
187 UnicodeString
*result
= new UnicodeString
[count
];
188 for(i
= 0; i
<count
; i
++) {
189 result
[i
] = r
->getStringEx(i
, status
);
193 status
= U_MISSING_RESOURCE_ERROR
;
198 const int32_t* RBDataMap::getIntArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
200 const ResourceBundle
*r
= getItem(key
, status
);
201 if(U_SUCCESS(status
)) {
204 count
= r
->getSize();
209 int32_t *result
= new int32_t[count
];
210 UnicodeString stringRes
;
211 for(i
= 0; i
<count
; i
++) {
212 stringRes
= r
->getStringEx(i
, status
);
213 result
[i
] = utoi(stringRes
);
217 status
= U_MISSING_RESOURCE_ERROR
;