1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ********************************************************************** 5 * Copyright (c) 2004-2014, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ********************************************************************** 8 * Author: Alan Liu 9 * Created: April 26, 2004 10 * Since: ICU 3.0 11 ********************************************************************** 12 */ 13 #include "unicode/utypes.h" 14 15 #if !UCONFIG_NO_FORMATTING 16 17 #include "unicode/currunit.h" 18 #include "unicode/ustring.h" 19 #include "cstring.h" 20 21 U_NAMESPACE_BEGIN 22 23 CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) { 24 *isoCode = 0; 25 if (U_SUCCESS(ec)) { 26 if (_isoCode != nullptr && u_strlen(_isoCode)==3) { 27 u_strcpy(isoCode, _isoCode); 28 char simpleIsoCode[4]; 29 u_UCharsToChars(isoCode, simpleIsoCode, 4); 30 initCurrency(simpleIsoCode); 31 } else { 32 ec = U_ILLEGAL_ARGUMENT_ERROR; 33 } 34 } 35 } 36 37 CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) { 38 u_strcpy(isoCode, other.isoCode); 39 } 40 41 CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) { 42 // Make sure this is a currency. 43 // OK to hard-code the string because we are comparing against another hard-coded string. 44 if (uprv_strcmp("currency", getType()) != 0) { 45 ec = U_ILLEGAL_ARGUMENT_ERROR; 46 isoCode[0] = 0; 47 } else { 48 // Get the ISO Code from the subtype field. 49 u_charsToUChars(getSubtype(), isoCode, 4); 50 isoCode[3] = 0; // make 100% sure it is NUL-terminated 51 } 52 } 53 54 CurrencyUnit::CurrencyUnit() : MeasureUnit() { 55 u_strcpy(isoCode, u"XXX"); 56 char simpleIsoCode[4]; 57 u_UCharsToChars(isoCode, simpleIsoCode, 4); 58 initCurrency(simpleIsoCode); 59 } 60 61 CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) { 62 if (this == &other) { 63 return *this; 64 } 65 MeasureUnit::operator=(other); 66 u_strcpy(isoCode, other.isoCode); 67 return *this; 68 } 69 70 UObject* CurrencyUnit::clone() const { 71 return new CurrencyUnit(*this); 72 } 73 74 CurrencyUnit::~CurrencyUnit() { 75 } 76 77 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit) 78 79 U_NAMESPACE_END 80 81 #endif // !UCONFIG_NO_FORMATTING 82