genericopenlibs/cstdlib/LSTDLIB/ATOF.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // FUNCTION
       
    15 // <<atof>>, <<atoff>>---string to double or float
       
    16 // INDEX
       
    17 // atof
       
    18 // INDEX
       
    19 // atoff
       
    20 // ANSI_SYNOPSIS
       
    21 //
       
    22 
       
    23 // #include <stdlib.h>
       
    24 // double atof(const char *<[s]>);
       
    25 // float atoff(const char *<[s]>);
       
    26 // TRAD_SYNOPSIS
       
    27 // #include <stdlib.h>
       
    28 // double atof(<[s]>)
       
    29 // char *<[s]>;
       
    30 // float atoff(<[s]>)
       
    31 // char *<[s]>;
       
    32 // <<atof>> converts the initial portion of a string to a <<double>>.
       
    33 // <<atoff>> converts the initial portion of a string to a <<float>>.
       
    34 // The functions parse the character string <[s]>,
       
    35 // locating a substring which can be converted to a floating point
       
    36 // value. The substring must match the format:
       
    37 // . [+|-]<[digits]>[.][<[digits]>][(e|E)[+|-]<[digits]>]
       
    38 // The substring converted is the longest initial
       
    39 // fragment of <[s]> that has the expected format, beginning with
       
    40 // the first non-whitespace character.  The substring
       
    41 // is empty if <<str>> is empty, consists entirely
       
    42 // of whitespace, or if the first non-whitespace character is
       
    43 // something other than <<+>>, <<->>, <<.>>, or a digit.
       
    44 // <<atof(<[s]>)>> is implemented as <<strtod(<[s]>, NULL)>>.
       
    45 // <<atoff(<[s]>)>> is implemented as <<strtodf(<[s]>, NULL)>>.
       
    46 // RETURNS
       
    47 // <<atof>> returns the converted substring value, if any, as a
       
    48 // <<double>>; or <<0.0>>,  if no conversion could be performed.
       
    49 // If the correct value is out of the range of representable values, plus
       
    50 // or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is stored in
       
    51 // <<errno>>.
       
    52 // If the correct value would cause underflow, <<0.0>> is returned
       
    53 // and <<ERANGE>> is stored in <<errno>>.
       
    54 // <<atoff>> obeys the same rules as <<atof>>, except that it
       
    55 // returns a <<float>>.
       
    56 // PORTABILITY
       
    57 // <<atof>> is ANSI C. <<atof>>, <<atoi>>, and <<atol>> are subsumed by <<strtod>>
       
    58 // and <<strtol>>, but are used extensively in existing code. These functions are
       
    59 // less reliable, but may be faster if the argument is verified to be in a valid
       
    60 // range.
       
    61 // Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
       
    62 // <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
       
    63 //
       
    64 
       
    65 #include <e32std.h>
       
    66 #include <stdlib.h>
       
    67 #include <errno.h>
       
    68 #include <math.h>
       
    69 #include "../INC/ESTLIB.H"
       
    70 extern "C" {
       
    71 	
       
    72 /**
       
    73 Convert string to double-precision floating-point value.
       
    74 Parses string interpreting its content as a floating-point value
       
    75 until a character that can not be interpreted is found, 
       
    76 and returns a double precision value.
       
    77 @return   The converted double value from the input string.
       
    78 If an error occurs 0 is returned.
       
    79 @param s String representing a floating point number. 
       
    80 @param tail Address of a pointer. 
       
    81 This is filled by the function with the address where scan has ended. 
       
    82 Serves to determine where there is the first non-numerical character in the string.
       
    83 */
       
    84 EXPORT_C double strtod (const char *s, char** tail) __SOFTFP
       
    85 	{
       
    86 	TLex8 data=(unsigned char *)s;
       
    87 	TReal64 ret=0.0;
       
    88 	data.SkipSpace();
       
    89 	TBool minus = data.Peek() == '-';
       
    90 	TInt r=data.Val(ret);
       
    91 	if (r==KErrOverflow)
       
    92 		{
       
    93 		errno=ERANGE;
       
    94 		if (minus)
       
    95 			ret = -HUGE_VAL;
       
    96 		else
       
    97 			ret = HUGE_VAL;
       
    98 		}
       
    99 	else if (r==KErrUnderflow)
       
   100 		errno=ERANGE;
       
   101 	if (tail)
       
   102 		*tail=(char*)(s+data.Offset());
       
   103 	return ret;
       
   104 	}
       
   105 
       
   106 /**
       
   107 Convert string to double.
       
   108 Parses string interpreting its content as a floating point number 
       
   109 and returns a value of type double.
       
   110 @return   The converted floating point value of the input string.
       
   111   On overflow the result is undefined.
       
   112   If an error occurs 0.0 is returned.
       
   113 @param s String representing a floating point number. 
       
   114 */
       
   115 EXPORT_C double atof (const char *s) __SOFTFP
       
   116 	{
       
   117 	return strtod(s,NULL);
       
   118 	}
       
   119 
       
   120 } // extern "C"