2016-01-28 14:52:40 +01:00
|
|
|
/** @file
|
|
|
|
|
*
|
|
|
|
|
* @par History
|
|
|
|
|
* - 2005/07/06 Shinigami: convert_numeric will use string representation if overflow
|
|
|
|
|
*/
|
2016-02-11 22:54:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "escrutil.h"
|
|
|
|
|
|
|
|
|
|
#include <climits>
|
2018-01-29 21:55:48 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include "bobject.h"
|
|
|
|
|
#include "impstr.h"
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
namespace Pol
|
|
|
|
|
{
|
|
|
|
|
namespace Bscript
|
|
|
|
|
{
|
|
|
|
|
bool could_be_a_number( const char* s )
|
|
|
|
|
{
|
|
|
|
|
if ( s[0] == '0' && ( s[1] == 'x' || s[1] == 'X' ) ) // Hex number
|
|
|
|
|
{
|
|
|
|
|
s += 2;
|
|
|
|
|
while ( *s )
|
|
|
|
|
{
|
|
|
|
|
char ch = *s;
|
|
|
|
|
++s;
|
|
|
|
|
if ( isxdigit( ch ) )
|
|
|
|
|
continue;
|
|
|
|
|
else if ( isspace( ch ) )
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else // expect -, +, 0-9, . only
|
|
|
|
|
{
|
|
|
|
|
while ( *s )
|
|
|
|
|
{
|
|
|
|
|
char ch = *s;
|
|
|
|
|
++s;
|
|
|
|
|
if ( ch == '-' || ch == '+' || ( ch >= '0' && ch <= '9' ) || ch == '.' )
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* convert_numeric( const std::string& str, int radix )
|
|
|
|
|
{
|
|
|
|
|
const char* s = str.c_str();
|
|
|
|
|
int ch = static_cast<unsigned char>( s[0] );
|
|
|
|
|
if ( isdigit( ch ) || ch == '.' || ch == '+' || ch == '-' )
|
|
|
|
|
{
|
2018-10-09 16:36:35 +02:00
|
|
|
char *endptr = nullptr, *endptr2 = nullptr;
|
2016-02-19 19:26:35 +01:00
|
|
|
int l = strtol( s, &endptr, radix );
|
|
|
|
|
double d = strtod( s, &endptr2 );
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
if ( endptr >= endptr2 )
|
|
|
|
|
{
|
|
|
|
|
// it's a long
|
|
|
|
|
if ( endptr )
|
|
|
|
|
{
|
|
|
|
|
if ( ( l != INT_MIN ) && ( l != INT_MAX ) )
|
|
|
|
|
{
|
|
|
|
|
while ( *endptr )
|
|
|
|
|
{
|
|
|
|
|
if ( !isspace( *endptr ) )
|
|
|
|
|
{
|
|
|
|
|
if ( *endptr == '/' && *( endptr + 1 ) == '/' )
|
|
|
|
|
{ // what follows is a comment
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr;
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
|
|
|
|
++endptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr; // overflow, read it as string
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
|
|
|
|
return new BLong( l );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( !could_be_a_number( s ) )
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr;
|
2016-02-19 19:26:35 +01:00
|
|
|
if ( endptr2 )
|
|
|
|
|
{
|
|
|
|
|
if ( ( d != -HUGE_VAL ) && ( d != +HUGE_VAL ) )
|
|
|
|
|
{
|
|
|
|
|
while ( *endptr2 )
|
|
|
|
|
{
|
|
|
|
|
if ( !isspace( *endptr2 ) )
|
|
|
|
|
{
|
|
|
|
|
if ( *endptr2 == '/' && *( endptr2 + 1 ) == '/' )
|
|
|
|
|
{
|
|
|
|
|
// assume what follows is a comment
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr;
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
|
|
|
|
++endptr2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr; // overflow, read it as string
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
|
|
|
|
return new Double( d );
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-09 16:36:35 +02:00
|
|
|
return nullptr;
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* bobject_from_string( const std::string& str, int radix )
|
|
|
|
|
{
|
|
|
|
|
BObjectImp* imp = convert_numeric( str, radix );
|
|
|
|
|
if ( imp )
|
|
|
|
|
return imp;
|
|
|
|
|
else
|
|
|
|
|
return new ConstString( str );
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
|
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
std::string normalize_ecl_filename( const std::string& filename )
|
|
|
|
|
{
|
|
|
|
|
if ( filename.find( ".ecl" ) == std::string::npos )
|
|
|
|
|
return filename + ".ecl";
|
|
|
|
|
else
|
|
|
|
|
return filename;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-29 21:55:48 +01:00
|
|
|
}
|