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