sl@0: /* sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include "FLDSTD.H" sl@0: sl@0: #include "FLDNUMS.H" sl@0: sl@0: LOCAL_D const TInt sDeneryArray[13] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; sl@0: LOCAL_D const TText* const LsRomanArray[13] = { _S("M"), _S("CM"), _S("D"), _S("CD"),_S("C"), _S("XC"),_S("L"),_S("XL"),_S("X"),_S("IX"),_S("V"),_S("IV"),_S("I") }; sl@0: sl@0: ////////////////////////////// sl@0: // TDeneryToCharBase sl@0: ////////////////////////////// sl@0: sl@0: EXPORT_C TInt TDeneryToCharBase::DeneryToChar(TPtr& aValueText,TInt aDenary)const sl@0: // if aValueText is big enough to hold the result then the Roman Numeral representation of aDenery is inserted sl@0: // else the required size of aValueText is returned sl@0: // sl@0: { sl@0: aValueText.SetLength(0); sl@0: TInt numChars = NumChars(aDenary); sl@0: if (aValueText.MaxLength() < numChars) sl@0: return numChars; sl@0: else sl@0: { sl@0: DoDeneryToChar(aValueText,aDenary); sl@0: return KErrNone; sl@0: } sl@0: } sl@0: sl@0: sl@0: ////////////////////////////// sl@0: // TRomanNumeral sl@0: ////////////////////////////// sl@0: sl@0: EXPORT_C TInt TRomanNumeral::NumChars(TInt aDenery)const sl@0: // returns the number of characters in a given "roman number" sl@0: // sl@0: { sl@0: TInt numChars=0; sl@0: TInt i=0; sl@0: sl@0: while (aDenery > 0) sl@0: { sl@0: if (sDeneryArray[i] <= aDenery) sl@0: { sl@0: numChars += _L(sRomanArray[i]).Length(); sl@0: aDenery -= sDeneryArray[i]; sl@0: } sl@0: else sl@0: i++; sl@0: } sl@0: sl@0: return numChars; sl@0: } sl@0: sl@0: sl@0: EXPORT_C void TRomanNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const sl@0: // sets aValueText to the Roman Numeral representation of aDenery sl@0: // sl@0: { sl@0: aValueText.SetLength(0); sl@0: TInt i=0; sl@0: sl@0: while (aDenery > 0) sl@0: { sl@0: if (sDeneryArray[i] <= aDenery) sl@0: { sl@0: aValueText.Append(_L(sRomanArray[i])); sl@0: aDenery -= sDeneryArray[i]; sl@0: } sl@0: else sl@0: i++; sl@0: } sl@0: } sl@0: sl@0: sl@0: ///////////////////////// sl@0: // TArabicNumeral sl@0: ///////////////////////// sl@0: sl@0: EXPORT_C TInt TArabicNumeral::NumChars(TInt aNum)const sl@0: // returns num digits in an integer (including sign bit if negative) sl@0: // sl@0: { sl@0: TInt chars=1; sl@0: if (aNum<0) sl@0: { sl@0: chars++; sl@0: aNum = aNum*(-1); sl@0: } sl@0: while (aNum>=10) sl@0: { sl@0: aNum = aNum/10; sl@0: chars++; sl@0: } sl@0: return chars; sl@0: } sl@0: sl@0: sl@0: EXPORT_C void TArabicNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const sl@0: { sl@0: aValueText.Num(aDenery); sl@0: } sl@0: sl@0: sl@0: ///////////////////////// sl@0: // TAlphabeticNumeral sl@0: ///////////////////////// sl@0: sl@0: EXPORT_C TInt TAlphabeticNumeral::NumChars(TInt aNum)const sl@0: // returns num digits in an integer (including sign bit if negative) sl@0: // sl@0: { sl@0: TInt chars=1; sl@0: if (aNum<0) sl@0: { sl@0: chars++; sl@0: aNum = aNum*(-1); sl@0: } sl@0: while (aNum>26) sl@0: { sl@0: aNum = aNum/26; sl@0: chars++; sl@0: } sl@0: return chars; sl@0: } sl@0: sl@0: sl@0: EXPORT_C void TAlphabeticNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const sl@0: { sl@0: TInt digit=0; sl@0: while (aDenery>0) sl@0: { sl@0: digit = aDenery%26; // remainder gives least significant figure sl@0: PropendDigitAsChar(aValueText,digit); sl@0: aDenery -= digit; sl@0: aDenery = aDenery/26; sl@0: } sl@0: } sl@0: sl@0: sl@0: void TAlphabeticNumeral::PropendDigitAsChar(TPtr& aValueText,TInt aDigit)const sl@0: { sl@0: __ASSERT_ALWAYS(aDigit>0,Panic(ECharOutOfRange)); sl@0: __ASSERT_ALWAYS(aDigit<27,Panic(ECharOutOfRange)); sl@0: __ASSERT_ALWAYS(aValueText.Length() buf; sl@0: buf.Append(TChar(aDigit+64)); sl@0: aValueText.Insert(0,buf); // insert at position zero, ie propend sl@0: } sl@0: sl@0: