changed qse_ntime_t to a structure and made related changes

This commit is contained in:
2012-11-11 16:07:34 +00:00
parent d6a3bfea8d
commit b94dd042c5
29 changed files with 460 additions and 304 deletions

View File

@ -377,7 +377,7 @@ Awk::Value::operator const Awk::char_t* () const
{
const Awk::char_t* ptr;
size_t len;
if (Awk::Value::getStr (&ptr, &len) <= -1) ptr = QSE_T("");
if (Awk::Value::getStr (&ptr, &len) <= -1) ptr = EMPTY_STRING;
return ptr;
}
@ -385,16 +385,14 @@ int Awk::Value::getInt (long_t* v) const
{
long_t lv = 0;
QSE_ASSERT (val != QSE_NULL);
QSE_ASSERT (this->val != QSE_NULL);
if (run != QSE_NULL &&
val->type != QSE_AWK_VAL_NIL &&
val->type != QSE_AWK_VAL_MAP)
if (run != QSE_NULL)
{
int n = qse_awk_rtx_valtolong (run->rtx, val, &lv);
int n = qse_awk_rtx_valtolong (this->run->rtx, this->val, &lv);
if (n <= -1)
{
run->awk->retrieveError (run);
run->awk->retrieveError (this->run);
return -1;
}
}
@ -407,16 +405,14 @@ int Awk::Value::getFlt (flt_t* v) const
{
flt_t rv = 0;
QSE_ASSERT (val != QSE_NULL);
QSE_ASSERT (this->val != QSE_NULL);
if (run != QSE_NULL &&
val->type != QSE_AWK_VAL_NIL &&
val->type != QSE_AWK_VAL_MAP)
if (this->run)
{
int n = qse_awk_rtx_valtoflt (run->rtx, val, &rv);
int n = qse_awk_rtx_valtoflt (this->run->rtx, this->val, &rv);
if (n <= -1)
{
run->awk->retrieveError (run);
run->awk->retrieveError (this->run);
return -1;
}
}
@ -425,21 +421,38 @@ int Awk::Value::getFlt (flt_t* v) const
return 0;
}
int Awk::Value::getNum (long_t* lv, flt_t* fv) const
{
QSE_ASSERT (this->val != QSE_NULL);
if (this->run != QSE_NULL)
{
int n = qse_awk_rtx_valtonum (this->run->rtx, this->val, lv, fv);
if (n <= -1)
{
run->awk->retrieveError (this->run);
return -1;
}
return n;
}
*lv = 0;
return 0;
}
int Awk::Value::getStr (const char_t** str, size_t* len) const
{
const char_t* p = EMPTY_STRING;
size_t l = 0;
QSE_ASSERT (val != QSE_NULL);
QSE_ASSERT (this->val != QSE_NULL);
if (run != QSE_NULL &&
val->type != QSE_AWK_VAL_NIL &&
val->type != QSE_AWK_VAL_MAP)
if (this->run != QSE_NULL)
{
if (val->type == QSE_AWK_VAL_STR)
if (this->val->type == QSE_AWK_VAL_STR)
{
p = ((qse_awk_val_str_t*)val)->val.ptr;
l = ((qse_awk_val_str_t*)val)->val.len;
p = ((qse_awk_val_str_t*)this->val)->val.ptr;
l = ((qse_awk_val_str_t*)this->val)->val.len;
}
else
{
@ -447,9 +460,9 @@ int Awk::Value::getStr (const char_t** str, size_t* len) const
{
qse_awk_rtx_valtostr_out_t out;
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (run->rtx, val, &out) <= -1)
if (qse_awk_rtx_valtostr (this->run->rtx, this->val, &out) <= -1)
{
run->awk->retrieveError (run);
run->awk->retrieveError (this->run);
return -1;
}

View File

@ -160,7 +160,7 @@ int StdAwk::open ()
qse_ntime_t now;
this->seed = (qse_gettime(&now) <= -1)? 0u: (long_t)now;
this->seed = (qse_gettime(&now) <= -1)? 0u: ((long_t)now.sec + (long_t)now.nsec);
/* i don't care if the seed becomes negative or overflows.
* i just convert the signed value to the unsigned one. */
this->prand = (qse_ulong_t)(this->seed * this->seed * this->seed);
@ -368,7 +368,7 @@ int StdAwk::srand (Run& run, Value& ret, const Value* args, size_t nargs,
if (nargs <= 0)
{
this->seed = (qse_gettime (&now) <= -1)?
(this->seed * this->seed): (long_t)now;
(this->seed * this->seed): ((long_t)now.sec + (long_t)now.nsec);
}
else
{
@ -478,16 +478,28 @@ int StdAwk::setioattr (
int tmout;
if ((tmout = timeout_code (ptr[1])) >= 0)
{
long_t tmout_val = args[2].toInt();
long_t lv;
flt_t fv;
int n;
n = args[2].getNum(&lv, &fv);
if (n <= -1) return -1;
if (tmout_val < QSE_TYPE_MIN(int) ||
tmout_val > QSE_TYPE_MAX(int))
return ret.setInt ((long_t)-1);
ioattr_t* ioattr = find_or_make_ioattr (ptr[0], l[0]);
if (ioattr == QSE_NULL) return -1;
ioattr->tmout[tmout] = tmout_val;
if (n == 0)
{
ioattr->tmout[tmout].sec = lv;
ioattr->tmout[tmout].nsec = 0;
}
else
{
qse_flt_t nsec;
ioattr->tmout[tmout].sec = (qse_long_t)fv;
nsec = fv - ioattr->tmout[tmout].sec;
ioattr->tmout[tmout].nsec = QSE_SEC_TO_NSEC(nsec);
}
return ret.setInt ((long_t)0);
}
#if defined(QSE_CHAR_IS_WCHAR)
@ -541,7 +553,10 @@ int StdAwk::getioattr (
int tmout;
if ((tmout = timeout_code(ptr[1])) >= 0)
{
return ret.setInt ((long_t)ioattr->tmout[tmout]);
if (ioattr->tmout[tmout].nsec == 0)
return ret.setInt ((long_t)ioattr->tmout[tmout].sec);
else
return ret.setFlt ((qse_flt_t)ioattr->tmout[tmout].sec + QSE_NSEC_TO_SEC((qse_flt_t)ioattr->tmout[tmout].nsec));
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)

View File

@ -152,7 +152,7 @@ typedef struct ioattr_t
{
qse_cmgr_t* cmgr;
qse_char_t cmgr_name[64]; /* i assume that the cmgr name never exceeds this length */
int tmout[4];
qse_ntime_t tmout[4];
} ioattr_t;
static ioattr_t* get_ioattr (qse_htb_t* tab, const qse_char_t* ptr, qse_size_t len);
@ -2021,7 +2021,7 @@ qse_awk_rtx_t* qse_awk_rtx_openstd (
rxtn->ecb.close = fini_rxtn;
qse_awk_rtx_pushecb (rtx, &rxtn->ecb);
rxtn->seed = (qse_gettime (&now) <= -1)? 0u: (qse_long_t)now;
rxtn->seed = (qse_gettime (&now) <= -1)? 0u: ((qse_long_t)now.sec + (qse_long_t)now.nsec);
/* i don't care if the seed becomes negative or overflows.
* i just convert the signed value to the unsigned one. */
rxtn->prand = (qse_ulong_t)(rxtn->seed * rxtn->seed * rxtn->seed);
@ -2113,7 +2113,7 @@ static int fnc_srand (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
if (nargs <= 0)
{
rxtn->seed = (qse_gettime (&now) <= -1)?
(rxtn->seed * rxtn->seed): (qse_long_t)now;
(rxtn->seed * rxtn->seed): ((qse_long_t)now.sec + (qse_long_t)now.nsec);
}
else
{
@ -2212,7 +2212,8 @@ static QSE_INLINE void init_ioattr (ioattr_t* ioattr)
for (i = 0; i < QSE_COUNTOF(ioattr->tmout); i++)
{
/* a negative number for no timeout */
ioattr->tmout[i] = -999;
ioattr->tmout[i].sec = -999;
ioattr->tmout[i].nsec = 0;
}
}
@ -2301,14 +2302,8 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
/* no error is returned by qse_awk_rtx_strnum() if the second
* parameter is 0. so i don't check for an error */
x = qse_awk_rtx_strtonum (rtx, 0, ptr[2], len[2], &l, &r);
if (x >= 1) l = (qse_long_t)r;
if (x == 0) r = (qse_flt_t)l;
if (l < QSE_TYPE_MIN(int) || l > QSE_TYPE_MAX(int))
{
fret = -1;
goto done;
}
ioattr = find_or_make_ioattr (rtx, &rxtn->cmgrtab, ptr[0], len[0]);
if (ioattr == QSE_NULL)
{
@ -2316,7 +2311,18 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
goto done;
}
ioattr->tmout[tmout] = l;
if (x == 0)
{
ioattr->tmout[tmout].sec = l;
ioattr->tmout[tmout].nsec = 0;
}
else if (x >= 1)
{
qse_flt_t nsec;
ioattr->tmout[tmout].sec = (qse_long_t)r;
nsec = r - ioattr->tmout[tmout].sec;
ioattr->tmout[tmout].nsec = QSE_SEC_TO_NSEC(nsec);
}
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)
@ -2424,7 +2430,10 @@ static int fnc_getioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
if ((tmout = timeout_code (ptr[1])) >= 0)
{
rv = qse_awk_rtx_makeintval (rtx, ioattr->tmout[tmout]);
if (ioattr->tmout[tmout].nsec == 0)
rv = qse_awk_rtx_makeintval (rtx, ioattr->tmout[tmout].sec);
else
rv = qse_awk_rtx_makefltval (rtx, (qse_flt_t)ioattr->tmout[tmout].sec + QSE_NSEC_TO_SEC((qse_flt_t)ioattr->tmout[tmout].nsec));
if (rv == QSE_NULL)
{
ret = -1;