From 88b408cdda2fea96012e95c7b41834e907f50e4f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 17 Dec 2008 03:42:48 +0000 Subject: [PATCH] added ase_gettime() and ase_settime(), also added many builtin functions to ase_awk_opensimple() --- ase/cmd/awk/awk.c | 40 ++-- ase/configure | 310 ++++++++++++++++++++++++++ ase/configure.ac | 7 + ase/include/ase/awk/StdAwk.hpp | 3 +- ase/include/ase/awk/awk.h | 65 +++++- ase/include/ase/cmn/makefile.am | 2 +- ase/include/ase/cmn/makefile.in | 2 +- ase/include/ase/cmn/time.h | 47 ++++ ase/include/ase/config.h.in | 81 +++++++ ase/lib/awk/StdAwk.cpp | 138 ++++++++++-- ase/lib/awk/func.c | 3 +- ase/lib/awk/std.c | 382 ++++++++++++++++++++++++++++++-- ase/lib/cmn/makefile.am | 1 + ase/lib/cmn/makefile.in | 4 +- ase/lib/cmn/time.c | 76 +++++++ 15 files changed, 1104 insertions(+), 57 deletions(-) create mode 100644 ase/include/ase/cmn/time.h create mode 100644 ase/lib/cmn/time.c diff --git a/ase/cmd/awk/awk.c b/ase/cmd/awk/awk.c index 9061e918..25e505ee 100644 --- a/ase/cmd/awk/awk.c +++ b/ase/cmd/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c 496 2008-12-15 09:56:48Z baconevi $ + * $Id: awk.c 499 2008-12-16 09:42:48Z baconevi $ */ #include @@ -14,7 +14,6 @@ #include #include #include -#include #include #define ABORT(label) goto label @@ -34,16 +33,21 @@ #include #endif +static ase_awk_t* app_awk = NULL; +static ase_awk_run_t* app_run = NULL; +static int app_debug = 0; + static void dprint (const ase_char_t* fmt, ...) { - va_list ap; - va_start (ap, fmt); - ase_vfprintf (stderr, fmt, ap); - va_end (ap); + if (app_debug) + { + va_list ap; + va_start (ap, fmt); + ase_vfprintf (stderr, fmt, ap); + va_end (ap); + } } -ase_awk_t* app_awk = NULL; -ase_awk_run_t* app_run = NULL; #ifdef _WIN32 static BOOL WINAPI stop_run (DWORD ctrl_type) @@ -176,9 +180,11 @@ static void print_usage (const ase_char_t* argv0) ase_printf (ASE_T("Usage: %s [options] -f sourcefile [ -- ] [datafile]*\n"), argv0); ase_printf (ASE_T(" %s [options] [ -- ] sourcestring [datafile]*\n"), argv0); ase_printf (ASE_T("Where options are:\n")); - ase_printf (ASE_T(" -f sourcefile --file=sourcefile\n")); - ase_printf (ASE_T(" -d deparsedfile --deparsed-file=deparsedfile\n")); - ase_printf (ASE_T(" -F string --field-separator=string\n")); + ase_printf (ASE_T(" -h print this message\n")); + ase_printf (ASE_T(" -d show extra information\n")); + ase_printf (ASE_T(" -f/--file sourcefile set the source script file\n")); + ase_printf (ASE_T(" -o/--deparsed-file deparsedfile set the deparsing output file\n")); + ase_printf (ASE_T(" -F/--field-separator string set a field separator(FS)\n")); ase_printf (ASE_T("\nYou may specify the following options to change the behavior of the interpreter.\n")); for (j = 0; j < ASE_COUNTOF(otab); j++) @@ -263,7 +269,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) { ASE_T(":main"), ASE_T('m') }, { ASE_T(":file"), ASE_T('f') }, { ASE_T(":field-separator"), ASE_T('F') }, - { ASE_T(":deparsed-file"), ASE_T('d') }, + { ASE_T(":deparsed-file"), ASE_T('o') }, { ASE_T(":assign"), ASE_T('v') }, { ASE_T("help"), ASE_T('h') } @@ -271,7 +277,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) static ase_opt_t opt = { - ASE_T("hm:f:F:d:v:"), + ASE_T("hdm:f:F:o:v:"), lng }; @@ -321,6 +327,12 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) if (vm != ASE_NULL) ase_map_close (vm); return 1; + case ASE_T('d'): + { + app_debug = 1; + break; + } + case ASE_T('f'): { if (isfl >= isfc-1) /* -1 for last ASE_NULL */ @@ -347,7 +359,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) break; } - case ASE_T('d'): + case ASE_T('o'): { osf = opt.arg; break; diff --git a/ase/configure b/ase/configure index f9b0bb9a..31613a0e 100755 --- a/ase/configure +++ b/ase/configure @@ -21425,6 +21425,316 @@ fi done +OLDLIBS="$LIBS" +LIBS="$LIBM $LIBS" + + + + + + + + + +for ac_func in powl sinl cosl tanl atanl atan2l logl expl sqrtl +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +{ echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } +if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case declares $ac_func. + For example, HP-UX 11i declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef $ac_func + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined __stub_$ac_func || defined __stub___$ac_func +choke me +#endif + +int +main () +{ +return $ac_func (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + eval "$as_ac_var=no" +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +fi +ac_res=`eval echo '${'$as_ac_var'}'` + { echo "$as_me:$LINENO: result: $ac_res" >&5 +echo "${ECHO_T}$ac_res" >&6; } +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + + + + + + + + + + +for ac_func in pow sin cos tan atan atan2 log exp sqrt +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +{ echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } +if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case declares $ac_func. + For example, HP-UX 11i declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef $ac_func + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined __stub_$ac_func || defined __stub___$ac_func +choke me +#endif + +int +main () +{ +return $ac_func (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + eval "$as_ac_var=no" +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +fi +ac_res=`eval echo '${'$as_ac_var'}'` + { echo "$as_me:$LINENO: result: $ac_res" >&5 +echo "${ECHO_T}$ac_res" >&6; } +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + + + + + + + + + + +for ac_func in powf sinf cosf tanf atanf atan2f logf expf sqrtf +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +{ echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } +if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case declares $ac_func. + For example, HP-UX 11i declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef $ac_func + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined __stub_$ac_func || defined __stub___$ac_func +choke me +#endif + +int +main () +{ +return $ac_func (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + eval "$as_ac_var=no" +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +fi +ac_res=`eval echo '${'$as_ac_var'}'` + { echo "$as_me:$LINENO: result: $ac_res" >&5 +echo "${ECHO_T}$ac_res" >&6; } +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + +LIBS="$OLDLIBS" + { echo "$as_me:$LINENO: checking for char" >&5 echo $ECHO_N "checking for char... $ECHO_C" >&6; } if test "${ac_cv_type_char+set}" = set; then diff --git a/ase/configure.ac b/ase/configure.ac index 054d4882..8a29f204 100644 --- a/ase/configure.ac +++ b/ase/configure.ac @@ -91,6 +91,13 @@ AC_CHECK_FUNCS([mbrlen mbrtowc wcrtomb]) AC_CHECK_FUNCS([mbsnrtowcs mbsrtowcs wcsnrtombs wcsrtombs]) AC_CHECK_FUNCS([lseek64 stat64 fstat64 ftruncate64]) +OLDLIBS="$LIBS" +LIBS="$LIBM $LIBS" +AC_CHECK_FUNCS([powl sinl cosl tanl atanl atan2l logl expl sqrtl]) +AC_CHECK_FUNCS([pow sin cos tan atan atan2 log exp sqrt]) +AC_CHECK_FUNCS([powf sinf cosf tanf atanf atan2f logf expf sqrtf]) +LIBS="$OLDLIBS" + dnl Checks the size of primitive data types AC_CHECK_SIZEOF(char) AC_CHECK_SIZEOF(short) diff --git a/ase/include/ase/awk/StdAwk.hpp b/ase/include/ase/awk/StdAwk.hpp index 7bdc3925..6291ca4d 100644 --- a/ase/include/ase/awk/StdAwk.hpp +++ b/ase/include/ase/awk/StdAwk.hpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.hpp 468 2008-12-10 10:19:59Z baconevi $ + * $Id: StdAwk.hpp 499 2008-12-16 09:42:48Z baconevi $ * * {License} */ @@ -23,6 +23,7 @@ class StdAwk: public Awk public: StdAwk (); int open (); + int run (const char_t* main, const char_t** args, size_t nargs); protected: diff --git a/ase/include/ase/awk/awk.h b/ase/include/ase/awk/awk.h index 37868b88..b9b800a2 100644 --- a/ase/include/ase/awk/awk.h +++ b/ase/include/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 496 2008-12-15 09:56:48Z baconevi $ + * $Id: awk.h 499 2008-12-16 09:42:48Z baconevi $ * * {License} */ @@ -1142,17 +1142,68 @@ void ase_awk_refdownval_nofree (ase_awk_run_t* run, ase_awk_val_t* val); void ase_awk_freevalchunk (ase_awk_run_t* run, ase_awk_val_chunk_t* chunk); ase_bool_t ase_awk_valtobool ( - ase_awk_run_t* run, ase_awk_val_t* val); + ase_awk_run_t* run, + ase_awk_val_t* val +); ase_char_t* ase_awk_valtostr ( - ase_awk_run_t* run, ase_awk_val_t* val, - int opt, ase_str_t* buf, ase_size_t* len); + ase_awk_run_t* run, + ase_awk_val_t* val, + int opt, + ase_str_t* buf, + ase_size_t* len +); +/****f* ase.awk/ase_awk_valtonum + * NAME + * ase_awk_valtonum - convert a value to a number + * + * DESCRIPTION + * The ase_awk_valtonum() function converts a value to a number. + * The converted value is stored into the variable pointed to by + * either l or r depending on the type of the number. If the value + * is converted to a long number, the function returns 0 and l is + * set with the converted number. If the value is converted to a real number, + * the function returns 1 and r is set with a real number. + * + * RETURN + * The ase_awk_valtonum() function returns -1 on error, 0 if the converted + * value is a long number and 1 if it is a real number. + * + * EXAMPLES + * ase_long_t l; + * ase_real_t r; + * int n; + * + * n = ase_awk_valtonum (v, &l, &r); + * if (n == -1) error (); + * else if (n == 0) do_long (l); + * else if (n == 1) do_real (r); + * + * SYNOPSIS + */ int ase_awk_valtonum ( - ase_awk_run_t* run, ase_awk_val_t* v, ase_long_t* l, ase_real_t* r); + ase_awk_run_t* run, + ase_awk_val_t* v /* the value to convert to a number */, + ase_long_t* l /* a pointer to a long number */, + ase_real_t* r /* a pointer to a ase_real_t */ +); +/******/ + +/****f* ase.awk/ase_awk_strtonum + * NAME + * ase_awk_strtonum - convert a string to a number + * + * SYNOPSIS + */ int ase_awk_strtonum ( - ase_awk_run_t* run, const ase_char_t* ptr, ase_size_t len, - ase_long_t* l, ase_real_t* r); + ase_awk_run_t* run, + const ase_char_t* ptr, + ase_size_t len, + ase_long_t* l, + ase_real_t* r +); +/******/ #ifdef __cplusplus } diff --git a/ase/include/ase/cmn/makefile.am b/ase/include/ase/cmn/makefile.am index b2aa7037..f5babf4e 100644 --- a/ase/include/ase/cmn/makefile.am +++ b/ase/include/ase/cmn/makefile.am @@ -1,5 +1,5 @@ -pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h +pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h time.h pkgincludedir= $(includedir)/ase/cmn diff --git a/ase/include/ase/cmn/makefile.in b/ase/include/ase/cmn/makefile.in index 57c4753c..479f0c24 100644 --- a/ase/include/ase/cmn/makefile.in +++ b/ase/include/ase/cmn/makefile.in @@ -178,7 +178,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h +pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h time.h CLEANFILES = *dist all: all-am diff --git a/ase/include/ase/cmn/time.h b/ase/include/ase/cmn/time.h new file mode 100644 index 00000000..7adfc561 --- /dev/null +++ b/ase/include/ase/cmn/time.h @@ -0,0 +1,47 @@ +/* + * $Id$ + */ + +#ifndef _ASE_CMN_TIME_H_ +#define _ASE_CMN_TIME_H_ + +#include +#include + +#define ASE_EPOCH_YEAR ((ase_time_t)1970) +#define ASE_EPOCH_MON ((ase_time_t)1) +#define ASE_EPOCH_DAY ((ase_time_t)1) +#define ASE_EPOCH_WDAY ((ase_time_t)4) + +#define ASE_DAY_IN_WEEK ((ase_time_t)7) +#define ASE_MON_IN_YEAR ((ase_time_t)12) +#define ASE_HOUR_IN_DAY ((ase_time_t)24) +#define ASE_MIN_IN_HOUR ((ase_time_t)60) +#define ASE_MIN_IN_DAY (ASE_MIN_IN_HOUR * ASE_HOUR_IN_DAY) +#define ASE_SEC_IN_MIN ((ase_time_t)60) +#define ASE_SEC_IN_HOUR (ASE_SEC_IN_MIN * ASE_MIN_IN_HOUR) +#define ASE_SEC_IN_DAY (ASE_SEC_IN_MIN * ASE_MIN_IN_DAY) +#define ASE_MSEC_IN_SEC ((ase_time_t)1000) +#define ASE_MSEC_IN_MIN (ASE_MSEC_IN_SEC * ASE_SEC_IN_MIN) +#define ASE_MSEC_IN_HOUR (ASE_MSEC_IN_SEC * ASE_SEC_IN_HOUR) +#define ASE_MSEC_IN_DAY (ASE_MSEC_IN_SEC * ASE_SEC_IN_DAY) + +#define ASE_USEC_IN_MSEC ((ase_time_t)1000) +#define ASE_NSEC_IN_USEC ((ase_time_t)1000) +#define ASE_USEC_IN_SEC ((ase_time_t)ASE_USEC_IN_MSEC * ASE_MSEC_IN_SEC) + +/* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970) */ +typedef ase_long_t ase_time_t; + +#ifdef __cplusplus +extern "C" { +#endif + +int ase_gettime (ase_time_t* t); +int ase_settime (ase_time_t t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ase/include/ase/config.h.in b/ase/include/ase/config.h.in index e12dd856..4225b776 100644 --- a/ase/include/ase/config.h.in +++ b/ase/include/ase/config.h.in @@ -78,9 +78,45 @@ /* patch level */ #undef ASE_VERSION_PATCH +/* Define to 1 if you have the `atan' function. */ +#undef HAVE_ATAN + +/* Define to 1 if you have the `atan2' function. */ +#undef HAVE_ATAN2 + +/* Define to 1 if you have the `atan2f' function. */ +#undef HAVE_ATAN2F + +/* Define to 1 if you have the `atan2l' function. */ +#undef HAVE_ATAN2L + +/* Define to 1 if you have the `atanf' function. */ +#undef HAVE_ATANF + +/* Define to 1 if you have the `atanl' function. */ +#undef HAVE_ATANL + +/* Define to 1 if you have the `cos' function. */ +#undef HAVE_COS + +/* Define to 1 if you have the `cosf' function. */ +#undef HAVE_COSF + +/* Define to 1 if you have the `cosl' function. */ +#undef HAVE_COSL + /* Define to 1 if you have the header file. */ #undef HAVE_DLFCN_H +/* Define to 1 if you have the `exp' function. */ +#undef HAVE_EXP + +/* Define to 1 if you have the `expf' function. */ +#undef HAVE_EXPF + +/* Define to 1 if you have the `expl' function. */ +#undef HAVE_EXPL + /* Define to 1 if you have the `fstat64' function. */ #undef HAVE_FSTAT64 @@ -90,6 +126,15 @@ /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H +/* Define to 1 if you have the `log' function. */ +#undef HAVE_LOG + +/* Define to 1 if you have the `logf' function. */ +#undef HAVE_LOGF + +/* Define to 1 if you have the `logl' function. */ +#undef HAVE_LOGL + /* Define to 1 if you have the `lseek64' function. */ #undef HAVE_LSEEK64 @@ -108,6 +153,33 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `pow' function. */ +#undef HAVE_POW + +/* Define to 1 if you have the `powf' function. */ +#undef HAVE_POWF + +/* Define to 1 if you have the `powl' function. */ +#undef HAVE_POWL + +/* Define to 1 if you have the `sin' function. */ +#undef HAVE_SIN + +/* Define to 1 if you have the `sinf' function. */ +#undef HAVE_SINF + +/* Define to 1 if you have the `sinl' function. */ +#undef HAVE_SINL + +/* Define to 1 if you have the `sqrt' function. */ +#undef HAVE_SQRT + +/* Define to 1 if you have the `sqrtf' function. */ +#undef HAVE_SQRTF + +/* Define to 1 if you have the `sqrtl' function. */ +#undef HAVE_SQRTL + /* Define to 1 if you have the `stat64' function. */ #undef HAVE_STAT64 @@ -135,6 +207,15 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TYPES_H +/* Define to 1 if you have the `tan' function. */ +#undef HAVE_TAN + +/* Define to 1 if you have the `tanf' function. */ +#undef HAVE_TANF + +/* Define to 1 if you have the `tanl' function. */ +#undef HAVE_TANL + /* Define to 1 if you have the header file. */ #undef HAVE_UNISTD_H diff --git a/ase/lib/awk/StdAwk.cpp b/ase/lib/awk/StdAwk.cpp index 1e779d46..d01beb33 100644 --- a/ase/lib/awk/StdAwk.cpp +++ b/ase/lib/awk/StdAwk.cpp @@ -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 #include +#include #include #include @@ -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, diff --git a/ase/lib/awk/func.c b/ase/lib/awk/func.c index 92d40f29..1ae96c5a 100644 --- a/ase/lib/awk/func.c +++ b/ase/lib/awk/func.c @@ -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) diff --git a/ase/lib/awk/std.c b/ase/lib/awk/std.c index e1fa7912..8f312f8d 100644 --- a/ase/lib/awk/std.c +++ b/ase/lib/awk/std.c @@ -4,9 +4,12 @@ #include "awk.h" #include +#include +#include #include #include +#include #include 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; +} diff --git a/ase/lib/cmn/makefile.am b/ase/lib/cmn/makefile.am index 06e6f7b0..0af33f7a 100644 --- a/ase/lib/cmn/makefile.am +++ b/ase/lib/cmn/makefile.am @@ -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 diff --git a/ase/lib/cmn/makefile.in b/ase/lib/cmn/makefile.in index 586300a3..d8f506c2 100644 --- a/ase/lib/cmn/makefile.in +++ b/ase/lib/cmn/makefile.in @@ -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@ diff --git a/ase/lib/cmn/time.c b/ase/lib/cmn/time.c new file mode 100644 index 00000000..8e62204e --- /dev/null +++ b/ase/lib/cmn/time.c @@ -0,0 +1,76 @@ +/* + * $Id$ + */ + +#include + +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +#if defined(ASE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H) +#include +#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 +} +