]>
Commit | Line | Data |
---|---|---|
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 | ||
46f4442e A |
14 | DataMap::~DataMap() {} |
15 | DataMap::DataMap() {} | |
16 | ||
b75a7d8f A |
17 | int32_t |
18 | DataMap::utoi(const UnicodeString &s) const | |
19 | { | |
20 | char ch[256]; | |
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 */ | |
25 | return atoi(ch); | |
26 | } | |
27 | ||
28 | U_CDECL_BEGIN | |
29 | void U_CALLCONV | |
30 | deleteResBund(void *obj) { | |
31 | delete (ResourceBundle *)obj; | |
32 | } | |
33 | U_CDECL_END | |
34 | ||
35 | ||
36 | RBDataMap::~RBDataMap() | |
37 | { | |
38 | delete fData; | |
39 | } | |
40 | ||
41 | RBDataMap::RBDataMap() | |
42 | { | |
43 | UErrorCode status = U_ZERO_ERROR; | |
44 | fData = new Hashtable(TRUE, status); | |
45 | fData->setValueDeleter(deleteResBund); | |
46 | } | |
47 | ||
48 | // init from table resource | |
49 | // will put stuff in hashtable according to | |
50 | // keys. | |
51 | RBDataMap::RBDataMap(UResourceBundle *data, UErrorCode &status) | |
52 | { | |
53 | fData = new Hashtable(TRUE, status); | |
54 | fData->setValueDeleter(deleteResBund); | |
55 | init(data, status); | |
56 | } | |
57 | ||
58 | // init from headers and resource | |
59 | // with checking the whether the size of resource matches | |
60 | // header size | |
61 | RBDataMap::RBDataMap(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status) | |
62 | { | |
63 | fData = new Hashtable(TRUE, status); | |
64 | fData->setValueDeleter(deleteResBund); | |
65 | init(headers, data, status); | |
66 | } | |
67 | ||
68 | ||
69 | void RBDataMap::init(UResourceBundle *data, UErrorCode &status) { | |
70 | int32_t i = 0; | |
71 | fData->removeAll(); | |
72 | UResourceBundle *t = NULL; | |
73 | for(i = 0; i < ures_getSize(data); i++) { | |
74 | t = ures_getByIndex(data, i, t, &status); | |
73c04bcf | 75 | fData->put(UnicodeString(ures_getKey(t), -1, US_INV), new ResourceBundle(t, status), status); |
b75a7d8f A |
76 | } |
77 | ures_close(t); | |
78 | } | |
79 | ||
80 | void RBDataMap::init(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status) | |
81 | { | |
82 | int32_t i = 0; | |
83 | fData->removeAll(); | |
84 | UResourceBundle *t = NULL; | |
85 | const UChar *key = NULL; | |
86 | int32_t keyLen = 0; | |
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); | |
92 | } | |
93 | } else { | |
94 | // error | |
95 | status = U_INVALID_FORMAT_ERROR; | |
96 | } | |
97 | ures_close(t); | |
98 | } | |
99 | ||
374ca955 | 100 | const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const |
b75a7d8f | 101 | { |
374ca955 A |
102 | if(U_FAILURE(status)) { |
103 | return NULL; | |
104 | } | |
105 | ||
73c04bcf | 106 | UnicodeString hashKey(key, -1, US_INV); |
374ca955 | 107 | const ResourceBundle *r = (ResourceBundle *)fData->get(hashKey); |
b75a7d8f | 108 | if(r != NULL) { |
374ca955 A |
109 | return r; |
110 | } else { | |
111 | status = U_MISSING_RESOURCE_ERROR; | |
112 | return NULL; | |
113 | } | |
114 | } | |
115 | ||
116 | const UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const | |
117 | { | |
118 | const ResourceBundle *r = getItem(key, status); | |
119 | if(U_SUCCESS(status)) { | |
b75a7d8f A |
120 | return r->getString(status); |
121 | } else { | |
374ca955 A |
122 | return UnicodeString(); |
123 | } | |
124 | } | |
125 | ||
126 | int32_t | |
127 | RBDataMap::getInt28(const char* key, UErrorCode &status) const | |
128 | { | |
129 | const ResourceBundle *r = getItem(key, status); | |
130 | if(U_SUCCESS(status)) { | |
131 | return r->getInt(status); | |
132 | } else { | |
133 | return 0; | |
134 | } | |
135 | } | |
136 | ||
137 | uint32_t | |
138 | RBDataMap::getUInt28(const char* key, UErrorCode &status) const | |
139 | { | |
140 | const ResourceBundle *r = getItem(key, status); | |
141 | if(U_SUCCESS(status)) { | |
142 | return r->getUInt(status); | |
143 | } else { | |
144 | return 0; | |
b75a7d8f A |
145 | } |
146 | } | |
147 | ||
374ca955 A |
148 | const int32_t * |
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); | |
153 | } else { | |
154 | return NULL; | |
155 | } | |
156 | } | |
157 | ||
158 | const uint8_t * | |
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); | |
163 | } else { | |
164 | return NULL; | |
165 | } | |
166 | } | |
b75a7d8f A |
167 | |
168 | int32_t RBDataMap::getInt(const char* key, UErrorCode &status) const | |
169 | { | |
b75a7d8f A |
170 | UnicodeString r = this->getString(key, status); |
171 | if(U_SUCCESS(status)) { | |
374ca955 A |
172 | return utoi(r); |
173 | } else { | |
174 | return 0; | |
b75a7d8f | 175 | } |
b75a7d8f A |
176 | } |
177 | ||
178 | const UnicodeString* RBDataMap::getStringArray(int32_t& count, const char* key, UErrorCode &status) const | |
179 | { | |
374ca955 A |
180 | const ResourceBundle *r = getItem(key, status); |
181 | if(U_SUCCESS(status)) { | |
b75a7d8f A |
182 | int32_t i = 0; |
183 | ||
184 | count = r->getSize(); | |
374ca955 A |
185 | if(count <= 0) { |
186 | return NULL; | |
187 | } | |
188 | ||
b75a7d8f A |
189 | UnicodeString *result = new UnicodeString[count]; |
190 | for(i = 0; i<count; i++) { | |
191 | result[i] = r->getStringEx(i, status); | |
192 | } | |
193 | return result; | |
194 | } else { | |
b75a7d8f A |
195 | return NULL; |
196 | } | |
197 | } | |
198 | ||
199 | const int32_t* RBDataMap::getIntArray(int32_t& count, const char* key, UErrorCode &status) const | |
200 | { | |
374ca955 A |
201 | const ResourceBundle *r = getItem(key, status); |
202 | if(U_SUCCESS(status)) { | |
b75a7d8f A |
203 | int32_t i = 0; |
204 | ||
205 | count = r->getSize(); | |
374ca955 A |
206 | if(count <= 0) { |
207 | return NULL; | |
208 | } | |
209 | ||
b75a7d8f A |
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); | |
215 | } | |
216 | return result; | |
217 | } else { | |
b75a7d8f A |
218 | return NULL; |
219 | } | |
220 | } | |
73c04bcf | 221 |