Example #1
0
const UChar*
TimeZone::dereferOlsonLink(const UnicodeString& id) {
    const UChar *result = NULL;
    UErrorCode ec = U_ZERO_ERROR;
    UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);

    // resolve zone index by name
    UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec);
    int32_t idx = findInStringArray(names, id, ec);
    result = ures_getStringByIndex(names, idx, NULL, &ec);

    // open the zone bundle by index
    ures_getByKey(rb, kZONES, rb, &ec);
    ures_getByIndex(rb, idx, rb, &ec);

    if (U_SUCCESS(ec)) {
        if (ures_getType(rb) == URES_INT) {
            // this is a link - dereference the link
            int32_t deref = ures_getInt(rb, &ec);
            const UChar* tmp = ures_getStringByIndex(names, deref, NULL, &ec);
            if (U_SUCCESS(ec)) {
                result = tmp;
            }
        }
    }

    ures_close(names);
    ures_close(rb);

    return result;
}
Example #2
0
static jobjectArray getAmPmMarkers(JNIEnv* env, UResourceBundle* gregorian) {
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle gregorianElems(ures_getByKey(gregorian, "AmPmMarkers", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ures_resetIterator(gregorianElems.get());

    int lengthAm, lengthPm;
    const jchar* am = ures_getStringByIndex(gregorianElems.get(), 0, &lengthAm, &status);
    const jchar* pm = ures_getStringByIndex(gregorianElems.get(), 1, &lengthPm, &status);

    if (U_FAILURE(status)) {
        return NULL;
    }
    
    jobjectArray amPmMarkers = env->NewObjectArray(2, string_class, NULL);
    jstring amU = env->NewString(am, lengthAm);
    env->SetObjectArrayElement(amPmMarkers, 0, amU);
    env->DeleteLocalRef(amU);
    jstring pmU = env->NewString(pm, lengthPm);
    env->SetObjectArrayElement(amPmMarkers, 1, pmU);
    env->DeleteLocalRef(pmU);

    return amPmMarkers;
}
Example #3
0
    TZEnumeration(const char* country) : map(NULL), len(0), pos(0) {
        if (!getOlsonMeta()) {
            return;
        }

        UErrorCode ec = U_ZERO_ERROR;
        UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
        ures_getByKey(res, kREGIONS, res, &ec);
        if (U_SUCCESS(ec) && ures_getType(res) == URES_ARRAY) {
            UChar uCountry[] = {0, 0, 0, 0};
            if (country) {
                u_charsToUChars(country, uCountry, 2);
            } else {
                u_strcpy(uCountry, WORLD);
            }

            // count matches
            int32_t count = 0;
            int32_t i;
            const UChar *region;
            for (i = 0; i < ures_getSize(res); i++) {
                region = ures_getStringByIndex(res, i, NULL, &ec);
                if (U_FAILURE(ec)) {
                    break;
                }
                if (u_strcmp(uCountry, region) == 0) {
                    count++;
                }
            }

            if (count > 0) {
                map = (int32_t*)uprv_malloc(sizeof(int32_t) * count);
                if (map != NULL) {
                    int32_t idx = 0;
                    for (i = 0; i < ures_getSize(res); i++) {
                        region = ures_getStringByIndex(res, i, NULL, &ec);
                        if (U_FAILURE(ec)) {
                            break;
                        }
                        if (u_strcmp(uCountry, region) == 0) {
                            map[idx++] = i;
                        }
                    }
                    if (U_SUCCESS(ec)) {
                        len = count;
                    } else {
                        uprv_free(map);
                        map = NULL;
                    }
                } else {
                    U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec)));
                }
            }
        }
        ures_close(res);
    }
