]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/datamap.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2002-2006, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 /* Created by weiv 05/09/2002 */
11 #include "unicode/datamap.h"
12 #include "unicode/resbund.h"
13 #include "unicode/unistr.h"
17 DataMap::~DataMap() {}
21 DataMap::utoi(const UnicodeString
&s
) const
24 const UChar
*u
= toUCharPtr(s
.getBuffer());
25 int32_t len
= s
.length();
26 u_UCharsToChars(u
, ch
, len
);
27 ch
[len
] = 0; /* include terminating \0 */
33 deleteResBund(void *obj
) {
34 delete (ResourceBundle
*)obj
;
39 RBDataMap::~RBDataMap()
44 RBDataMap::RBDataMap()
46 UErrorCode status
= U_ZERO_ERROR
;
47 fData
= new Hashtable(TRUE
, status
);
48 fData
->setValueDeleter(deleteResBund
);
51 // init from table resource
52 // will put stuff in hashtable according to
54 RBDataMap::RBDataMap(UResourceBundle
*data
, UErrorCode
&status
)
56 fData
= new Hashtable(TRUE
, status
);
57 fData
->setValueDeleter(deleteResBund
);
61 // init from headers and resource
62 // with checking the whether the size of resource matches
64 RBDataMap::RBDataMap(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
66 fData
= new Hashtable(TRUE
, status
);
67 fData
->setValueDeleter(deleteResBund
);
68 init(headers
, data
, status
);
72 void RBDataMap::init(UResourceBundle
*data
, UErrorCode
&status
) {
75 UResourceBundle
*t
= NULL
;
76 for(i
= 0; i
< ures_getSize(data
); i
++) {
77 t
= ures_getByIndex(data
, i
, t
, &status
);
78 fData
->put(UnicodeString(ures_getKey(t
), -1, US_INV
), new ResourceBundle(t
, status
), status
);
83 void RBDataMap::init(UResourceBundle
*headers
, UResourceBundle
*data
, UErrorCode
&status
)
87 UResourceBundle
*t
= NULL
;
88 const UChar
*key
= NULL
;
90 if(ures_getSize(headers
) == ures_getSize(data
)) {
91 for(i
= 0; i
< ures_getSize(data
); i
++) {
92 t
= ures_getByIndex(data
, i
, t
, &status
);
93 key
= ures_getStringByIndex(headers
, i
, &keyLen
, &status
);
94 fData
->put(UnicodeString(key
, keyLen
), new ResourceBundle(t
, status
), status
);
98 status
= U_INVALID_FORMAT_ERROR
;
103 const ResourceBundle
*RBDataMap::getItem(const char* key
, UErrorCode
&status
) const
105 if(U_FAILURE(status
)) {
109 UnicodeString
hashKey(key
, -1, US_INV
);
110 const ResourceBundle
*r
= (ResourceBundle
*)fData
->get(hashKey
);
114 status
= U_MISSING_RESOURCE_ERROR
;
119 const UnicodeString
RBDataMap::getString(const char* key
, UErrorCode
&status
) const
121 const ResourceBundle
*r
= getItem(key
, status
);
122 if(U_SUCCESS(status
)) {
123 return r
->getString(status
);
125 return UnicodeString();
130 RBDataMap::getInt28(const char* key
, UErrorCode
&status
) const
132 const ResourceBundle
*r
= getItem(key
, status
);
133 if(U_SUCCESS(status
)) {
134 return r
->getInt(status
);
141 RBDataMap::getUInt28(const char* key
, UErrorCode
&status
) const
143 const ResourceBundle
*r
= getItem(key
, status
);
144 if(U_SUCCESS(status
)) {
145 return r
->getUInt(status
);
152 RBDataMap::getIntVector(int32_t &length
, const char *key
, UErrorCode
&status
) const {
153 const ResourceBundle
*r
= getItem(key
, status
);
154 if(U_SUCCESS(status
)) {
155 return r
->getIntVector(length
, status
);
162 RBDataMap::getBinary(int32_t &length
, const char *key
, UErrorCode
&status
) const {
163 const ResourceBundle
*r
= getItem(key
, status
);
164 if(U_SUCCESS(status
)) {
165 return r
->getBinary(length
, status
);
171 int32_t RBDataMap::getInt(const char* key
, UErrorCode
&status
) const
173 UnicodeString r
= this->getString(key
, status
);
174 if(U_SUCCESS(status
)) {
181 const UnicodeString
* RBDataMap::getStringArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
183 const ResourceBundle
*r
= getItem(key
, status
);
184 if(U_SUCCESS(status
)) {
187 count
= r
->getSize();
192 UnicodeString
*result
= new UnicodeString
[count
];
193 for(i
= 0; i
<count
; i
++) {
194 result
[i
] = r
->getStringEx(i
, status
);
202 const int32_t* RBDataMap::getIntArray(int32_t& count
, const char* key
, UErrorCode
&status
) const
204 const ResourceBundle
*r
= getItem(key
, status
);
205 if(U_SUCCESS(status
)) {
208 count
= r
->getSize();
213 int32_t *result
= new int32_t[count
];
214 UnicodeString stringRes
;
215 for(i
= 0; i
<count
; i
++) {
216 stringRes
= r
->getStringEx(i
, status
);
217 result
[i
] = utoi(stringRes
);