intl/icu/source/tools/ctestfw/datamap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/ctestfw/datamap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/********************************************************************
     1.5 + * COPYRIGHT: 
     1.6 + * Copyright (c) 2002-2006, International Business Machines Corporation and
     1.7 + * others. All Rights Reserved.
     1.8 + ********************************************************************/
     1.9 +
    1.10 +/* Created by weiv 05/09/2002 */
    1.11 +
    1.12 +#include "unicode/datamap.h"
    1.13 +#include "unicode/resbund.h"
    1.14 +#include "hash.h"
    1.15 +#include <stdlib.h>
    1.16 +
    1.17 +DataMap::~DataMap() {}
    1.18 +DataMap::DataMap() {}
    1.19 +
    1.20 +int32_t 
    1.21 +DataMap::utoi(const UnicodeString &s) const
    1.22 +{
    1.23 +  char ch[256];
    1.24 +  const UChar *u = s.getBuffer();
    1.25 +  int32_t len = s.length();
    1.26 +  u_UCharsToChars(u, ch, len);
    1.27 +  ch[len] = 0; /* include terminating \0 */
    1.28 +  return atoi(ch);
    1.29 +}
    1.30 +
    1.31 +U_CDECL_BEGIN
    1.32 +void U_CALLCONV
    1.33 +deleteResBund(void *obj) {
    1.34 +  delete (ResourceBundle *)obj;
    1.35 +}
    1.36 +U_CDECL_END
    1.37 +
    1.38 +
    1.39 +RBDataMap::~RBDataMap()
    1.40 +{
    1.41 +  delete fData;
    1.42 +}
    1.43 +
    1.44 +RBDataMap::RBDataMap()
    1.45 +{
    1.46 +  UErrorCode status = U_ZERO_ERROR;
    1.47 +  fData = new Hashtable(TRUE, status);
    1.48 +  fData->setValueDeleter(deleteResBund);
    1.49 +}
    1.50 +
    1.51 +// init from table resource
    1.52 +// will put stuff in hashtable according to 
    1.53 +// keys.
    1.54 +RBDataMap::RBDataMap(UResourceBundle *data, UErrorCode &status)
    1.55 +{
    1.56 +  fData = new Hashtable(TRUE, status);
    1.57 +  fData->setValueDeleter(deleteResBund);
    1.58 +  init(data, status);
    1.59 +}
    1.60 +
    1.61 +// init from headers and resource
    1.62 +// with checking the whether the size of resource matches 
    1.63 +// header size
    1.64 +RBDataMap::RBDataMap(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status) 
    1.65 +{
    1.66 +  fData = new Hashtable(TRUE, status);
    1.67 +  fData->setValueDeleter(deleteResBund);
    1.68 +  init(headers, data, status);
    1.69 +}
    1.70 +
    1.71 +
    1.72 +void RBDataMap::init(UResourceBundle *data, UErrorCode &status) {
    1.73 +  int32_t i = 0;
    1.74 +  fData->removeAll();
    1.75 +  UResourceBundle *t = NULL;
    1.76 +  for(i = 0; i < ures_getSize(data); i++) {
    1.77 +    t = ures_getByIndex(data, i, t, &status);
    1.78 +    fData->put(UnicodeString(ures_getKey(t), -1, US_INV), new ResourceBundle(t, status), status);
    1.79 +  }
    1.80 +  ures_close(t);
    1.81 +}
    1.82 +
    1.83 +void RBDataMap::init(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status)
    1.84 +{
    1.85 +  int32_t i = 0;
    1.86 +  fData->removeAll();
    1.87 +  UResourceBundle *t = NULL;
    1.88 +  const UChar *key = NULL;
    1.89 +  int32_t keyLen = 0;
    1.90 +  if(ures_getSize(headers) == ures_getSize(data)) {
    1.91 +    for(i = 0; i < ures_getSize(data); i++) {
    1.92 +      t = ures_getByIndex(data, i, t, &status);
    1.93 +      key = ures_getStringByIndex(headers, i, &keyLen, &status);
    1.94 +      fData->put(UnicodeString(key, keyLen), new ResourceBundle(t, status), status);
    1.95 +    }
    1.96 +  } else {
    1.97 +    // error
    1.98 +    status = U_INVALID_FORMAT_ERROR;
    1.99 +  }
   1.100 +  ures_close(t);
   1.101 +}
   1.102 +
   1.103 +const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const
   1.104 +{
   1.105 +  if(U_FAILURE(status)) {
   1.106 +    return NULL;
   1.107 +  }
   1.108 +
   1.109 +  UnicodeString hashKey(key, -1, US_INV);
   1.110 +  const ResourceBundle *r = (ResourceBundle *)fData->get(hashKey);
   1.111 +  if(r != NULL) {
   1.112 +    return r;
   1.113 +  } else {
   1.114 +    status = U_MISSING_RESOURCE_ERROR;
   1.115 +    return NULL;
   1.116 +  }
   1.117 +}
   1.118 +
   1.119 +const UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const
   1.120 +{
   1.121 +  const ResourceBundle *r = getItem(key, status);
   1.122 +  if(U_SUCCESS(status)) {
   1.123 +    return r->getString(status);
   1.124 +  } else {
   1.125 +    return UnicodeString();
   1.126 +  }
   1.127 +}
   1.128 +
   1.129 +int32_t
   1.130 +RBDataMap::getInt28(const char* key, UErrorCode &status) const
   1.131 +{
   1.132 +  const ResourceBundle *r = getItem(key, status);
   1.133 +  if(U_SUCCESS(status)) {
   1.134 +    return r->getInt(status);
   1.135 +  } else {
   1.136 +    return 0;
   1.137 +  }
   1.138 +}
   1.139 +
   1.140 +uint32_t
   1.141 +RBDataMap::getUInt28(const char* key, UErrorCode &status) const
   1.142 +{
   1.143 +  const ResourceBundle *r = getItem(key, status);
   1.144 +  if(U_SUCCESS(status)) {
   1.145 +    return r->getUInt(status);
   1.146 +  } else {
   1.147 +    return 0;
   1.148 +  }
   1.149 +}
   1.150 +
   1.151 +const int32_t *
   1.152 +RBDataMap::getIntVector(int32_t &length, const char *key, UErrorCode &status) const {
   1.153 +  const ResourceBundle *r = getItem(key, status);
   1.154 +  if(U_SUCCESS(status)) {
   1.155 +    return r->getIntVector(length, status);
   1.156 +  } else {
   1.157 +    return NULL;
   1.158 +  }
   1.159 +}
   1.160 +
   1.161 +const uint8_t *
   1.162 +RBDataMap::getBinary(int32_t &length, const char *key, UErrorCode &status) const {
   1.163 +  const ResourceBundle *r = getItem(key, status);
   1.164 +  if(U_SUCCESS(status)) {
   1.165 +    return r->getBinary(length, status);
   1.166 +  } else {
   1.167 +    return NULL;
   1.168 +  }
   1.169 +}
   1.170 +
   1.171 +int32_t RBDataMap::getInt(const char* key, UErrorCode &status) const
   1.172 +{
   1.173 +  UnicodeString r = this->getString(key, status);
   1.174 +  if(U_SUCCESS(status)) {
   1.175 +    return utoi(r);
   1.176 +  } else {
   1.177 +    return 0;
   1.178 +  }
   1.179 +}
   1.180 +
   1.181 +const UnicodeString* RBDataMap::getStringArray(int32_t& count, const char* key, UErrorCode &status) const 
   1.182 +{
   1.183 +  const ResourceBundle *r = getItem(key, status);
   1.184 +  if(U_SUCCESS(status)) {
   1.185 +    int32_t i = 0;
   1.186 +
   1.187 +    count = r->getSize();
   1.188 +    if(count <= 0) {
   1.189 +      return NULL;
   1.190 +    }
   1.191 +
   1.192 +    UnicodeString *result = new UnicodeString[count];
   1.193 +    for(i = 0; i<count; i++) {
   1.194 +      result[i] = r->getStringEx(i, status);
   1.195 +    }
   1.196 +    return result;
   1.197 +  } else {
   1.198 +    return NULL;
   1.199 +  }
   1.200 +}
   1.201 +
   1.202 +const int32_t* RBDataMap::getIntArray(int32_t& count, const char* key, UErrorCode &status) const
   1.203 +{
   1.204 +  const ResourceBundle *r = getItem(key, status);
   1.205 +  if(U_SUCCESS(status)) {
   1.206 +    int32_t i = 0;
   1.207 +
   1.208 +    count = r->getSize();
   1.209 +    if(count <= 0) {
   1.210 +      return NULL;
   1.211 +    }
   1.212 +
   1.213 +    int32_t *result = new int32_t[count];
   1.214 +    UnicodeString stringRes;
   1.215 +    for(i = 0; i<count; i++) {
   1.216 +      stringRes = r->getStringEx(i, status);
   1.217 +      result[i] = utoi(stringRes);
   1.218 +    }
   1.219 +    return result;
   1.220 +  } else {
   1.221 +    return NULL;
   1.222 +  }
   1.223 +}
   1.224 +

mercurial