Pol  Revision:cb584c9
escrutil.cpp
Go to the documentation of this file.
1 
8 #include "escrutil.h"
9 
10 #include <climits>
11 #include <cmath>
12 #include <ctype.h>
13 #include <stdlib.h>
14 
15 #include "bobject.h"
16 #include "impstr.h"
17 
18 namespace Pol
19 {
20 namespace Bscript
21 {
22 bool could_be_a_number( const char* s )
23 {
24  if ( s[0] == '0' && ( s[1] == 'x' || s[1] == 'X' ) ) // Hex number
25  {
26  s += 2;
27  while ( *s )
28  {
29  char ch = *s;
30  ++s;
31  if ( isxdigit( ch ) )
32  continue;
33  else if ( isspace( ch ) )
34  return true;
35  else
36  return false;
37  }
38  return true;
39  }
40  else // expect -, +, 0-9, . only
41  {
42  while ( *s )
43  {
44  char ch = *s;
45  ++s;
46  if ( ch == '-' || ch == '+' || ( ch >= '0' && ch <= '9' ) || ch == '.' )
47  {
48  continue;
49  }
50  else
51  {
52  return false;
53  }
54  }
55  return true;
56  }
57 }
58 
59 BObjectImp* convert_numeric( const std::string& str, int radix )
60 {
61  const char* s = str.c_str();
62  int ch = static_cast<unsigned char>( s[0] );
63  if ( isdigit( ch ) || ch == '.' || ch == '+' || ch == '-' )
64  {
65  char *endptr = NULL, *endptr2 = NULL;
66  int l = strtol( s, &endptr, radix );
67  double d = strtod( s, &endptr2 );
68 
69  if ( endptr >= endptr2 )
70  {
71  // it's a long
72  if ( endptr )
73  {
74  if ( ( l != INT_MIN ) && ( l != INT_MAX ) )
75  {
76  while ( *endptr )
77  {
78  if ( !isspace( *endptr ) )
79  {
80  if ( *endptr == '/' && *( endptr + 1 ) == '/' )
81  { // what follows is a comment
82  break;
83  }
84  return NULL;
85  }
86  ++endptr;
87  }
88  }
89  else
90  return NULL; // overflow, read it as string
91  }
92  return new BLong( l );
93  }
94  else
95  {
96  if ( !could_be_a_number( s ) )
97  return NULL;
98  if ( endptr2 )
99  {
100  if ( ( d != -HUGE_VAL ) && ( d != +HUGE_VAL ) )
101  {
102  while ( *endptr2 )
103  {
104  if ( !isspace( *endptr2 ) )
105  {
106  if ( *endptr2 == '/' && *( endptr2 + 1 ) == '/' )
107  {
108  // assume what follows is a comment
109  break;
110  }
111  return NULL;
112  }
113  ++endptr2;
114  }
115  }
116  else
117  return NULL; // overflow, read it as string
118  }
119  return new Double( d );
120  }
121  }
122  return NULL;
123 }
124 
125 BObjectImp* bobject_from_string( const std::string& str, int radix )
126 {
127  BObjectImp* imp = convert_numeric( str, radix );
128  if ( imp )
129  return imp;
130  else
131  return new ConstString( str );
132 }
133 
134 
135 std::string normalize_ecl_filename( const std::string& filename )
136 {
137  if ( filename.find( ".ecl" ) == std::string::npos )
138  return filename + ".ecl";
139  else
140  return filename;
141 }
142 }
143 }
bool could_be_a_number(const char *s)
Definition: escrutil.cpp:22
BObjectImp * bobject_from_string(const std::string &str, int radix)
Definition: escrutil.cpp:125
BObjectImp * convert_numeric(const std::string &str, int radix)
Definition: escrutil.cpp:59
std::string normalize_ecl_filename(const std::string &filename)
Definition: escrutil.cpp:135
Definition: berror.cpp:12