Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /**
0002  * @file
0003  *
0004  * @ingroup libmisc_conv_help Conversion Helpers
0005  *
0006  * @brief Convert String to Double (with validation)
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 2009.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  *  Copyright (c) 2011  Ralf Corsépius, Ulm, Germany.
0014  *
0015  *  The license and distribution terms for this file may be
0016  *  found in the file LICENSE in this distribution or at
0017  *  http://www.rtems.org/license/LICENSE.
0018  */
0019 
0020 #ifdef HAVE_CONFIG_H
0021 #include "config.h"
0022 #endif
0023 
0024 #include <errno.h>
0025 #include <stdlib.h>
0026 #include <math.h>
0027 
0028 #include <rtems/stringto.h>
0029 
0030 /*
0031  *  Instantiate an error checking wrapper for strtod (double)
0032  */
0033 
0034 rtems_status_code rtems_string_to_double (
0035   const char *s,
0036   double *n,
0037   char **endptr
0038 )
0039 {
0040   double result;
0041   char *end;
0042 
0043   if ( !n )
0044     return RTEMS_INVALID_ADDRESS;
0045 
0046   errno = 0;
0047   *n = 0;
0048 
0049   result = strtod( s, &end );
0050 
0051   if ( endptr )
0052     *endptr = end;
0053 
0054   if ( end == s )
0055     return RTEMS_NOT_DEFINED;
0056 
0057   if ( ( errno == ERANGE ) &&
0058     (( result == 0 ) || ( result == HUGE_VAL ) || ( result == -HUGE_VAL )))
0059       return RTEMS_INVALID_NUMBER;
0060 
0061   *n = result;
0062 
0063   return RTEMS_SUCCESSFUL;
0064 }