1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDLIB/ATOF.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,120 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// FUNCTION
1.18 +// <<atof>>, <<atoff>>---string to double or float
1.19 +// INDEX
1.20 +// atof
1.21 +// INDEX
1.22 +// atoff
1.23 +// ANSI_SYNOPSIS
1.24 +//
1.25 +
1.26 +// #include <stdlib.h>
1.27 +// double atof(const char *<[s]>);
1.28 +// float atoff(const char *<[s]>);
1.29 +// TRAD_SYNOPSIS
1.30 +// #include <stdlib.h>
1.31 +// double atof(<[s]>)
1.32 +// char *<[s]>;
1.33 +// float atoff(<[s]>)
1.34 +// char *<[s]>;
1.35 +// <<atof>> converts the initial portion of a string to a <<double>>.
1.36 +// <<atoff>> converts the initial portion of a string to a <<float>>.
1.37 +// The functions parse the character string <[s]>,
1.38 +// locating a substring which can be converted to a floating point
1.39 +// value. The substring must match the format:
1.40 +// . [+|-]<[digits]>[.][<[digits]>][(e|E)[+|-]<[digits]>]
1.41 +// The substring converted is the longest initial
1.42 +// fragment of <[s]> that has the expected format, beginning with
1.43 +// the first non-whitespace character. The substring
1.44 +// is empty if <<str>> is empty, consists entirely
1.45 +// of whitespace, or if the first non-whitespace character is
1.46 +// something other than <<+>>, <<->>, <<.>>, or a digit.
1.47 +// <<atof(<[s]>)>> is implemented as <<strtod(<[s]>, NULL)>>.
1.48 +// <<atoff(<[s]>)>> is implemented as <<strtodf(<[s]>, NULL)>>.
1.49 +// RETURNS
1.50 +// <<atof>> returns the converted substring value, if any, as a
1.51 +// <<double>>; or <<0.0>>, if no conversion could be performed.
1.52 +// If the correct value is out of the range of representable values, plus
1.53 +// or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is stored in
1.54 +// <<errno>>.
1.55 +// If the correct value would cause underflow, <<0.0>> is returned
1.56 +// and <<ERANGE>> is stored in <<errno>>.
1.57 +// <<atoff>> obeys the same rules as <<atof>>, except that it
1.58 +// returns a <<float>>.
1.59 +// PORTABILITY
1.60 +// <<atof>> is ANSI C. <<atof>>, <<atoi>>, and <<atol>> are subsumed by <<strtod>>
1.61 +// and <<strtol>>, but are used extensively in existing code. These functions are
1.62 +// less reliable, but may be faster if the argument is verified to be in a valid
1.63 +// range.
1.64 +// Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.65 +// <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.66 +//
1.67 +
1.68 +#include <e32std.h>
1.69 +#include <stdlib.h>
1.70 +#include <errno.h>
1.71 +#include <math.h>
1.72 +#include "../INC/ESTLIB.H"
1.73 +extern "C" {
1.74 +
1.75 +/**
1.76 +Convert string to double-precision floating-point value.
1.77 +Parses string interpreting its content as a floating-point value
1.78 +until a character that can not be interpreted is found,
1.79 +and returns a double precision value.
1.80 +@return The converted double value from the input string.
1.81 +If an error occurs 0 is returned.
1.82 +@param s String representing a floating point number.
1.83 +@param tail Address of a pointer.
1.84 +This is filled by the function with the address where scan has ended.
1.85 +Serves to determine where there is the first non-numerical character in the string.
1.86 +*/
1.87 +EXPORT_C double strtod (const char *s, char** tail) __SOFTFP
1.88 + {
1.89 + TLex8 data=(unsigned char *)s;
1.90 + TReal64 ret=0.0;
1.91 + data.SkipSpace();
1.92 + TBool minus = data.Peek() == '-';
1.93 + TInt r=data.Val(ret);
1.94 + if (r==KErrOverflow)
1.95 + {
1.96 + errno=ERANGE;
1.97 + if (minus)
1.98 + ret = -HUGE_VAL;
1.99 + else
1.100 + ret = HUGE_VAL;
1.101 + }
1.102 + else if (r==KErrUnderflow)
1.103 + errno=ERANGE;
1.104 + if (tail)
1.105 + *tail=(char*)(s+data.Offset());
1.106 + return ret;
1.107 + }
1.108 +
1.109 +/**
1.110 +Convert string to double.
1.111 +Parses string interpreting its content as a floating point number
1.112 +and returns a value of type double.
1.113 +@return The converted floating point value of the input string.
1.114 + On overflow the result is undefined.
1.115 + If an error occurs 0.0 is returned.
1.116 +@param s String representing a floating point number.
1.117 +*/
1.118 +EXPORT_C double atof (const char *s) __SOFTFP
1.119 + {
1.120 + return strtod(s,NULL);
1.121 + }
1.122 +
1.123 +} // extern "C"