FreeBASIC  0.91.0
str_convfrom.c
Go to the documentation of this file.
1 /* val function */
2 
3 #include "fb.h"
4 
5 FBCALL double fb_hStr2Double( char *src, ssize_t len )
6 {
7  char *p, *q, c;
8  int radix, i, skip;
9  double ret;
10 
11  /* skip white spc */
12  p = fb_hStrSkipChar( src, len, 32 );
13 
14  len -= (ssize_t)(p - src);
15  if( len < 1 )
16  return 0.0;
17 
18  else if( (len >= 2) && (p[0] == '&') )
19  {
20  skip = 2;
21  radix = 0;
22  switch( p[1] )
23  {
24  case 'h':
25  case 'H':
26  radix = 16;
27  break;
28  case 'o':
29  case 'O':
30  radix = 8;
31  break;
32  case 'b':
33  case 'B':
34  radix = 2;
35  break;
36 
37  default: /* assume octal */
38  radix = 8;
39  skip = 1;
40  break;
41  }
42 
43  if( radix != 0 )
44  return (double)fb_hStrRadix2Longint( &p[skip], len - skip, radix );
45  }
46 
47  /* Workaround: strtod() does not allow 'd' as an exponent specifier on
48  * non-win32 platforms, so create a temporary buffer and replace any
49  * 'd's with 'e'
50  */
51  q = malloc( len + 1 );
52  for( i = 0; i < len; i++ )
53  {
54  c = p[i];
55  if( c == 'd' || c == 'D' )
56  ++c;
57  q[i] = c;
58  }
59  q[len] = '\0';
60 
61  ret = strtod( q, NULL );
62  free( q );
63 
64  return ret;
65 }
66 
67 FBCALL double fb_VAL ( FBSTRING *str )
68 {
69  double val;
70 
71  if( str == NULL )
72  return 0.0;
73 
74  if( (str->data == NULL) || (FB_STRSIZE( str ) == 0) )
75  val = 0.0;
76  else
77  val = fb_hStr2Double( str->data, FB_STRSIZE( str ) );
78 
79  /* del if temp */
80  fb_hStrDelTemp( str );
81 
82  return val;
83 }