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