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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /********************************************************************
     2  * COPYRIGHT: 
     3  * Copyright (c) 2002-2006, 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"
    11 #include "hash.h"
    12 #include <stdlib.h>
    14 DataMap::~DataMap() {}
    15 DataMap::DataMap() {}
    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 }
    28 U_CDECL_BEGIN
    29 void U_CALLCONV
    30 deleteResBund(void *obj) {
    31   delete (ResourceBundle *)obj;
    32 }
    33 U_CDECL_END
    36 RBDataMap::~RBDataMap()
    37 {
    38   delete fData;
    39 }
    41 RBDataMap::RBDataMap()
    42 {
    43   UErrorCode status = U_ZERO_ERROR;
    44   fData = new Hashtable(TRUE, status);
    45   fData->setValueDeleter(deleteResBund);
    46 }
    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 }
    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 }
    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);
    75     fData->put(UnicodeString(ures_getKey(t), -1, US_INV), new ResourceBundle(t, status), status);
    76   }
    77   ures_close(t);
    78 }
    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 }
   100 const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const
   101 {
   102   if(U_FAILURE(status)) {
   103     return NULL;
   104   }
   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 }
   116 const 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 }
   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 }
   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;
   145   }
   146 }
   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 }
   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 }
   168 int32_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 }
   178 const 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;
   184     count = r->getSize();
   185     if(count <= 0) {
   186       return NULL;
   187     }
   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 }
   199 const 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;
   205     count = r->getSize();
   206     if(count <= 0) {
   207       return NULL;
   208     }
   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 }

mercurial