/*
**********************************************************************
-* Copyright (c) 2002-2011, International Business Machines
+* Copyright (c) 2002-2012, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
static const int32_t MAX_POW10 = (sizeof(POW10)/sizeof(POW10[0])) - 1;
-#define ISO_COUNTRY_CODE_LENGTH 3
+#define ISO_CURRENCY_CODE_LENGTH 3
//------------------------------------------------------------
// Resource tags
*/
static inline char*
myUCharsToChars(char* resultOfLen4, const UChar* currency) {
- u_UCharsToChars(currency, resultOfLen4, ISO_COUNTRY_CODE_LENGTH);
- resultOfLen4[ISO_COUNTRY_CODE_LENGTH] = 0;
+ u_UCharsToChars(currency, resultOfLen4, ISO_CURRENCY_CODE_LENGTH);
+ resultOfLen4[ISO_CURRENCY_CODE_LENGTH] = 0;
return resultOfLen4;
}
}
// Look up our currency, or if that's not available, then DEFAULT
- char buf[ISO_COUNTRY_CODE_LENGTH+1];
+ char buf[ISO_CURRENCY_CODE_LENGTH+1];
UErrorCode ec2 = U_ZERO_ERROR; // local error code: soft failure
UResourceBundle* rb = ures_getByKey(currencyMeta, myUCharsToChars(buf, currency), NULL, &ec2);
if (U_FAILURE(ec2)) {
struct CReg : public icu::UMemory {
CReg *next;
- UChar iso[ISO_COUNTRY_CODE_LENGTH+1];
+ UChar iso[ISO_CURRENCY_CODE_LENGTH+1];
char id[ULOC_FULLNAME_CAPACITY];
CReg(const UChar* _iso, const char* _id)
}
uprv_strncpy(id, _id, len);
id[len] = 0;
- uprv_memcpy(iso, _iso, ISO_COUNTRY_CODE_LENGTH * sizeof(const UChar));
- iso[ISO_COUNTRY_CODE_LENGTH] = 0;
+ uprv_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH * sizeof(const UChar));
+ iso[ISO_CURRENCY_CODE_LENGTH] = 0;
}
static UCurrRegistryKey reg(const UChar* _iso, const char* _id, UErrorCode* status)
return 0;
}
- char buf[ISO_COUNTRY_CODE_LENGTH+1];
+ char buf[ISO_CURRENCY_CODE_LENGTH+1];
myUCharsToChars(buf, currency);
/* Normalize the keyword value to uppercase */
}
// If we fail to find a match, use the ISO 4217 code
- *len = u_strlen(currency); // Should == ISO_COUNTRY_CODE_LENGTH, but maybe not...?
+ *len = u_strlen(currency); // Should == ISO_CURRENCY_CODE_LENGTH, but maybe not...?
*ec = U_USING_DEFAULT_WARNING;
return currency;
}
return 0;
}
- char buf[ISO_COUNTRY_CODE_LENGTH+1];
+ char buf[ISO_CURRENCY_CODE_LENGTH+1];
myUCharsToChars(buf, currency);
const UChar* s = NULL;
}
// If we fail to find a match, use the ISO 4217 code
- *len = u_strlen(currency); // Should == ISO_COUNTRY_CODE_LENGTH, but maybe not...?
+ *len = u_strlen(currency); // Should == ISO_CURRENCY_CODE_LENGTH, but maybe not...?
*ec = U_USING_DEFAULT_WARNING;
return currency;
}
return en;
}
+
+U_CAPI int32_t U_EXPORT2
+ucurr_getNumericCode(const UChar* currency) {
+ int32_t code = 0;
+ if (currency && u_strlen(currency) == ISO_CURRENCY_CODE_LENGTH) {
+ UErrorCode status = U_ZERO_ERROR;
+
+ UResourceBundle *bundle = ures_openDirect(0, "currencyNumericCodes", &status);
+ ures_getByKey(bundle, "codeMap", bundle, &status);
+ if (U_SUCCESS(status)) {
+ char alphaCode[ISO_CURRENCY_CODE_LENGTH+1];
+ myUCharsToChars(alphaCode, currency);
+ T_CString_toUpperCase(alphaCode);
+ ures_getByKey(bundle, alphaCode, bundle, &status);
+ int tmpCode = ures_getInt(bundle, &status);
+ if (U_SUCCESS(status)) {
+ code = tmpCode;
+ }
+ }
+ ures_close(bundle);
+ }
+ return code;
+}
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof
/*
**********************************************************************
-* Copyright (c) 2002-2011, International Business Machines
+* Copyright (c) 2002-2012, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
UBool commonlyUsed,
UErrorCode* status);
+
+/**
+ * Returns the ISO 4217 numeric code for the currency.
+ * <p>Note: If the ISO 4217 numeric code is not assigned for the currency or
+ * the currency is unknown, this function returns 0.
+ *
+ * @param currency null-terminated 3-letter ISO 4217 code
+ * @return The ISO 4217 numeric code of the currency
+ * @draft ICU 49
+ */
+U_DRAFT int32_t U_EXPORT2
+ucurr_getNumericCode(const UChar* currency);
+
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 2005-2009, International Business Machines Corporation and
+ * Copyright (c) 2005-2012, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
unum_close(parser);
}
+typedef struct {
+ const char* alphaCode;
+ int32_t numericCode;
+} NumCodeTestEntry;
+
+static const NumCodeTestEntry NUMCODE_TESTDATA[] = {
+ {"USD", 840},
+ {"Usd", 840}, /* mixed casing */
+ {"EUR", 978},
+ {"JPY", 392},
+ {"XFU", 0}, /* XFU: no numeric code */
+ {"ZZZ", 0}, /* ZZZ: undefined ISO currency code */
+ {"bogus", 0}, /* bogus code */
+ {0, 0},
+};
+
+static void TestNumericCode(void) {
+ UChar code[4];
+ int32_t i;
+ int32_t numCode;
+
+ for (i = 0; NUMCODE_TESTDATA[i].alphaCode; i++) {
+ u_charsToUChars(NUMCODE_TESTDATA[i].alphaCode, code, sizeof(code)/sizeof(code[0]));
+ numCode = ucurr_getNumericCode(code);
+ if (numCode != NUMCODE_TESTDATA[i].numericCode) {
+ log_err("Error: ucurr_getNumericCode returned %d for currency %s, expected - %d\n",
+ numCode, NUMCODE_TESTDATA[i].alphaCode, NUMCODE_TESTDATA[i].numericCode);
+ }
+ }
+}
+
void addCurrencyTest(TestNode** root);
#define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x)
TESTCASE(TestEnumListCount);
TESTCASE(TestFractionDigitOverride);
TESTCASE(TestPrefixSuffix);
+ TESTCASE(TestNumericCode);
}
#endif /* #if !UCONFIG_NO_FORMATTING */