added ase_gettime() and ase_settime(), also added many builtin functions to ase_awk_opensimple()
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdAwk.cpp 468 2008-12-10 10:19:59Z baconevi $
|
||||
* $Id: StdAwk.cpp 499 2008-12-16 09:42:48Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
@ -10,6 +10,7 @@
|
||||
|
||||
#include <ase/awk/StdAwk.hpp>
|
||||
#include <ase/cmn/str.h>
|
||||
#include <ase/cmn/time.h>
|
||||
#include <ase/utl/stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
@ -26,11 +27,8 @@
|
||||
ASE_BEGIN_NAMESPACE(ASE)
|
||||
/////////////////////////////////
|
||||
|
||||
|
||||
StdAwk::StdAwk ()
|
||||
{
|
||||
seed = (unsigned int)::time(NULL);
|
||||
::srand (seed);
|
||||
}
|
||||
|
||||
#define ADD_FUNC(name,min,max,impl) \
|
||||
@ -67,52 +65,144 @@ int StdAwk::open ()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int StdAwk::run (const char_t* main, const char_t** args, size_t nargs)
|
||||
{
|
||||
ase_time_t now;
|
||||
|
||||
if (ase_gettime(&now) == -1) this->seed = 0;
|
||||
else this->seed = (unsigned int)now;
|
||||
|
||||
::srand (this->seed);
|
||||
|
||||
return Awk::run (main, args, nargs);
|
||||
}
|
||||
|
||||
int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::sin(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_SINL)
|
||||
(real_t)::sinl(args[0].toReal())
|
||||
#elif defined(HAVE_SIN)
|
||||
(real_t)::sin(args[0].toReal())
|
||||
#elif defined(HAVE_SINF)
|
||||
(real_t)::sinf(args[0].toReal())
|
||||
#else
|
||||
#error ### no sin function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::cos (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::cos(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_COSL)
|
||||
(real_t)::cosl(args[0].toReal())
|
||||
#elif defined(HAVE_COS)
|
||||
(real_t)::cos(args[0].toReal())
|
||||
#elif defined(HAVE_COSF)
|
||||
(real_t)::cosf(args[0].toReal())
|
||||
#else
|
||||
#error ### no cos function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::tan (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::tan(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_TANL)
|
||||
(real_t)::tanl(args[0].toReal())
|
||||
#elif defined(HAVE_TAN)
|
||||
(real_t)::tan(args[0].toReal())
|
||||
#elif defined(HAVE_TANF)
|
||||
(real_t)::tanf(args[0].toReal())
|
||||
#else
|
||||
#error ### no tan function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::atan (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::atan(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_ATANL)
|
||||
(real_t)::atanl(args[0].toReal())
|
||||
#elif defined(HAVE_ATAN)
|
||||
(real_t)::atan(args[0].toReal())
|
||||
#elif defined(HAVE_ATANF)
|
||||
(real_t)::atanf(args[0].toReal())
|
||||
#else
|
||||
#error ### no atan function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::atan2 (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::atan2(args[0].toReal(), args[1].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_ATAN2L)
|
||||
(real_t)::atan2l(args[0].toReal(), args[1].toReal())
|
||||
#elif defined(HAVE_ATAN2)
|
||||
(real_t)::atan2(args[0].toReal(), args[1].toReal())
|
||||
#elif defined(HAVE_ATAN2F)
|
||||
(real_t)::atan2f(args[0].toReal(), args[1].toReal())
|
||||
#else
|
||||
#error ### no atan2 function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::log (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::log(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_LOGL)
|
||||
(real_t)::logl(args[0].toReal())
|
||||
#elif defined(HAVE_LOG)
|
||||
(real_t)::log(args[0].toReal())
|
||||
#elif defined(HAVE_LOGF)
|
||||
(real_t)::logf(args[0].toReal())
|
||||
#else
|
||||
#error ### no log function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::exp (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::exp(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_EXPL)
|
||||
(real_t)::expl(args[0].toReal())
|
||||
#elif defined(HAVE_EXP)
|
||||
(real_t)::exp(args[0].toReal())
|
||||
#elif defined(HAVE_EXPF)
|
||||
(real_t)::expf(args[0].toReal())
|
||||
#else
|
||||
#error ### no exp function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::sqrt (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((real_t)::sqrt(args[0].toReal()));
|
||||
return ret.set (
|
||||
#if defined(HAVE_SQRTL)
|
||||
(real_t)::sqrtl(args[0].toReal())
|
||||
#elif defined(HAVE_SQRT)
|
||||
(real_t)::sqrt(args[0].toReal())
|
||||
#elif defined(HAVE_SQRTF)
|
||||
(real_t)::sqrtf(args[0].toReal())
|
||||
#else
|
||||
#error ### no sqrt function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
int StdAwk::fnint (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
@ -130,12 +220,22 @@ int StdAwk::rand (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
unsigned int prevSeed = seed;
|
||||
unsigned int prevSeed = this->seed;
|
||||
|
||||
seed = (nargs == 0)?
|
||||
(unsigned int)::time(NULL):
|
||||
(unsigned int)args[0].toInt();
|
||||
::srand (seed);
|
||||
if (nargs == 0)
|
||||
{
|
||||
ase_time_t now;
|
||||
|
||||
if (ase_gettime (&now) == -1)
|
||||
this->seed = (unsigned int)now;
|
||||
else this->seed >>= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->seed = (unsigned int)args[0].toInt();
|
||||
}
|
||||
|
||||
::srand (this->seed);
|
||||
return ret.set ((long_t)prevSeed);
|
||||
}
|
||||
|
||||
@ -149,7 +249,9 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
int StdAwk::systime (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
{
|
||||
return ret.set ((long_t)::time(NULL));
|
||||
ase_time_t now;
|
||||
if (ase_gettime (&now) == -1) now = 0;
|
||||
return ret.set ((long_t)now / ASE_MSEC_IN_SEC);
|
||||
}
|
||||
|
||||
int StdAwk::strftime (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: func.c 391 2008-09-27 09:51:23Z baconevi $
|
||||
* $Id: func.c 499 2008-12-16 09:42:48Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
@ -802,6 +802,7 @@ static int bfn_split (
|
||||
((ase_awk_val_map_t*)t1)->map,
|
||||
key, key_len, t2, 0) == ASE_NULL)
|
||||
{
|
||||
/* assignment failed. restore the reference counter */
|
||||
ase_awk_refdownval (run, t2);
|
||||
|
||||
if (str_free != ASE_NULL)
|
||||
|
@ -4,9 +4,12 @@
|
||||
|
||||
#include "awk.h"
|
||||
#include <ase/cmn/sio.h>
|
||||
#include <ase/cmn/str.h>
|
||||
#include <ase/cmn/time.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <ase/utl/stdio.h>
|
||||
|
||||
typedef struct xtn_t
|
||||
@ -14,9 +17,22 @@ typedef struct xtn_t
|
||||
ase_awk_prmfns_t prmfns;
|
||||
} xtn_t;
|
||||
|
||||
typedef struct rxtn_t
|
||||
{
|
||||
unsigned int seed;
|
||||
} rxtn_t;
|
||||
|
||||
static ase_real_t custom_awk_pow (void* custom, ase_real_t x, ase_real_t y)
|
||||
{
|
||||
#if defined(HAVE_POWL)
|
||||
return powl (x, y);
|
||||
#elif defined(HAVE_POW)
|
||||
return pow (x, y);
|
||||
#elif defined(HAVE_POWF)
|
||||
return powf (x, y);
|
||||
#else
|
||||
#error ### no pow function available ###
|
||||
#endif
|
||||
}
|
||||
|
||||
static int custom_awk_sprintf (
|
||||
@ -33,6 +49,8 @@ static int custom_awk_sprintf (
|
||||
return n;
|
||||
}
|
||||
|
||||
static int add_functions (ase_awk_t* awk);
|
||||
|
||||
ase_awk_t* ase_awk_opensimple (ase_size_t xtnsize)
|
||||
{
|
||||
ase_awk_t* awk;
|
||||
@ -52,6 +70,12 @@ ase_awk_t* ase_awk_opensimple (ase_size_t xtnsize)
|
||||
ASE_AWK_IMPLICIT | ASE_AWK_EXTIO | ASE_AWK_NEWLINE |
|
||||
ASE_AWK_BASEONE | ASE_AWK_PABLOCK);
|
||||
|
||||
if (add_functions (awk) == -1)
|
||||
{
|
||||
ase_awk_close (awk);
|
||||
return ASE_NULL;
|
||||
}
|
||||
|
||||
return awk;
|
||||
}
|
||||
|
||||
@ -683,6 +707,8 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf, ase_awk_runcbs_t* cbs)
|
||||
{
|
||||
ase_awk_runios_t ios;
|
||||
runio_data_t rd;
|
||||
rxtn_t rxtn;
|
||||
ase_time_t now;
|
||||
|
||||
rd.ic.files = icf;
|
||||
rd.ic.index = 0;
|
||||
@ -692,21 +718,249 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf, ase_awk_runcbs_t* cbs)
|
||||
ios.console = awk_extio_console;
|
||||
ios.data = &rd;
|
||||
|
||||
if (ase_gettime (&now) == -1) rxtn.seed = 0;
|
||||
else rxtn.seed = (unsigned int)now;
|
||||
srand (rxtn.seed);
|
||||
|
||||
return ase_awk_run (
|
||||
awk,
|
||||
ASE_NULL/*mfn*/,
|
||||
&ios,
|
||||
cbs,
|
||||
ASE_NULL/*runarg*/,
|
||||
ASE_NULL
|
||||
&rxtn/*ASE_NULL*/
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*** EXTRA BUILTIN FUNCTIONS ***/
|
||||
#if 0
|
||||
int aes_awk_func_sleep (
|
||||
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
enum
|
||||
{
|
||||
BFN_MATH_LD,
|
||||
BFN_MATH_D,
|
||||
BFN_MATH_F
|
||||
};
|
||||
|
||||
static int bfn_math_1 (
|
||||
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl, int type, void* f)
|
||||
{
|
||||
ase_size_t nargs;
|
||||
ase_awk_val_t* a0;
|
||||
ase_long_t lv;
|
||||
ase_real_t rv;
|
||||
ase_awk_val_t* r;
|
||||
int n;
|
||||
|
||||
nargs = ase_awk_getnargs (run);
|
||||
ASE_ASSERT (nargs == 1);
|
||||
|
||||
a0 = ase_awk_getarg (run, 0);
|
||||
|
||||
n = ase_awk_valtonum (run, a0, &lv, &rv);
|
||||
if (n == -1) return -1;
|
||||
if (n == 0) rv = (ase_real_t)lv;
|
||||
|
||||
if (type == BFN_MATH_LD)
|
||||
{
|
||||
long double (*rf) (long double) =
|
||||
(long double(*)(long double))f;
|
||||
r = ase_awk_makerealval (run, rf(rv));
|
||||
}
|
||||
else if (type == BFN_MATH_D)
|
||||
{
|
||||
double (*rf) (double) = (double(*)(double))f;
|
||||
r = ase_awk_makerealval (run, rf(rv));
|
||||
}
|
||||
else
|
||||
{
|
||||
ASE_ASSERT (type == BFN_MATH_F);
|
||||
float (*rf) (float) = (float(*)(float))f;
|
||||
r = ase_awk_makerealval (run, rf(rv));
|
||||
}
|
||||
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bfn_math_2 (
|
||||
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl, int type, void* f)
|
||||
{
|
||||
ase_size_t nargs;
|
||||
ase_awk_val_t* a0, * a1;
|
||||
ase_long_t lv0, lv1;
|
||||
ase_real_t rv0, rv1;
|
||||
ase_awk_val_t* r;
|
||||
int n;
|
||||
|
||||
nargs = ase_awk_getnargs (run);
|
||||
ASE_ASSERT (nargs == 2);
|
||||
|
||||
a0 = ase_awk_getarg (run, 0);
|
||||
a1 = ase_awk_getarg (run, 1);
|
||||
|
||||
n = ase_awk_valtonum (run, a0, &lv0, &rv0);
|
||||
if (n == -1) return -1;
|
||||
if (n == 0) rv0 = (ase_real_t)lv0;
|
||||
|
||||
n = ase_awk_valtonum (run, a1, &lv1, &rv1);
|
||||
if (n == -1) return -1;
|
||||
if (n == 0) rv1 = (ase_real_t)lv1;
|
||||
|
||||
if (type == BFN_MATH_LD)
|
||||
{
|
||||
long double (*rf) (long double,long double) =
|
||||
(long double(*)(long double,long double))f;
|
||||
r = ase_awk_makerealval (run, rf(rv0,rv1));
|
||||
}
|
||||
else if (type == BFN_MATH_D)
|
||||
{
|
||||
double (*rf) (double,double) = (double(*)(double,double))f;
|
||||
r = ase_awk_makerealval (run, rf(rv0,rv1));
|
||||
}
|
||||
else
|
||||
{
|
||||
ASE_ASSERT (type == BFN_MATH_F);
|
||||
float (*rf) (float,float) = (float(*)(float,float))f;
|
||||
r = ase_awk_makerealval (run, rf(rv0,rv1));
|
||||
}
|
||||
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bfn_sin (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_SINL)
|
||||
BFN_MATH_LD, sinl
|
||||
#elif defined(HAVE_SIN)
|
||||
BFN_MATH_D, sin
|
||||
#elif defined(HAVE_SINF)
|
||||
BFN_MATH_F, sinf
|
||||
#else
|
||||
#error ### no sin function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_cos (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_COSL)
|
||||
BFN_MATH_LD, cosl
|
||||
#elif defined(HAVE_COS)
|
||||
BFN_MATH_D, cos
|
||||
#elif defined(HAVE_COSF)
|
||||
BFN_MATH_F, cosf
|
||||
#else
|
||||
#error ### no cos function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_tan (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_TANL)
|
||||
BFN_MATH_LD, tanl
|
||||
#elif defined(HAVE_TAN)
|
||||
BFN_MATH_D, tan
|
||||
#elif defined(HAVE_TANF)
|
||||
BFN_MATH_F, tanf
|
||||
#else
|
||||
#error ### no tan function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_atan (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_ATANL)
|
||||
BFN_MATH_LD, atanl
|
||||
#elif defined(HAVE_ATAN)
|
||||
BFN_MATH_D, atan
|
||||
#elif defined(HAVE_ATANF)
|
||||
BFN_MATH_F, atanf
|
||||
#else
|
||||
#error ### no atan function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_atan2 (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_2 (run, fnm, fnl,
|
||||
#if defined(HAVE_ATAN2L)
|
||||
BFN_MATH_LD, atan2l
|
||||
#elif defined(HAVE_ATAN2)
|
||||
BFN_MATH_D, atan2
|
||||
#elif defined(HAVE_ATAN2F)
|
||||
BFN_MATH_F, atan2f
|
||||
#else
|
||||
#error ### no atan2 function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_log (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_LOGL)
|
||||
BFN_MATH_LD, logl
|
||||
#elif defined(HAVE_LOG)
|
||||
BFN_MATH_D, log
|
||||
#elif defined(HAVE_LOGF)
|
||||
BFN_MATH_F, logf
|
||||
#else
|
||||
#error ### no log function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_exp (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_EXPL)
|
||||
BFN_MATH_LD, expl
|
||||
#elif defined(HAVE_EXP)
|
||||
BFN_MATH_D, exp
|
||||
#elif defined(HAVE_EXPF)
|
||||
BFN_MATH_F, expf
|
||||
#else
|
||||
#error ### no exp function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_sqrt (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
return bfn_math_1 (run, fnm, fnl,
|
||||
#if defined(HAVE_SQRTL)
|
||||
BFN_MATH_LD, sqrtl
|
||||
#elif defined(HAVE_SQRT)
|
||||
BFN_MATH_D, sqrt
|
||||
#elif defined(HAVE_SQRTF)
|
||||
BFN_MATH_F, sqrtf
|
||||
#else
|
||||
#error ### no sqrt function available ###
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
static int bfn_int (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
ase_size_t nargs;
|
||||
ase_awk_val_t* a0;
|
||||
@ -724,14 +978,7 @@ int aes_awk_func_sleep (
|
||||
if (n == -1) return -1;
|
||||
if (n == 1) lv = (ase_long_t)rv;
|
||||
|
||||
#ifdef _WIN32
|
||||
Sleep ((DWORD)(lv * 1000));
|
||||
n = 0;
|
||||
#else
|
||||
n = sleep (lv);
|
||||
#endif
|
||||
|
||||
r = ase_awk_makeintval (run, n);
|
||||
r = ase_awk_makeintval (run, lv);
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
@ -741,4 +988,113 @@ int aes_awk_func_sleep (
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int bfn_rand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
ase_awk_val_t* r;
|
||||
|
||||
r = ase_awk_makeintval (run, rand());
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bfn_srand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
ase_size_t nargs;
|
||||
ase_awk_val_t* a0;
|
||||
ase_long_t lv;
|
||||
ase_real_t rv;
|
||||
ase_awk_val_t* r;
|
||||
int n;
|
||||
unsigned int prev;
|
||||
rxtn_t* rxtn;
|
||||
|
||||
rxtn = ase_awk_getrundata (run);
|
||||
nargs = ase_awk_getnargs (run);
|
||||
ASE_ASSERT (nargs == 0 || nargs == 1);
|
||||
|
||||
prev = rxtn->seed;
|
||||
|
||||
if (nargs == 1)
|
||||
{
|
||||
a0 = ase_awk_getarg (run, 0);
|
||||
|
||||
n = ase_awk_valtonum (run, a0, &lv, &rv);
|
||||
if (n == -1) return -1;
|
||||
if (n == 1) lv = (ase_long_t)rv;
|
||||
|
||||
rxtn->seed = lv;
|
||||
}
|
||||
else
|
||||
{
|
||||
ase_time_t now;
|
||||
|
||||
if (ase_gettime(&now) == -1) rxtn->seed >>= 1;
|
||||
else rxtn->seed = (unsigned int)now;
|
||||
}
|
||||
|
||||
srand (rxtn->seed);
|
||||
|
||||
r = ase_awk_makeintval (run, prev);
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bfn_systime (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
||||
{
|
||||
ase_awk_val_t* r;
|
||||
ase_time_t now;
|
||||
int n;
|
||||
|
||||
if (ase_gettime(&now) == -1) now = 0;
|
||||
|
||||
r = ase_awk_makeintval (run, now / ASE_MSEC_IN_SEC);
|
||||
if (r == ASE_NULL)
|
||||
{
|
||||
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ase_awk_setretval (run, r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define ADD_FUNC(awk,name,min,max,bfn) \
|
||||
if (ase_awk_addfunc (\
|
||||
(awk), (name), ase_strlen(name), \
|
||||
0, (min), (max), ASE_NULL, (bfn)) == ASE_NULL) return -1;
|
||||
|
||||
static int add_functions (ase_awk_t* awk)
|
||||
{
|
||||
ADD_FUNC (awk, ASE_T("sin"), 1, 1, bfn_sin);
|
||||
ADD_FUNC (awk, ASE_T("cos"), 1, 1, bfn_cos);
|
||||
ADD_FUNC (awk, ASE_T("tan"), 1, 1, bfn_tan);
|
||||
ADD_FUNC (awk, ASE_T("atan"), 1, 1, bfn_atan);
|
||||
ADD_FUNC (awk, ASE_T("atan2"), 2, 2, bfn_atan2);
|
||||
ADD_FUNC (awk, ASE_T("log"), 1, 1, bfn_log);
|
||||
ADD_FUNC (awk, ASE_T("exp"), 1, 1, bfn_exp);
|
||||
ADD_FUNC (awk, ASE_T("sqrt"), 1, 1, bfn_sqrt);
|
||||
ADD_FUNC (awk, ASE_T("int"), 1, 1, bfn_int);
|
||||
ADD_FUNC (awk, ASE_T("rand"), 0, 0, bfn_rand);
|
||||
ADD_FUNC (awk, ASE_T("srand"), 0, 1, bfn_srand);
|
||||
ADD_FUNC (awk, ASE_T("systime"), 0, 0, bfn_systime);
|
||||
/*
|
||||
ADD_FUNC (awk, ASE_T("strftime"), 0, 2, bfn_strftime);
|
||||
ADD_FUNC (awk, ASE_T("strfgmtime"), 0, 2, bfn_strfgmtime);
|
||||
ADD_FUNC (awk, ASE_T("system"), 1, 1, bfn_system);
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ libasecmn_la_SOURCES = mem.h chr.h \
|
||||
mem.c chr.c chr_cnv.c rex.c str_bas.c str_cnv.c str_dyn.c \
|
||||
lda.c map.c sll.c dll.c opt.c \
|
||||
fio.c sio.c tio.c tio_get.c tio_put.c \
|
||||
time.c \
|
||||
misc.c
|
||||
libasecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
|
||||
|
||||
|
@ -53,7 +53,7 @@ LTLIBRARIES = $(lib_LTLIBRARIES)
|
||||
libasecmn_la_LIBADD =
|
||||
am_libasecmn_la_OBJECTS = mem.lo chr.lo chr_cnv.lo rex.lo str_bas.lo \
|
||||
str_cnv.lo str_dyn.lo lda.lo map.lo sll.lo dll.lo opt.lo \
|
||||
fio.lo sio.lo tio.lo tio_get.lo tio_put.lo misc.lo
|
||||
fio.lo sio.lo tio.lo tio_get.lo tio_put.lo time.lo misc.lo
|
||||
libasecmn_la_OBJECTS = $(am_libasecmn_la_OBJECTS)
|
||||
libasecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
|
||||
@ -204,6 +204,7 @@ libasecmn_la_SOURCES = mem.h chr.h \
|
||||
mem.c chr.c chr_cnv.c rex.c str_bas.c str_cnv.c str_dyn.c \
|
||||
lda.c map.c sll.c dll.c opt.c \
|
||||
fio.c sio.c tio.c tio_get.c tio_put.c \
|
||||
time.c \
|
||||
misc.c
|
||||
|
||||
libasecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
|
||||
@ -291,6 +292,7 @@ distclean-compile:
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_bas.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_cnv.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_dyn.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_get.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_put.Plo@am__quote@
|
||||
|
76
ase/lib/cmn/time.c
Normal file
76
ase/lib/cmn/time.c
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* $Id$
|
||||
*/
|
||||
|
||||
#include <ase/cmn/time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#if defined(ASE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H)
|
||||
#include <sys/syscall.h>
|
||||
#endif
|
||||
|
||||
int ase_gettime (ase_time_t* t)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SYSTEMTIME st;
|
||||
FILETIME ft;
|
||||
|
||||
/*
|
||||
* MSDN: The FILETIME structure is a 64-bit value representing the
|
||||
* number of 100-nanosecond intervals since January 1, 1601 (UTC).
|
||||
*/
|
||||
|
||||
GetSystemTime (&st);
|
||||
if (SystemTimeToFileTime (&st, &ft) == FALSE) return -1;
|
||||
*t = ((ase_time_t)(*((ase_int64_t*)&ft)) / (10 * 1000));
|
||||
*t -= EPOCH_DIFF_MSECS;
|
||||
return 0;
|
||||
#else
|
||||
struct timeval tv;
|
||||
int n;
|
||||
|
||||
#ifdef SYS_gettimeofday
|
||||
n = syscall (SYS_gettimeofday, &tv, ASE_NULL);
|
||||
#else
|
||||
n = gettimeofday (&tv, ASE_NULL);
|
||||
#endif
|
||||
if (n == -1) return -1;
|
||||
|
||||
*t = tv.tv_sec * ASE_MSEC_IN_SEC + tv.tv_usec / ASE_USEC_IN_MSEC;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ase_settime (ase_time_t t)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
FILETIME ft;
|
||||
SYSTEMTIME st;
|
||||
|
||||
*((ase_int64_t*)&ft) = ((value + EPOCH_DIFF_MSECS) * (10 * 1000));
|
||||
if (FileTimeToSystemTime (&ft, &st) == FALSE) return -1;
|
||||
if (SetSystemTime(&st) == FALSE) return -1;
|
||||
return 0;
|
||||
#else
|
||||
struct timeval tv;
|
||||
int n;
|
||||
|
||||
tv.tv_sec = t / ASE_MSEC_IN_SEC;
|
||||
tv.tv_usec = (t % ASE_MSEC_IN_SEC) * ASE_USEC_IN_MSEC;
|
||||
|
||||
#ifdef SYS_settimeofday
|
||||
n = syscall (SYS_settimeofday, &tv, ASE_NULL);
|
||||
#else
|
||||
n = settimeofday (&tv, ASE_NULL);
|
||||
#endif
|
||||
if (n == -1) return -1;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user