static jstring getDecimalPatternChars(JNIEnv *env, UResourceBundle *rootElems) {
    
    UErrorCode status = U_ZERO_ERROR;

    int zeroL, digitL, decSepL, groupL, listL, percentL, permillL, expL, currSepL, minusL;
    int patternLength;

    jchar *patternChars;

    const jchar* zero = ures_getStringByIndex(rootElems, 4, &zeroL, &status);

    const jchar* digit = ures_getStringByIndex(rootElems, 5, &digitL, &status);

    const jchar* decSep = ures_getStringByIndex(rootElems, 0, &decSepL, &status);

    const jchar* group = ures_getStringByIndex(rootElems, 1, &groupL, &status);

    const jchar* list = ures_getStringByIndex(rootElems, 2, &listL, &status);

    const jchar* percent = ures_getStringByIndex(rootElems, 3, &percentL, &status);

    const jchar* permill = ures_getStringByIndex(rootElems, 8, &permillL, &status);

    const jchar* exp = ures_getStringByIndex(rootElems, 7, &expL, &status);

    const jchar* currSep = ures_getStringByIndex(rootElems, 0, &currSepL, &status);

    const jchar* minus = ures_getStringByIndex(rootElems, 6, &minusL, &status);

    if(U_FAILURE(status)) {
        return NULL;
    }


    patternChars = (jchar *) malloc(11 * sizeof(jchar));

    patternChars[0] = 0;

    u_strncat(patternChars, zero, 1);
    u_strncat(patternChars, digit, 1);
    u_strncat(patternChars, decSep, 1);
    u_strncat(patternChars, group, 1);
    u_strncat(patternChars, list, 1);
    u_strncat(patternChars, percent, 1);
    u_strncat(patternChars, permill, 1);
    u_strncat(patternChars, exp, 1);
    u_strncat(patternChars, currSep, 1);
    u_strncat(patternChars, minus, 1);

    jstring decimalPatternChars = env->NewString(patternChars, 10);

    free(patternChars);

    return decimalPatternChars;
}
Example #5
0
static jobjectArray getNames(JNIEnv* env, UResourceBundle* namesBundle, bool months, NameType type, NameWidth width) {
    const char* typeKey = (type == REGULAR) ? "format" : "stand-alone";
    const char* widthKey = (width == LONG) ? "wide" : "abbreviated";
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle formatBundle(ures_getByKey(namesBundle, typeKey, NULL, &status));
    ScopedResourceBundle valuesBundle(ures_getByKey(formatBundle.get(), widthKey, NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    // The months array has a trailing empty string. The days array has a leading empty string.
    int count = ures_getSize(valuesBundle.get());
    jobjectArray result = env->NewObjectArray(count + 1, JniConstants::stringClass, NULL);
    env->SetObjectArrayElement(result, months ? count : 0, env->NewStringUTF(""));
    int arrayOffset = months ? 0 : 1;
    for (int i = 0; i < count; ++i) {
        int nameLength;
        const jchar* name = ures_getStringByIndex(valuesBundle.get(), i, &nameLength, &status);
        if (U_FAILURE(status)) {
            return NULL;
        }
        ScopedLocalRef<jstring> nameString(env, env->NewString(name, nameLength));
        env->SetObjectArrayElement(result, arrayOffset++, nameString.get());
    }
    return result;
}
Example #6
0
static jstring ICU_getCurrencySymbol(JNIEnv* env, jclass, jstring locale, jstring currencyCode) {
    // We can't use ucurr_getName because it doesn't distinguish between using data root from
    // the root locale and parroting back the input because it's never heard of the currency code.
    ScopedUtfChars localeName(env, locale);
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle currLoc(ures_open(U_ICUDATA_CURR, localeName.c_str(), &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle currencies(ures_getByKey(currLoc.get(), "Currencies", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedUtfChars currency(env, currencyCode);
    ScopedResourceBundle currencyElems(ures_getByKey(currencies.get(), currency.c_str(), NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    int32_t charCount;
    const jchar* chars = ures_getStringByIndex(currencyElems.get(), 0, &charCount, &status);
    if (U_FAILURE(status)) {
        return NULL;
    }
    return (charCount == 0) ? NULL : env->NewString(chars, charCount);
}
// ---------------------------------------------------------------------------
//  Implementation of the virtual message loader API
// ---------------------------------------------------------------------------
bool ICUMsgLoader::loadMsg( const   XMLMsgLoader::XMLMsgId  msgToLoad
                          ,         XMLCh* const            toFill
                          , const   XMLSize_t               maxChars)
{
    UErrorCode   err = U_ZERO_ERROR;
    int32_t      strLen = 0;

    // Assuming array format
    const UChar *name = ures_getStringByIndex(fDomainBundle, (int32_t)msgToLoad-1, &strLen, &err);

    if (!U_SUCCESS(err) || (name == NULL))
    {
        return false;
    }

    int retStrLen = strLen > (int32_t)maxChars ? maxChars : strLen;

    if (sizeof(UChar)==sizeof(XMLCh))
    {
        XMLString::moveChars(toFill, (XMLCh*)name, retStrLen);
        toFill[retStrLen] = (XMLCh) 0;
    }
    else
    {
        XMLCh* retStr = toFill;
        const UChar *srcPtr = name;

        while (retStrLen--)
           *retStr++ = *srcPtr++;

        *retStr = 0;
    }

    return true;
}
Example #8
0
static jstring getCurrencySymbolNative(JNIEnv* env, jclass clazz, 
        jstring locale, jstring currencyCode) {
    // LOGI("ENTER getCurrencySymbolNative");

    const char* locName = env->GetStringUTFChars(locale, NULL);
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle root(ures_open(NULL, locName, &status));
    env->ReleaseStringUTFChars(locale, locName);
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle currencies(ures_getByKey(root.get(), "Currencies", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    const char* currName = env->GetStringUTFChars(currencyCode, NULL);
    ScopedResourceBundle currencyElems(ures_getByKey(currencies.get(), currName, NULL, &status));
    env->ReleaseStringUTFChars(currencyCode, currName);
    if (U_FAILURE(status)) {
        return NULL;
    }

    int currSymbL;
    const jchar* currSymbU = ures_getStringByIndex(currencyElems.get(), 0, &currSymbL, &status);
    if (U_FAILURE(status)) {
        return NULL;
    }

    return (currSymbL == 0) ? NULL : env->NewString(currSymbU, currSymbL);
}
Example #9
0
static jstring ICU_getCurrencySymbolNative(JNIEnv* env, jclass, jstring locale, jstring currencyCode) {
    ScopedUtfChars localeName(env, locale);
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle currLoc(ures_open(U_ICUDATA_CURR, localeName.c_str(), &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle currencies(ures_getByKey(currLoc.get(), "Currencies", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedUtfChars currency(env, currencyCode);
    ScopedResourceBundle currencyElems(ures_getByKey(currencies.get(), currency.c_str(), NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    int currSymbL;
    const jchar* currSymbU = ures_getStringByIndex(currencyElems.get(), 0, &currSymbL, &status);
    if (U_FAILURE(status)) {
        return NULL;
    }

    return (currSymbL == 0) ? NULL : env->NewString(currSymbU, currSymbL);
}
Example #10
0
static jobjectArray getEras(JNIEnv* env, UResourceBundle* gregorian) {
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle gregorianElems(ures_getByKey(gregorian, "eras", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle eraElems(ures_getByKey(gregorianElems.get(), "abbreviated", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    int eraCount = ures_getSize(eraElems.get());
    jobjectArray eras = env->NewObjectArray(eraCount, string_class, NULL);

    ures_resetIterator(eraElems.get());
    for (int i = 0; i < eraCount; ++i) {
        int eraLength;
        const jchar* era = ures_getStringByIndex(eraElems.get(), i, &eraLength, &status);
        if (U_FAILURE(status)) {
            return NULL;
        }
        jstring eraU = env->NewString(era, eraLength);
        env->SetObjectArrayElement(eras, i, eraU);
        env->DeleteLocalRef(eraU);
    }
    return eras;
}
Example #11
0
static jobjectArray getWeekdayNames(JNIEnv* env, UResourceBundle* gregorian, bool longNames) {
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle gregorianElems(ures_getByKey(gregorian, "dayNames", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle dayNameElems(ures_getByKey(gregorianElems.get(), "format", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ScopedResourceBundle dayNameElemsFormat(ures_getByKey(dayNameElems.get(), longNames ? "wide" : "abbreviated", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    ures_resetIterator(dayNameElemsFormat.get());
    int dayCount = ures_getSize(dayNameElemsFormat.get());
    jobjectArray weekdays = env->NewObjectArray(dayCount + 1, string_class, NULL);
    // first entry in the weekdays array is an empty string
    env->SetObjectArrayElement(weekdays, 0, env->NewStringUTF(""));
    for(int i = 0; i < dayCount; i++) {
        int dayNameLength;
        const jchar* day = ures_getStringByIndex(dayNameElemsFormat.get(), i, &dayNameLength, &status);
        if(U_FAILURE(status)) {
            return NULL;
        }
        jstring dayU = env->NewString(day, dayNameLength);
        env->SetObjectArrayElement(weekdays, i + 1, dayU);
        env->DeleteLocalRef(dayU);
    }
    return weekdays;
}
Example #12
0
static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
    UErrorCode status = U_ZERO_ERROR;
    int charCount;
    const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
    if (U_SUCCESS(status)) {
        setStringField(env, obj, fieldName, env->NewString(chars, charCount));
    }
}
static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
  UErrorCode status = U_ZERO_ERROR;
  int charCount;
  const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
  if (U_SUCCESS(status)) {
    setStringField(env, obj, fieldName, env->NewString(chars, charCount));
  } else {
    ALOGE("Error setting String field %s from ICU resource (index %d): %s", fieldName, index, u_errorName(status));
  }
}
Example #14
0
static void setCharField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
    UErrorCode status = U_ZERO_ERROR;
    int charCount;
    const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
    if (U_SUCCESS(status)) {
        jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "C");
        env->SetCharField(obj, fid, chars[0]);
    } else {
        LOGE("Error setting char field %s from ICU resource: %s", fieldName, u_errorName(status));
    }
}
static jobjectArray getShortMonthNames(JNIEnv *env, UResourceBundle *gregorian) {
    
    UErrorCode status = U_ZERO_ERROR;

    const jchar* shortMonth;
    jstring shortMonthU;

    UResourceBundle *gregorianElems = ures_getByKey(gregorian, "monthNames", NULL, &status);
    if(U_FAILURE(status)) {
        return NULL;
    }

    UResourceBundle *monthNameElems = ures_getByKey(gregorianElems, "format", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(gregorianElems);
        return NULL;
    }

    UResourceBundle *monthNameElemsFormat = ures_getByKey(monthNameElems, "abbreviated", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(monthNameElems);
        ures_close(gregorianElems);
        return NULL;
    }

    int shortMonthNameLength;
    ures_resetIterator(monthNameElemsFormat);
    int shortMonthCount = ures_getSize(monthNameElemsFormat);
    // the array length is +1 because the harmony locales had an empty string at the end of their month name array
    jobjectArray shortMonths = env->NewObjectArray(shortMonthCount + 1, string_class, NULL);
    for(int i = 0; i < shortMonthCount; i++) {
        shortMonth = ures_getStringByIndex(monthNameElemsFormat, i, &shortMonthNameLength, &status);
        if(U_FAILURE(status)) {
            ures_close(monthNameElemsFormat);
            ures_close(monthNameElems);
            ures_close(gregorianElems);
            return NULL;
        }
        shortMonthU = env->NewString(shortMonth, shortMonthNameLength);
        env->SetObjectArrayElement(shortMonths, i, shortMonthU);
        env->DeleteLocalRef(shortMonthU);
    }

    shortMonthU = env->NewStringUTF("");
    env->SetObjectArrayElement(shortMonths, shortMonthCount, shortMonthU);
    env->DeleteLocalRef(shortMonthU);

    ures_close(monthNameElemsFormat);
    ures_close(monthNameElems);
    ures_close(gregorianElems);
    return shortMonths;
}
Example #16
0
static UBool loadOlsonIDs() {
    if (OLSON_IDS != 0) {
        return TRUE;
    }

    UErrorCode ec = U_ZERO_ERROR;
    UnicodeString* ids = 0;
    int32_t count = 0;
    UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
    UResourceBundle *nres = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Names section
    if (U_SUCCESS(ec)) {
        getOlsonMeta(top);
        int32_t start = 0;
        count = ures_getSize(nres);
        ids = new UnicodeString[(count > 0) ? count : 1];
        // Null pointer check
        if (ids != NULL) {
            for (int32_t i=0; i<count; ++i) {
                int32_t idLen = 0;
                const UChar* id = ures_getStringByIndex(nres, i, &idLen, &ec);
                ids[i].fastCopyFrom(UnicodeString(TRUE, id, idLen));
                if (U_FAILURE(ec)) {
                    break;
                }
            }
        } else {
            ec = U_MEMORY_ALLOCATION_ERROR;
        }
    }
    ures_close(nres);
    ures_close(top);

    if (U_FAILURE(ec)) {
        delete[] ids;
        return FALSE;
    }

    // Keep mutexed operations as short as possible by doing all
    // computations first, then doing pointer copies within the mutex.
    umtx_lock(&LOCK);
    if (OLSON_IDS == 0) {
        OLSON_IDS = ids;
        ids = 0;
        ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
    }
    umtx_unlock(&LOCK);

    // If another thread initialized the statics first, then delete
    // our unused data.
    delete[] ids;
    return TRUE;
}
static jobjectArray getMonthNames(JNIEnv *env, UResourceBundle *gregorian) {
    
    UErrorCode status = U_ZERO_ERROR;

    const jchar* month;
    jstring monthU;

    UResourceBundle *gregorianElems = ures_getByKey(gregorian, "monthNames", NULL, &status);
    if(U_FAILURE(status)) {
        return NULL;
    }

    UResourceBundle *monthNameElems = ures_getByKey(gregorianElems, "format", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(gregorianElems);
        return NULL;
    }

    UResourceBundle *monthNameElemsFormat = ures_getByKey(monthNameElems, "wide", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(monthNameElems);
        ures_close(gregorianElems);
        return NULL;
    }

    int monthNameLength;
    ures_resetIterator(monthNameElemsFormat);
    int monthCount = ures_getSize(monthNameElemsFormat);
    jobjectArray months = env->NewObjectArray(monthCount + 1, string_class, NULL);
    for(int i = 0; i < monthCount; i++) {
        month = ures_getStringByIndex(monthNameElemsFormat, i, &monthNameLength, &status);
        if(U_FAILURE(status)) {
            ures_close(monthNameElemsFormat);
            ures_close(monthNameElems);
            ures_close(gregorianElems);
            return NULL;
        }
        monthU = env->NewString(month, monthNameLength);
        env->SetObjectArrayElement(months, i, monthU);
        env->DeleteLocalRef(monthU);
    }

    monthU = env->NewStringUTF("");
    env->SetObjectArrayElement(months, monthCount, monthU);
    env->DeleteLocalRef(monthU);

    ures_close(monthNameElemsFormat);
    ures_close(monthNameElems);
    ures_close(gregorianElems);
    return months;
}
static jobjectArray getWeekdayNames(JNIEnv *env, UResourceBundle *gregorian) {
    
    UErrorCode status = U_ZERO_ERROR;

    const jchar* day;
    jstring dayU;

    UResourceBundle *gregorianElems = ures_getByKey(gregorian, "dayNames", NULL, &status);
    if(U_FAILURE(status)) {
        return NULL;
    }

    UResourceBundle *dayNameElems = ures_getByKey(gregorianElems, "format", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(gregorianElems);
        return NULL;
    }

    UResourceBundle *dayNameElemsFormat = ures_getByKey(dayNameElems, "wide", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(dayNameElems);
        ures_close(gregorianElems);
        return NULL;
    }

    int dayNameLength;
    ures_resetIterator(dayNameElemsFormat);
    int dayCount = ures_getSize(dayNameElemsFormat);
    jobjectArray weekdays = env->NewObjectArray(dayCount + 1, string_class, NULL);
    // first entry in the weekdays array is an empty string
    env->SetObjectArrayElement(weekdays, 0, env->NewStringUTF(""));
    for(int i = 0; i < dayCount; i++) {
        day = ures_getStringByIndex(dayNameElemsFormat, i, &dayNameLength, &status);
        if(U_FAILURE(status)) {
            ures_close(dayNameElemsFormat);
            ures_close(dayNameElems);
            ures_close(gregorianElems);
            return NULL;
        }
        dayU = env->NewString(day, dayNameLength);
        env->SetObjectArrayElement(weekdays, i + 1, dayU);
        env->DeleteLocalRef(dayU);
    }
    
    ures_close(dayNameElemsFormat);
    ures_close(dayNameElems);
    ures_close(gregorianElems);
    return weekdays;

}
Example #19
0
static jobjectArray getAmPmMarkers(JNIEnv* env, UResourceBundle* gregorian) {
    UErrorCode status = U_ZERO_ERROR;
    ScopedResourceBundle gregorianElems(ures_getByKey(gregorian, "AmPmMarkers", NULL, &status));
    if (U_FAILURE(status)) {
        return NULL;
    }

    int lengthAm, lengthPm;
    const jchar* am = ures_getStringByIndex(gregorianElems.get(), 0, &lengthAm, &status);
    const jchar* pm = ures_getStringByIndex(gregorianElems.get(), 1, &lengthPm, &status);

    if (U_FAILURE(status)) {
        return NULL;
    }

    jobjectArray amPmMarkers = env->NewObjectArray(2, JniConstants::stringClass, NULL);
    ScopedLocalRef<jstring> amU(env, env->NewString(am, lengthAm));
    env->SetObjectArrayElement(amPmMarkers, 0, amU.get());
    ScopedLocalRef<jstring> pmU(env, env->NewString(pm, lengthPm));
    env->SetObjectArrayElement(amPmMarkers, 1, pmU.get());

    return amPmMarkers;
}
static jobjectArray getShortWeekdayNames(JNIEnv *env, UResourceBundle *gregorian) {
    
    UErrorCode status = U_ZERO_ERROR;

    const jchar* shortDay;
    jstring shortDayU;

    UResourceBundle *gregorianElems = ures_getByKey(gregorian, "dayNames", NULL, &status);
    if(U_FAILURE(status)) {
        return NULL;
    }

    UResourceBundle *dayNameElems = ures_getByKey(gregorianElems, "format", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(gregorianElems);
        return NULL;
    }

    UResourceBundle *dayNameElemsFormat = ures_getByKey(dayNameElems, "abbreviated", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(dayNameElems);
        ures_close(gregorianElems);
        return NULL;
    }

    int shortDayNameLength;
    ures_resetIterator(dayNameElemsFormat);
    int shortDayCount = ures_getSize(dayNameElemsFormat);
    jobjectArray shortWeekdays = env->NewObjectArray(shortDayCount + 1, string_class, NULL);
    env->SetObjectArrayElement(shortWeekdays, 0, env->NewStringUTF(""));
    for(int i = 0; i < shortDayCount; i++) {
        shortDay = ures_getStringByIndex(dayNameElemsFormat, i, &shortDayNameLength, &status);
        if(U_FAILURE(status)) {
            ures_close(dayNameElemsFormat);
            ures_close(dayNameElems);
            ures_close(gregorianElems);
            return NULL;
        }
        shortDayU = env->NewString(shortDay, shortDayNameLength);
        env->SetObjectArrayElement(shortWeekdays, i + 1, shortDayU);
        env->DeleteLocalRef(shortDayU);
    }

    ures_close(dayNameElemsFormat);
    ures_close(dayNameElems);
    ures_close(gregorianElems);
    return shortWeekdays;
}
Example #21
0
 UBool getID(int32_t i) {
     UErrorCode ec = U_ZERO_ERROR;
     int32_t idLen = 0;
     const UChar* id = NULL;
     UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
     top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section
     id = ures_getStringByIndex(top, i, &idLen, &ec);
     if(U_FAILURE(ec)) {
         unistr.truncate(0);
     }
     else {
         unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen));
     }
     ures_close(top);
     return U_SUCCESS(ec);
 }
static jstring getCurrencySymbolNative(JNIEnv* env, jclass clazz, 
        jstring locale, jstring currencyCode) {
    // LOGI("ENTER getCurrencySymbolNative");

    UErrorCode status = U_ZERO_ERROR;

    const char *locName = env->GetStringUTFChars(locale, NULL);
    UResourceBundle *root = ures_open(NULL, locName, &status);
    env->ReleaseStringUTFChars(locale, locName);
    if(U_FAILURE(status)) {
        return NULL;
    }

    UResourceBundle *rootElems = ures_getByKey(root, "Currencies", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(root);
        return NULL;
    }

    const char *currName = env->GetStringUTFChars(currencyCode, NULL);
    UResourceBundle *currencyElems = ures_getByKey(rootElems, currName, NULL, &status);
    env->ReleaseStringUTFChars(currencyCode, currName);
    if(U_FAILURE(status)) {
        ures_close(rootElems);
        ures_close(root);
        return NULL;
    }

    int currSymbL;
    const jchar *currSymbU = ures_getStringByIndex(currencyElems, 0, &currSymbL, &status);
    if(U_FAILURE(status)) {
        ures_close(currencyElems);
        ures_close(rootElems);
        ures_close(root);
        return NULL;
    }

    ures_close(currencyElems);
    ures_close(rootElems);
    ures_close(root);

    if(currSymbL == 0) {
        return NULL;
    }
    return env->NewString(currSymbU, currSymbL);
}
Example #23
0
static void EnumUResourceBundle(const UResourceBundle* bundle,
                                EnumCalendarInfoCallback callback,
                                const void* context)
{
    int32_t eraNameCount = ures_getSize(bundle);

    for (int i = 0; i < eraNameCount; i++)
    {
        UErrorCode status = U_ZERO_ERROR;
        int32_t ignore; // We don't care about the length of the string as it is null terminated.
        const UChar* eraName = ures_getStringByIndex(bundle, i, &ignore, &status);

        if (U_SUCCESS(status))
        {
            callback(eraName, context);
        }
    }
}
static jobjectArray getEras(JNIEnv* env, UResourceBundle *gregorian) {
    
    jobjectArray eras;
    jstring eraU;
    const jchar* era;

    UErrorCode status = U_ZERO_ERROR;

    UResourceBundle *gregorianElems;
    UResourceBundle *eraElems;

    gregorianElems = ures_getByKey(gregorian, "eras", NULL, &status);
    if(U_FAILURE(status)) {
        return NULL;
    }

    eraElems = ures_getByKey(gregorianElems, "abbreviated", NULL, &status);
    if(U_FAILURE(status)) {
        ures_close(gregorianElems);
        return NULL;
    }

    int eraLength;

    int eraCount = ures_getSize(eraElems);
    eras = env->NewObjectArray(eraCount, string_class, NULL);

    ures_resetIterator(eraElems);
    for(int i = 0; i < eraCount; i++) {
        era = ures_getStringByIndex(eraElems, i, &eraLength, &status);
        if(U_FAILURE(status)) {
            ures_close(gregorianElems);
            ures_close(eraElems);
            return NULL;
        }
        eraU = env->NewString(era, eraLength);
        env->SetObjectArrayElement(eras, i, eraU);
        env->DeleteLocalRef(eraU);
    }
    ures_close(eraElems);
    ures_close(gregorianElems);

    return eras;
}
Example #25
0
void RBDataMap::init(UResourceBundle *headers, UResourceBundle *data, UErrorCode &status)
{
  int32_t i = 0;
  fData->removeAll();
  UResourceBundle *t = NULL;
  const UChar *key = NULL;
  int32_t keyLen = 0;
  if(ures_getSize(headers) == ures_getSize(data)) {
    for(i = 0; i < ures_getSize(data); i++) {
      t = ures_getByIndex(data, i, t, &status);
      key = ures_getStringByIndex(headers, i, &keyLen, &status);
      fData->put(UnicodeString(key, keyLen), new ResourceBundle(t, status), status);
    }
  } else {
    // error
    status = U_INVALID_FORMAT_ERROR;
  }
  ures_close(t);
}
Example #26
0
const UChar *getErrorName(UErrorCode errorNumber) {
    UErrorCode status = U_ZERO_ERROR;
    int32_t len = 0;

    UResourceBundle *error = ures_open(currdir, locale, &status);

    UResourceBundle *errorcodes = ures_getByKey(error, "errorcodes", NULL, &status);

    const UChar *result = ures_getStringByIndex(errorcodes, errorNumber, &len, &status);

    ures_close(errorcodes);
    ures_close(error);

    if(U_SUCCESS(status)) {
        return result;
    } else {
        return baderror;
    }

}
Example #27
0
void RelativeDateFormat::loadDates(UErrorCode &status) {
    UResourceBundle *rb = ures_open(NULL, fLocale.getBaseName(), &status);
    LocalUResourceBundlePointer dateTimePatterns(
        ures_getByKeyWithFallback(rb,
                                  "calendar/gregorian/DateTimePatterns",
                                  (UResourceBundle*)NULL, &status));
    if(U_SUCCESS(status)) {
        int32_t patternsSize = ures_getSize(dateTimePatterns.getAlias());
        if (patternsSize > kDateTime) {
            int32_t resStrLen = 0;
            int32_t glueIndex = kDateTime;
            if (patternsSize >= (kDateTimeOffset + kShort + 1)) {
                int32_t offsetIncrement = (fDateStyle & ~kRelative); // Remove relative bit.
                if (offsetIncrement >= (int32_t)kFull &&
                    offsetIncrement <= (int32_t)kShortRelative) {
                    glueIndex = kDateTimeOffset + offsetIncrement;
                }
            }

            const UChar *resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), glueIndex, &resStrLen, &status);
            if (U_SUCCESS(status) && resStrLen >= patItem1Len && u_strncmp(resStr,patItem1,patItem1Len)==0) {
                fCombinedHasDateAtStart = TRUE;
            }
            fCombinedFormat = new SimpleFormatter(UnicodeString(TRUE, resStr, resStrLen), 2, 2, status);
        }
    }

    // Data loading for relative names, e.g., "yesterday", "today", "tomorrow".
    fDatesLen = UDAT_DIRECTION_COUNT; // Maximum defined by data.
    fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);

    RelDateFmtDataSink sink(fDates, fDatesLen);
    ures_getAllItemsWithFallback(rb, "fields/day/relative", sink, status);

    ures_close(rb);

    if(U_FAILURE(status)) {
        fDatesLen=0;
        return;
    }
}
Example #28
0
const UnicodeString U_EXPORT2
TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) {
    U_DEBUG_TZ_MSG(("gEI(%d)\n", index));
    UnicodeString result;
    UErrorCode ec = U_ZERO_ERROR;
    UResourceBundle res;
    ures_initStackObject(&res);
    UResourceBundle *top = openOlsonResource(id, res, ec);
    int32_t zone = -1;
    if (U_SUCCESS(ec)) {
        int32_t size = ures_getSize(&res);
        if (size == 4 || size == 6) {
            UResourceBundle r;
            ures_initStackObject(&r);
            ures_getByIndex(&res, size-1, &r, &ec);
            const int32_t* v = ures_getIntVector(&r, &size, &ec);
            if (index >= 0 && index < size && getOlsonMeta()) {
                zone = v[index];
            }
            ures_close(&r);
        }
    }
    ures_close(&res);
    if (zone >= 0) {
        UResourceBundle *ares = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Zones section
        if (U_SUCCESS(ec)) {
            int32_t idLen = 0;
            const UChar* id = ures_getStringByIndex(ares, zone, &idLen, &ec);
            result.fastCopyFrom(UnicodeString(TRUE, id, idLen));
            U_DEBUG_TZ_MSG(("gei(%d) -> %d, len%d, %s\n", index, zone, result.length(), u_errorName(ec)));
        }
        ures_close(ares);
    }
    ures_close(top);
#if defined(U_DEBUG_TZ)
    if(result.length() ==0) {
      U_DEBUG_TZ_MSG(("equiv [__, #%d] -> 0 (%s)\n", index, u_errorName(ec)));
    }
#endif
    return result;
}
Example #29
0
static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id, UErrorCode &status)
{
    UnicodeString copy;
    const UChar *u;
    int32_t len;
    
    int32_t start = 0;
    int32_t limit = ures_getSize(array);
    int32_t mid;
    int32_t lastMid = INT32_MAX;
    if(U_FAILURE(status) || (limit < 1)) { 
        return -1;
    }
    U_DEBUG_TZ_MSG(("fisa: Looking for %s, between %d and %d\n", U_DEBUG_TZ_STR(UnicodeString(id).getTerminatedBuffer()), start, limit));
    
    for (;;) {
        mid = (int32_t)((start + limit) / 2);
        if (lastMid == mid) {   /* Have we moved? */
            break;  /* We haven't moved, and it wasn't found. */
        }
        lastMid = mid;
        u = ures_getStringByIndex(array, mid, &len, &status);
        if (U_FAILURE(status)) {
            break;
        }
        U_DEBUG_TZ_MSG(("tz: compare to %s, %d .. [%d] .. %d\n", U_DEBUG_TZ_STR(u), start, mid, limit));
        copy.setTo(TRUE, u, len);
        int r = id.compare(copy);
        if(r==0) {
            U_DEBUG_TZ_MSG(("fisa: found at %d\n", mid));
            return mid;
        } else if(r<0) {
            limit = mid;
        } else {
            start = mid;
        }
    }
    U_DEBUG_TZ_MSG(("fisa: not found\n"));
    return -1;
}
Example #30
0
const UChar*
TimeZone::getRegion(const UnicodeString& id) {
    const UChar *result = WORLD;
    UErrorCode ec = U_ZERO_ERROR;
    UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);

    // resolve zone index by name
    UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &ec);
    int32_t idx = findInStringArray(res, id, ec);

    // get region mapping
    ures_getByKey(rb, kREGIONS, res, &ec);
    const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &ec);
    if (U_SUCCESS(ec)) {
        result = tmp;
    }

    ures_close(res);
    ures_close(rb);

    return result;
}