textrendering/texthandling/sfields/FLDNUMS.CPP
changeset 0 1fb32624e06b
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include "FLDSTD.H"
       
    21 
       
    22 #include "FLDNUMS.H"
       
    23 
       
    24 LOCAL_D const TInt sDeneryArray[13] = {  1000, 900,  500, 400, 100, 90,  50, 40,  10, 9,   5,  4,   1  };
       
    25 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") };
       
    26 
       
    27 //////////////////////////////
       
    28 // TDeneryToCharBase
       
    29 //////////////////////////////
       
    30 
       
    31 EXPORT_C TInt TDeneryToCharBase::DeneryToChar(TPtr& aValueText,TInt aDenary)const
       
    32 // if aValueText is big enough to hold the result then the Roman Numeral representation of aDenery is inserted
       
    33 // else the required size of aValueText is returned
       
    34 //
       
    35 	{
       
    36 	aValueText.SetLength(0);
       
    37 	TInt numChars = NumChars(aDenary);
       
    38 	if (aValueText.MaxLength() < numChars)
       
    39 		return numChars;
       
    40 	else 
       
    41 		{
       
    42 		DoDeneryToChar(aValueText,aDenary);
       
    43 		return KErrNone;
       
    44 		}
       
    45 	}
       
    46 
       
    47 
       
    48 //////////////////////////////
       
    49 // TRomanNumeral
       
    50 //////////////////////////////
       
    51 
       
    52 EXPORT_C TInt TRomanNumeral::NumChars(TInt aDenery)const
       
    53 // returns the number of characters in a given "roman number"
       
    54 //
       
    55 	{
       
    56 	TInt numChars=0;
       
    57 	TInt i=0;
       
    58 
       
    59 	while (aDenery > 0)
       
    60 		{
       
    61 		if (sDeneryArray[i] <= aDenery)
       
    62 			{
       
    63 			numChars += _L(sRomanArray[i]).Length();
       
    64 			aDenery -= sDeneryArray[i];
       
    65 			}
       
    66 		else
       
    67 			i++;
       
    68 		}
       
    69 
       
    70 	return numChars;
       
    71 	}
       
    72 
       
    73 
       
    74 EXPORT_C void TRomanNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const
       
    75 // sets aValueText to the Roman Numeral representation of aDenery
       
    76 //
       
    77 	{
       
    78 	aValueText.SetLength(0);
       
    79 	TInt i=0;
       
    80 
       
    81 	while (aDenery > 0)
       
    82 		{
       
    83 		if (sDeneryArray[i] <= aDenery)
       
    84 			{
       
    85 			aValueText.Append(_L(sRomanArray[i]));
       
    86 			aDenery -= sDeneryArray[i];
       
    87 			}
       
    88 		else
       
    89 			i++;
       
    90 		}
       
    91 	}
       
    92 
       
    93 
       
    94 /////////////////////////
       
    95 // TArabicNumeral
       
    96 /////////////////////////
       
    97 
       
    98 EXPORT_C TInt TArabicNumeral::NumChars(TInt aNum)const
       
    99 // returns num digits in an integer (including sign bit if negative)
       
   100 //
       
   101 	{
       
   102 	TInt chars=1;
       
   103 	if (aNum<0)
       
   104 		{
       
   105 		chars++;
       
   106 		aNum = aNum*(-1);
       
   107 		}
       
   108 	while (aNum>=10)
       
   109 		{
       
   110 		aNum = aNum/10;
       
   111 		chars++;
       
   112 		}
       
   113 	return chars;
       
   114 	}
       
   115 
       
   116 
       
   117 EXPORT_C void TArabicNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const
       
   118 	{
       
   119 	aValueText.Num(aDenery);
       
   120 	}
       
   121 
       
   122 
       
   123 /////////////////////////
       
   124 // TAlphabeticNumeral
       
   125 /////////////////////////
       
   126 
       
   127 EXPORT_C TInt TAlphabeticNumeral::NumChars(TInt aNum)const
       
   128 // returns num digits in an integer (including sign bit if negative)
       
   129 //
       
   130 	{
       
   131 	TInt chars=1;
       
   132 	if (aNum<0)
       
   133 		{
       
   134 		chars++;
       
   135 		aNum = aNum*(-1);
       
   136 		}
       
   137 	while (aNum>26)
       
   138 		{
       
   139 		aNum = aNum/26;
       
   140 		chars++;
       
   141 		}
       
   142 	return chars;
       
   143 	}
       
   144 
       
   145 
       
   146 EXPORT_C void TAlphabeticNumeral::DoDeneryToChar(TPtr& aValueText,TInt aDenery)const
       
   147 	{
       
   148 	TInt digit=0;
       
   149 	while (aDenery>0)
       
   150 		{
       
   151 		digit = aDenery%26; // remainder gives least significant figure
       
   152 		PropendDigitAsChar(aValueText,digit);
       
   153 		aDenery -= digit;
       
   154 		aDenery = aDenery/26;
       
   155 		}
       
   156 	}
       
   157 
       
   158 
       
   159 void TAlphabeticNumeral::PropendDigitAsChar(TPtr& aValueText,TInt aDigit)const
       
   160 	{
       
   161 	__ASSERT_ALWAYS(aDigit>0,Panic(ECharOutOfRange));
       
   162 	__ASSERT_ALWAYS(aDigit<27,Panic(ECharOutOfRange));
       
   163 	__ASSERT_ALWAYS(aValueText.Length()<aValueText.MaxLength(),Panic(EBufferFull));
       
   164 
       
   165 	TBuf<1> buf;
       
   166 	buf.Append(TChar(aDigit+64));
       
   167 	aValueText.Insert(0,buf); // insert at position zero, ie propend
       
   168 	}
       
   169 
       
   170