ICU-1097 rename currency.* to ucurr.*

X-SVN-Rev: 9048
This commit is contained in:
Alan Liu 2002-07-08 17:47:01 +00:00
parent 61c7a84b5e
commit 62039179f9
2 changed files with 0 additions and 306 deletions

View File

@ -1,201 +0,0 @@
/*
**********************************************************************
* Copyright (c) 2002, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
#include "unicode/currency.h"
#include "cstring.h"
#include "unicode/locid.h"
#include "unicode/resbund.h"
//------------------------------------------------------------
// Constants
// Default currency meta data of last resort. We try to use the
// defaults encoded in the meta data resource bundle. If there is a
// configuration/build error and these are not available, we use these
// hard-coded defaults (which should be identical).
static const int32_t LAST_RESORT_DATA[] = { 2, 0 };
// POW10[i] = 10^i, i=0..MAX_POW10
static const int32_t POW10[] = { 1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000 };
static const int32_t MAX_POW10 = (sizeof(POW10)/sizeof(POW10[0])) - 1;
//------------------------------------------------------------
// Resource tags
// Tag for meta-data, in root.
static const char CURRENCY_META[] = "CurrencyMeta";
// Tag for default meta-data, in CURRENCY_META
static const char DEFAULT_META[] = "DEFAULT";
// Tag for legacy currency elements data
static const char CURRENCY_ELEMENTS[] = "CurrencyElements";
// Tag for localized display names (symbols) of currencies
static const char CURRENCIES[] = "Currencies";
//------------------------------------------------------------
// Code
/**
* Internal function to look up currency data. Result is an array of
* two integers. The first is the fraction digits. The second is the
* rounding increment, or 0 if none. The rounding increment is in
* units of 10^(-fraction_digits).
*/
static const int32_t*
_findData(const char* currency) {
// Get CurrencyMeta resource out of root locale file. [This may
// move out of the root locale file later; if it does, update this
// code.]
UErrorCode ec = U_ZERO_ERROR;
ResourceBundle currencyMeta =
ResourceBundle((char*)0, Locale(""), ec).get(CURRENCY_META, ec);
if (U_FAILURE(ec)) {
// Config/build error; return hard-coded defaults
return LAST_RESORT_DATA;
}
// Look up our currency, or if that's not available, then DEFAULT
ResourceBundle rb = currencyMeta.get(currency, ec);
if (U_FAILURE(ec)) {
rb = currencyMeta.get(DEFAULT_META, ec);
if (U_FAILURE(ec)) {
// Config/build error; return hard-coded defaults
return LAST_RESORT_DATA;
}
}
int32_t len;
const int32_t *data = rb.getIntVector(len, ec);
if (U_FAILURE(ec) || len < 2) {
// Config/build error; return hard-coded defaults
return LAST_RESORT_DATA;
}
return data;
}
U_CAPI void U_EXPORT2
ucurr_forLocale(const char* locale,
char* result,
UErrorCode* ec) {
// TODO: ? Establish separate resource for locale->currency mapping
// ? <IF> we end up deleting the CurrencyElements resource.
// ? In the meantime the CurrencyElements tag has exactly the
// ? data we want.
// Look up the CurrencyElements resource for this locale.
// It contains: [0] = currency symbol, e.g. "$";
// [1] = intl. currency symbol, e.g. "USD";
// [2] = monetary decimal separator, e.g. ".".
if (ec != NULL && U_SUCCESS(*ec)) {
ResourceBundle rb((char*)0, Locale(locale), *ec);
UnicodeString s = rb.get(CURRENCY_ELEMENTS, *ec).getStringEx(1, *ec);
if (U_SUCCESS(*ec)) {
s.extract(0, 3, result, "");
return;
}
}
result[0] = 0;
}
U_NAMESPACE_BEGIN
/**
* Rather than convert a UnicodeString to a UChar array and then back
* to a UnicodeString, possibly with allocation of UChar space on the
* heap, we use this function in DecimalFormat.
*
* This is internal to ICU, but exported by this file. Note that it
* is C++ API even though it's named like a C function.
*
* @internal
*/
UnicodeString
ucurr_getSymbolAsUnicodeString(const char* currency,
const Locale& locale) {
// Look up the Currencies resource for the given locale. The
// Currencies locale data looks like this:
//|en {
//| Currencies {
//| USD { "$" }
//| CHF { "sFr" }
//| //...
//| }
//|}
UErrorCode ec = U_ZERO_ERROR;
ResourceBundle rb((char*)0, locale, ec);
UnicodeString s = rb.get(CURRENCIES, ec).getStringEx(currency, ec);
UBool found = U_SUCCESS(ec);
if (!found) {
// Since the Currencies resource is not fully populated yet,
// check to see if we can find the currency in the
// CurrencyElements resource.
ec = U_ZERO_ERROR;
ResourceBundle ce = rb.get(CURRENCY_ELEMENTS, ec);
UnicodeString curr(currency, "");
if (ce.getStringEx(1, ec) == curr) {
s = ce.getStringEx((int32_t)0, ec);
found = U_SUCCESS(ec);
}
if (!found) {
// If we fail to find a match, use the full ISO code
s = curr;
}
}
return s;
}
U_NAMESPACE_END
U_CAPI int32_t U_EXPORT2
ucurr_getSymbol(const char* currency,
UChar* result,
int32_t resultCapacity,
const char* locale,
UErrorCode* ec) {
if (ec == NULL || U_FAILURE(*ec)) {
return 0;
}
UnicodeString s = ucurr_getSymbolAsUnicodeString(currency, Locale(locale));
return s.extract(result, resultCapacity, *ec);
}
U_CAPI int32_t U_EXPORT2
ucurr_getDefaultFractionDigits(const char* currency) {
return (_findData(currency))[0];
}
U_CAPI double U_EXPORT2
ucurr_getRoundingIncrement(const char* currency) {
const int32_t *data = _findData(currency);
// If there is no rounding, or if the meta data is invalid,
// return 0.0 to indicate no rounding.
if (data[1] == 0 || data[0] < 0 || data[0] > MAX_POW10) {
return 0.0;
}
// Return data[1] / 10^(data[0]). The only actual rounding data,
// as of this writing, is CHF { 2, 25 }.
return double(data[1]) / POW10[data[0]];
}
//eof

View File

@ -1,105 +0,0 @@
/*
**********************************************************************
* Copyright (c) 2002, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* $Source: /xsrl/Nsvn/icu/icu/source/i18n/unicode/Attic/currency.h,v $
* $Revision: 1.4 $
**********************************************************************
*/
#ifndef UCURRENCY_H
#define UCURRENCY_H
#include "unicode/utypes.h"
/**
* The ucurr API encapsulates information about a currency, as defined by
* ISO 4217. A currency is represented by a 3-character string
* containing its ISO 4217 code. This API can return various data
* necessary the proper display of a currency:
*
* <ul><li>A display symbol, for a specific locale
* <li>The number of fraction digits to display
* <li>A rounding increment
* </ul>
*
* The <tt>DecimalFormat</tt> class uses these data to display
* currencies.
* @author Alan Liu
* @since ICU 2.2
*/
/**
* Returns a currency code for the default currency in the given
* locale.
* @param locale the locale for which to retrieve a currency code
* @param result pointer to a buffer of at least 4 chars
* to receive a null-terminated 3-letter ISO 4217 code
* @param ec error code
*/
U_CAPI void U_EXPORT2
ucurr_forLocale(const char* locale,
char* result,
UErrorCode* ec);
/**
* Returns the display string for the given currency in the
* given locale. For example, the display string for the USD
* currency object in the en_US locale is "$".
* @param currency null-terminated 3-letter ISO 4217 code
* @param result pointer to result buffer, or NULL
* @param resultCapacity size of result buffer
* @param locale locale in which to display currency
* @param ec error code
* @return size of result, possibly larger than resultCapacity
*/
U_CAPI int32_t U_EXPORT2
ucurr_getSymbol(const char* currency,
UChar* result,
int32_t resultCapacity,
const char* locale,
UErrorCode* ec);
/**
* Returns the number of the number of fraction digits that should
* be displayed for the given currency.
* @return a non-negative number of fraction digits to be
* displayed
*/
U_CAPI int32_t U_EXPORT2
ucurr_getDefaultFractionDigits(const char* currency);
/**
* Returns the rounding increment for the given currency, or 0.0 if no
* rounding is done by the currency.
* @return the non-negative rounding increment, or 0.0 if none
*/
U_CAPI double U_EXPORT2
ucurr_getRoundingIncrement(const char* currency);
/*------------------------------------------------------------------*/
/* Internal API. Do not use. Will change without notice. */
#ifdef XP_CPLUSPLUS
#include "unicode/unistr.h"
U_NAMESPACE_BEGIN
class Locale;
/**
* Do not use; for ICU internal use only. This is a C++ function
* even though it's named like a C function.
*
* @internal
*/
UnicodeString ucurr_getSymbolAsUnicodeString(const char* currency,
const Locale& locale);
U_NAMESPACE_END
#endif
#endif