symport/e32/euser/maths/um_dtor.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\euser\maths\um_dtor.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "um_std.h"
       
    19 
       
    20 
       
    21 void TLex8::Scndig(TInt &aSig,TInt &aExp,TInt64 &aDl)
       
    22 //
       
    23 // Scans a decimal digit field and accumulates the value to a TInt64 at aDl
       
    24 // Used before decimal point - do not drop trailing zeros.
       
    25 //
       
    26 	{
       
    27 
       
    28 	FOREVER
       
    29 		{
       
    30 		if (iNext>=iEnd)
       
    31 			break;
       
    32 		TChar c=Peek();
       
    33 		if (!c.IsDigit())
       
    34 			break;
       
    35 		else
       
    36 			c=Get();
       
    37 		if (aSig<KMaxPrecision+2)		
       
    38 			{
       
    39 			aDl *= 10;				// Multiply accumulator by 10
       
    40 			aDl+=((TUint)c)-'0';	// Add current digit
       
    41 			aSig++;
       
    42 			}
       
    43 		else
       
    44 			aExp++;
       
    45 		}														  
       
    46 	}
       
    47 
       
    48 void TLex8::ScndigAfterPoint(TInt &aSig,TInt64 &aDl)
       
    49 //
       
    50 // Scans a decimal digit field and accumulates the value to a TInt64 at aDl
       
    51 // Used after decimal point - drops trailing zeros.
       
    52 //
       
    53 // Could be improved with change to header file!!
       
    54 	{
       
    55 	TInt trailing=0;	// no of trailing zeros
       
    56 	TInt leading=0;		// no of leading zeros
       
    57 	TInt n;
       
    58 	TChar c;
       
    59 	
       
    60 	if (aDl==0)		
       
    61 		//	if no significance before decimal point need extra digit to lead to full significance
       
    62 		leading++;	
       
    63 
       
    64 	FOREVER
       
    65 		{
       
    66 		if (iNext>=iEnd)
       
    67 			break;
       
    68 		c=Peek();
       
    69 		if (!c.IsDigit())
       
    70 			break;
       
    71 		else
       
    72 			{
       
    73 			c=Get();
       
    74 			if (c=='0')
       
    75 				{
       
    76 				if (aDl!=0)		// possible trailing zeros
       
    77 					trailing++;	
       
    78 				else			// if aDl==0 multiplying by 10 and adding 0 has no effect  
       
    79 					{
       
    80 					leading++;
       
    81 					aSig++;		// leading zeros have significance
       
    82 					}
       
    83 				}	
       
    84 			else if ((aSig<KMaxPrecision+2+leading && !trailing) || (trailing && aSig+trailing+1<KMaxPrecision+2))
       
    85 				{
       
    86 				// first multiply, taking account of preceeding zeros
       
    87 				for (n=trailing; n>=0; n--)
       
    88 					{
       
    89 					aDl *= 10;			// Multiply accumulator by 10
       
    90 					}
       
    91 				// now add current digit
       
    92 				aDl+=((TUint)c)-'0';
       
    93 				// now update significant digits used
       
    94 				aSig+=trailing+1;
       
    95 				trailing=0;
       
    96 				}
       
    97 			}
       
    98 		}
       
    99 	}	
       
   100 
       
   101 void TLex16::Scndig(TInt &aSig,TInt &aExp,TInt64 &aDl)
       
   102 //
       
   103 // Scans a decimal digit field and accumulates the value to a TInt64 at aDl
       
   104 //
       
   105 	{
       
   106 
       
   107 	FOREVER
       
   108 		{
       
   109 		TChar c=Peek();
       
   110 		if (!c.IsDigit())
       
   111 			break;
       
   112 		else
       
   113 			c=Get();
       
   114 		if (aSig<KMaxPrecision+2)	
       
   115 			{
       
   116 			aDl *= 10;				// Multiply accumulator by 10
       
   117 			aDl+=((TUint)c)-'0';	// Add current digit
       
   118 			aSig++;
       
   119 			}
       
   120 		else
       
   121 			aExp++;
       
   122 		}														  
       
   123 	}
       
   124 
       
   125 EXPORT_C TInt TLex8::Val(TReal32& aVal)
       
   126 //
       
   127 // Convert a 32 bit real.
       
   128 //
       
   129 	{
       
   130 	TRealX x;
       
   131 	TInt r=Val(x);
       
   132 	if (r==KErrNone)
       
   133 		r=x.GetTReal(aVal);
       
   134 	return r;
       
   135 	}
       
   136 
       
   137 EXPORT_C TInt TLex8::Val(TReal32& aVal, TChar aPoint)
       
   138 //
       
   139 // Convert a 32 bit real.
       
   140 //
       
   141 	{
       
   142 	TRealX x;
       
   143 	TInt r=Val(x,aPoint);
       
   144 	if (r==KErrNone)
       
   145 		r=x.GetTReal(aVal);
       
   146 	return r;
       
   147 	}
       
   148 
       
   149 EXPORT_C TInt TLex8::Val(TReal64& aVal)
       
   150 //
       
   151 // Convert a 64 bit real.
       
   152 //
       
   153 	{
       
   154 	TRealX x;
       
   155 	TInt r=Val(x);
       
   156 	if (r==KErrNone)
       
   157 		r=x.GetTReal(aVal);
       
   158 	return r;
       
   159 	}
       
   160 
       
   161 EXPORT_C TInt TLex8::Val(TReal64& aVal, TChar aPoint)
       
   162 //
       
   163 // Convert a 64 bit real.
       
   164 //
       
   165 	{
       
   166 	TRealX x;
       
   167 	TInt r=Val(x,aPoint);
       
   168 	if (r==KErrNone)
       
   169 		r=x.GetTReal(aVal);
       
   170 	return r;
       
   171 	}
       
   172 
       
   173 TInt TLex8::Val(TRealX& aVal)
       
   174 //
       
   175 // Convert an extended real. Use the locale decimal point.
       
   176 //
       
   177 	{
       
   178 	TLocale locale;
       
   179 	return(Val(aVal,locale.DecimalSeparator()));
       
   180 	}
       
   181 
       
   182 TInt TLex8::Val(TRealX& aVal, TChar aPoint)
       
   183 //
       
   184 // Convert an extended real.
       
   185 //
       
   186 	{
       
   187 
       
   188 	TLexMark8 start(iNext);
       
   189 	if (iNext>=iEnd)
       
   190 		return(KErrGeneral);
       
   191 	TInt64 n(0);
       
   192 	TBool minus=EFalse;
       
   193 	if (Peek()=='-')
       
   194 		{
       
   195 		Inc();
       
   196 		minus=ETrue;
       
   197 		}
       
   198 	else if (Peek()=='+')
       
   199 		Inc();
       
   200 	TInt digflg=Peek().IsDigit();
       
   201 	while (Peek()=='0')		// Skip leading zeros
       
   202 		Inc();
       
   203 	TInt nsig=0;
       
   204 	TInt nskip=0;
       
   205 	Scndig(nsig,nskip,n);
       
   206 	TInt nint=nsig;
       
   207 	TInt nfract=0;
       
   208 	if (Peek()==aPoint)
       
   209 		{
       
   210 		Inc();
       
   211 		if (!digflg)
       
   212 			digflg=Peek().IsDigit();
       
   213 		ScndigAfterPoint(nsig,n);	// skip trailing zeros
       
   214 		nfract=nsig-nint;
       
   215 		}
       
   216 	if (!digflg)
       
   217 		{
       
   218 		UnGetToMark(start);
       
   219 		return(KErrGeneral);	// Not a number
       
   220 		}
       
   221 	TInt nexp=0;
       
   222 	TInt r;
       
   223 	if (Peek()=='E' || Peek()=='e')
       
   224 		{
       
   225 		TLexMark8 rollback(iNext);
       
   226 		Inc();
       
   227 		r=Val(nexp);
       
   228 		if (r!=KErrNone)
       
   229 			{
       
   230 			if (r==KErrOverflow)
       
   231 				{
       
   232 				aVal.SetInfinite(minus);
       
   233 				return r;
       
   234 				}
       
   235 			else
       
   236 				{
       
   237 				//it wasn't a number after the 'e', so rollback to the 'e'
       
   238 				UnGetToMark(rollback);
       
   239 				}
       
   240 			}
       
   241 		}
       
   242 
       
   243 	if (n == 0)
       
   244 		{
       
   245 		aVal.SetZero();
       
   246 		return KErrNone;
       
   247 		}
       
   248 
       
   249 	aVal=minus?-n:n;
       
   250 	nexp+=nskip-nfract;
       
   251 	r=Math::MultPow10X(aVal,nexp);
       
   252 	return r;
       
   253 	}
       
   254 
       
   255 EXPORT_C TInt TLex16::Val(TReal32& aVal)
       
   256 //
       
   257 // Convert a 32 bit real.
       
   258 //
       
   259 	{
       
   260 	TRealX x;
       
   261 	TInt r=Val(x);
       
   262 	if (r==KErrNone)
       
   263 		r=x.GetTReal(aVal);
       
   264 	return r;
       
   265 	}
       
   266 
       
   267 EXPORT_C TInt TLex16::Val(TReal32& aVal, TChar aPoint)
       
   268 //
       
   269 // Convert a 32 bit real.
       
   270 //
       
   271 	{
       
   272 	TRealX x;
       
   273 	TInt r=Val(x,aPoint);
       
   274 	if (r==KErrNone)
       
   275 		r=x.GetTReal(aVal);
       
   276 	return r;
       
   277 	}
       
   278 
       
   279 EXPORT_C TInt TLex16::Val(TReal64& aVal)
       
   280 //
       
   281 // Convert a 64 bit real.
       
   282 //
       
   283 	{
       
   284 	TRealX x;
       
   285 	TInt r=Val(x);
       
   286 	if (r==KErrNone)
       
   287 		r=x.GetTReal(aVal);
       
   288 	return r;
       
   289 	}
       
   290 
       
   291 EXPORT_C TInt TLex16::Val(TReal64& aVal, TChar aPoint)
       
   292 //
       
   293 // Convert a 64 bit real.
       
   294 //
       
   295 	{
       
   296 	TRealX x;
       
   297 	TInt r=Val(x,aPoint);
       
   298 	if (r==KErrNone)
       
   299 		r=x.GetTReal(aVal);
       
   300 	return r;
       
   301 	}
       
   302 
       
   303 TInt TLex16::Val(TRealX& aVal)
       
   304 //
       
   305 // Convert an extended real. Use the locale decimal point.
       
   306 //
       
   307 	{
       
   308 	TLocale locale;
       
   309 	return(Val(aVal,locale.DecimalSeparator()));
       
   310 	}
       
   311 
       
   312 TInt TLex16::Val(TRealX& aVal, TChar aPoint)
       
   313 //
       
   314 // Convert a 64 bit real
       
   315 //
       
   316 	{
       
   317 	
       
   318 	HBufC8 *temp=HBufC8::New(iEnd-iNext);
       
   319 	if (temp==NULL)
       
   320 		return(KErrNoMemory);
       
   321 	TPtr8 tdes(temp->Des());
       
   322 
       
   323 //	for (TUint8 *p=(TUint8*)iNext; p<(TUint8*)iEnd; p+=2)
       
   324 //		tdes.Append(*p);
       
   325 
       
   326 	for (const TText* p = (TText*)iNext; p < (TText*)iEnd; p++)
       
   327 		{
       
   328 		TChar c = *p;
       
   329 		if (c == aPoint)
       
   330 			c = '.';
       
   331 		else if (c == '.')
       
   332 			c = ' ';
       
   333 		else if (c > 255)
       
   334 			c = ' ';
       
   335 		tdes.Append((TUint8)c);
       
   336 		}
       
   337 	aPoint = '.';
       
   338 
       
   339 	TLex8 lex(tdes);
       
   340 	lex.Mark();
       
   341 	TInt r=lex.Val(aVal,aPoint);
       
   342 	User::Free(temp);
       
   343 	if (r==KErrNone)
       
   344 		Inc(lex.TokenLength());
       
   345 	return r;
       
   346 	}