enhanced qse_awk_rtx_strtonum() to better determine the input record type.

This commit is contained in:
hyung-hwan 2009-06-12 20:59:59 +00:00
parent 9ee15f2e0a
commit d6899c0b58
4 changed files with 49 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h 196 2009-06-11 07:44:44Z hyunghwan.chung $
* $Id: awk.h 197 2009-06-12 02:59:59Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1690,19 +1690,28 @@ int qse_awk_rtx_valtonum (
);
/******/
/****f* AWK/qse_awk_rtx_strtonum
* NAME
* qse_awk_rtx_strtonum - convert a string to a number
* SYNOPSIS
/**
* The qse_awk_rtx_strtonum() function converts a string to a number.
* A numeric string in the valid decimal, hexadecimal(0x), binary(0b),
* octal(0) notation is converted to an integer and it is stored into
* memory pointed to by @a l; A string containng '.', 'E', or 'e' is
* converted to a floating-pointer number and it is stored into memory
* pointed to by @a r. If @a strict is 0, the function takes up to the last
* valid character and never fails. If @a strict is non-zero, an invalid
* character causes the function to return an error.
*
* @return 0 if converted to an integer,
* 1 if converted to a floating-point number
* -1 on error.
*/
int qse_awk_rtx_strtonum (
qse_awk_rtx_t* run,
const qse_char_t* ptr,
qse_size_t len,
qse_long_t* l,
qse_real_t* r
qse_awk_rtx_t* rtx, /**< runtime context */
int strict, /**< determines to perform strict check */
const qse_char_t* ptr, /**< points to a string to convert */
qse_size_t len, /**< number of characters in a string */
qse_long_t* l, /**< stores a converted integer */
qse_real_t* r /**< stores a converted floating-poing number */
);
/******/
/**
* The qse_awk_rtx_alloc() function allocats a memory block of @a size bytes

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp 189 2009-06-07 06:23:53Z hyunghwan.chung $
* $Id: Awk.cpp 197 2009-06-12 02:59:59Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -383,7 +383,8 @@ int Awk::Argument::init (const char_t* str, size_t len)
this->str.ptr = (char_t*)str;
this->str.len = len;
if (qse_awk_rtx_strtonum (this->run->run,
if (qse_awk_rtx_strtonum (
this->run->run, 0,
str, len, &this->inum, &this->rnum) == 0)
{
this->rnum = (real_t)this->inum;

View File

@ -1,5 +1,5 @@
/*
* $Id: rec.c 171 2009-06-01 09:34:34Z hyunghwan.chung $
* $Id: rec.c 197 2009-06-12 02:59:59Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -201,6 +201,10 @@ static int split_record (qse_awk_rtx_t* run)
while (p != QSE_NULL)
{
qse_long_t l;
qse_real_t r;
int x;
if (fs_len <= 1)
{
p = qse_awk_rtx_strxntok (
@ -228,8 +232,12 @@ static int split_record (qse_awk_rtx_t* run)
run->inrec.flds[run->inrec.nflds].ptr = tok;
run->inrec.flds[run->inrec.nflds].len = tok_len;
x = qse_awk_rtx_strtonum (run, 1, tok, tok_len, &l, &r);
run->inrec.flds[run->inrec.nflds].val =
qse_awk_rtx_makestrval (run, tok, tok_len);
(x <= -1)? qse_awk_rtx_makestrval (run, tok, tok_len):
(x == 0)? qse_awk_rtx_makeintval (run, l):
/*(x >= 1)?*/ qse_awk_rtx_makerealval (run, r);
if (run->inrec.flds[run->inrec.nflds].val == QSE_NULL)
{

View File

@ -1,5 +1,5 @@
/*
* $Id: val.c 182 2009-06-03 21:50:32Z hyunghwan.chung $
* $Id: val.c 197 2009-06-12 02:59:59Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1155,9 +1155,12 @@ int qse_awk_rtx_valtonum (
if (v->type == QSE_AWK_VAL_STR)
{
return qse_awk_rtx_strtonum (run,
return qse_awk_rtx_strtonum (
run, 0,
((qse_awk_val_str_t*)v)->ptr,
((qse_awk_val_str_t*)v)->len, l, r);
((qse_awk_val_str_t*)v)->len,
l, r
);
}
#ifdef DEBUG_VAL
@ -1171,24 +1174,24 @@ int qse_awk_rtx_valtonum (
}
int qse_awk_rtx_strtonum (
qse_awk_rtx_t* run, const qse_char_t* ptr, qse_size_t len,
qse_awk_rtx_t* rtx, int strict,
const qse_char_t* ptr, qse_size_t len,
qse_long_t* l, qse_real_t* r)
{
const qse_char_t* endptr;
*l = qse_awk_strxtolong (run->awk, ptr, len, 0, &endptr);
if (*endptr == QSE_T('.') ||
*endptr == QSE_T('E') ||
*endptr == QSE_T('e'))
*l = qse_awk_strxtolong (rtx->awk, ptr, len, 0, &endptr);
if (endptr < ptr + len &&
(*endptr == QSE_T('.') ||
*endptr == QSE_T('E') ||
*endptr == QSE_T('e')))
{
*r = qse_awk_strxtoreal (run->awk, ptr, len, QSE_NULL);
/* TODO: need to check if it is a valid number using
* endptr for strxtoreal? */
*r = qse_awk_strxtoreal (rtx->awk, ptr, len, &endptr);
if (strict && endptr < ptr + len) return -1;
return 1; /* real */
}
/* TODO: do should i handle strings ending with invalid number
* characters like "123xx" or "dkdkdkd"? */
if (strict && endptr < ptr + len) return -1;
return 0; /* long */
}