1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2002-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7/* Created by weiv 05/09/2002 */
8
9#include "unicode/datamap.h"
10#include "unicode/resbund.h"
11#include "hash.h"
12#include <stdlib.h>
13
14DataMap::~DataMap() {}
15DataMap::DataMap() {}
16
17int32_t
18DataMap::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
28U_CDECL_BEGIN
29void U_CALLCONV
30deleteResBund(void *obj) {
31  delete (ResourceBundle *)obj;
32}
33U_CDECL_END
34
35
36RBDataMap::~RBDataMap()
37{
38  delete fData;
39}
40
41RBDataMap::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.
51RBDataMap::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
61RBDataMap::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
69void 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);
75    fData->put(UnicodeString(ures_getKey(t), -1, US_INV), new ResourceBundle(t, status), status);
76  }
77  ures_close(t);
78}
79
80void 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
100const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const
101{
102  if(U_FAILURE(status)) {
103    return NULL;
104  }
105
106  UnicodeString hashKey(key, -1, US_INV);
107  const ResourceBundle *r = (ResourceBundle *)fData->get(hashKey);
108  if(r != NULL) {
109    return r;
110  } else {
111    status = U_MISSING_RESOURCE_ERROR;
112    return NULL;
113  }
114}
115
116const UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const
117{
118  const ResourceBundle *r = getItem(key, status);
119  if(U_SUCCESS(status)) {
120    return r->getString(status);
121  } else {
122    return UnicodeString();
123  }
124}
125
126int32_t
127RBDataMap::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
137uint32_t
138RBDataMap::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;
145  }
146}
147
148const int32_t *
149RBDataMap::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
158const uint8_t *
159RBDataMap::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}
167
168int32_t RBDataMap::getInt(const char* key, UErrorCode &status) const
169{
170  UnicodeString r = this->getString(key, status);
171  if(U_SUCCESS(status)) {
172    return utoi(r);
173  } else {
174    return 0;
175  }
176}
177
178const UnicodeString* RBDataMap::getStringArray(int32_t& count, const char* key, UErrorCode &status) const
179{
180  const ResourceBundle *r = getItem(key, status);
181  if(U_SUCCESS(status)) {
182    int32_t i = 0;
183
184    count = r->getSize();
185    if(count <= 0) {
186      return NULL;
187    }
188
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 {
195    return NULL;
196  }
197}
198
199const int32_t* RBDataMap::getIntArray(int32_t& count, const char* key, UErrorCode &status) const
200{
201  const ResourceBundle *r = getItem(key, status);
202  if(U_SUCCESS(status)) {
203    int32_t i = 0;
204
205    count = r->getSize();
206    if(count <= 0) {
207      return NULL;
208    }
209
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 {
218    return NULL;
219  }
220}
221
222