Recovered from cvs revision 2007-05-25 03:08:00

This commit is contained in:
hyung-hwan 2007-05-26 01:30:00 +00:00
parent d58553120e
commit 5c480566dc
6 changed files with 258 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.java,v 1.4 2007/05/05 16:32:46 bacon Exp $
* $Id: Awk.java,v 1.6 2007/05/24 06:53:21 bacon Exp $
*
* {License}
*/
@ -82,6 +82,9 @@ public abstract class Awk
private native String valtostr (
long runid, Object obj) throws Exception;
protected native String strftime (String fmt, long sec);
protected native String strfgmtime (String fmt, long sec);
/* == simpler run methods == */
public void run (String main) throws Exception
{

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.java,v 1.5 2007/05/11 16:25:38 bacon Exp $
* $Id: StdAwk.java,v 1.7 2007/05/24 06:53:21 bacon Exp $
*
* {License}
*/
@ -64,6 +64,7 @@ public abstract class StdAwk extends Awk
addFunction ("systime", 0, 0);
addFunction ("strftime", 0, 2);
addFunction ("strfgmtime", 0, 2);
addFunction ("system", 1, 1);
}
@ -799,10 +800,18 @@ public abstract class StdAwk extends Awk
return new Long (msec / 1000);
}
public Object bfn_strftime (long runid, Object[] args)
public Object bfn_strftime (long runid, Object[] args) throws Exception
{
// TODO: implement this...
return null;
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (runid, args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (runid, args[1]);
return strftime (fmt, t);
}
public Object bfn_strfgmtime (long runid, Object[] args) throws Exception
{
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (runid, args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (runid, args[1]);
return strfgmtime (fmt, t);
}
/* miscellaneous built-in functions */

View File

@ -1,5 +1,5 @@
/*
* $Id: jni.c,v 1.6 2007/05/10 16:08:37 bacon Exp $
* $Id: jni.c,v 1.8 2007/05/24 06:53:21 bacon Exp $
*
* {License}
*/
@ -12,6 +12,7 @@
#include <wchar.h>
#include <stdarg.h>
#include <math.h>
#include <time.h>
#include <ase/awk/jni.h>
#include <ase/awk/awk.h>
#include <ase/awk/val.h>
@ -2348,3 +2349,166 @@ JNIEXPORT jstring JNICALL Java_ase_awk_Awk_valtostr (
return ret;
}
static jstring JNICALL call_strftime (
JNIEnv* env, jobject obj, jstring fmt, struct tm* tm)
{
ase_char_t buf[128];
ase_size_t len, i;
const jchar* ptr;
ase_char_t* tmp;
jstring ret;
len = (*env)->GetStringLength (env, fmt);
ptr = (*env)->GetStringChars (env, fmt, JNI_FALSE);
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
}
tmp = (jchar*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
if (tmp == NULL)
{
(*env)->ReleaseStringChars (env, fmt, ptr);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
tmp[i] = ASE_T('\0');
len = wcsftime (buf, ASE_COUNTOF(buf), tmp, tm);
free (tmp);
(*env)->ReleaseStringChars (env, fmt, ptr);
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
{
tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
if (tmp == NULL)
{
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
}
for (i = 0; i < len; i++) tmp[i] = (jchar)buf[i];
ret = (*env)->NewString (env, tmp, len);
free (tmp);
}
else
{
ret = (*env)->NewString (env, (jchar*)buf, len);
}
if (ret == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
}
return ret;
}
JNIEXPORT jstring JNICALL Java_ase_awk_Awk_strftime (
JNIEnv* env, jobject obj, jstring fmt, jlong sec)
{
struct tm* tm;
time_t t = (time_t)sec;
#ifdef _WIN32
tm = localtime (&t);
#else
struct tm tmb;
tm = localtime_r (&t, &tmb);
#endif
return call_strftime (env, obj, fmt, tm);
}
JNIEXPORT jstring JNICALL Java_ase_awk_Awk_strfgmtime (
JNIEnv* env, jobject obj, jstring fmt, jlong sec)
{
struct tm* tm;
time_t t = (time_t)sec;
#ifdef _WIN32
tm = gmtime (&t);
#else
struct tm tmb;
tm = gmtime_r (&t, &tmb);
#endif
return call_strftime (env, obj, fmt, tm);
}
/*
JNIEXPORT jint JNICALL Java_ase_awk_Awk_system (
JNIEnv* env, jobject obj, jstring cmd)
{
ase_size_t len, i;
const jchar* ptr;
ase_char_t* tmp;
jint ret;
len = (*env)->GetStringLength (env, fmt);
ptr = (*env)->GetStringChars (env, fmt, JNI_FALSE);
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
}
tmp = (jchar*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
if (tmp == NULL)
{
(*env)->ReleaseStringChars (env, fmt, ptr);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
tmp[i] = ASE_T('\0');
#ifdef _WIN32
ret = _tsystem(ptr);
#else
char* mbs = (char*)malloc (awk, len*5+1);
if (mbs == ASE_NULL) return -1;
size_t mbl = wcstombs (mbs, ptr, len*5);
if (mbl == (size_t)-1) return -1;
mbs[mbl] = '\0';
ret = system(mbs);
free (awk, mbs);
#endif
return ret;
}
*/

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp,v 1.18 2007/05/23 14:15:16 bacon Exp $
* $Id: Awk.cpp,v 1.20 2007/05/24 04:17:42 bacon Exp $
*/
#include <ase/awk/StdAwk.hpp>
@ -73,10 +73,10 @@ public:
{
#ifdef _WIN32
::Sleep (args[0].toInt() * 1000);
return ret->set (0);
#else
::sleep (args[0].toInt());
return ret->set ((long_t)::sleep (args[0].toInt()));
#endif
return 0;
}
int addConsoleInput (const char_t* file)
@ -464,7 +464,22 @@ static void print_error (const ase_char_t* msg)
static void print_usage (const ase_char_t* argv0)
{
ase_printf (ASE_T("Usage: %s [-m main-function] [-si source-in-file] [-so source-out-file] [-ci console-in-file]* [-co console-out-file]* [-a argument]*\n"), argv0);
const ase_char_t* base;
base = ase_strrchr(argv0, ASE_T('/'));
if (base == ASE_NULL)
base = ase_strrchr(argv0, ASE_T('\\'));
if (base == ASE_NULL) base = argv0;
ase_printf (ASE_T("Usage: %s [-m main] [-si file]? [-so file]? [-ci file]* [-co file]* [-a arg]*\n"), base);
ase_printf (ASE_T(" -m main Specify the main function name\n"));
ase_printf (ASE_T(" -si file Specify the input source file\n"));
ase_printf (ASE_T(" The source code is read from stdin when it is not specified\n"));
ase_printf (ASE_T(" -so file Specify the output source file\n"));
ase_printf (ASE_T(" The deparsed code is not output when is it not specified\n"));
ase_printf (ASE_T(" -ci file Specify the input console file\n"));
ase_printf (ASE_T(" -co file Specify the output console file\n"));
ase_printf (ASE_T(" -a str Specify an argument\n"));
}
int awk_main (int argc, ase_char_t* argv[])

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c,v 1.8 2007/05/16 09:15:14 bacon Exp $
* $Id: awk.c,v 1.9 2007/05/24 04:04:44 bacon Exp $
*/
#include <ase/awk/awk.h>
@ -846,6 +846,43 @@ static unsigned int __stdcall run_awk_thr (void* arg)
}
#endif
static int bfn_sleep (
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;
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 == 1) lv = (ase_long_t)rv;
#ifdef _WIN32
Sleep (lv * 1000);
n = 0;
#else
n = sleep (lv);
#endif
r = ase_awk_makeintval (run, n);
if (r == ASE_NULL)
{
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
return -1;
}
ase_awk_setretval (run, r);
return 0;
}
static int awk_main (int argc, ase_char_t* argv[])
{
ase_awk_t* awk;
@ -1016,6 +1053,17 @@ static int awk_main (int argc, ase_char_t* argv[])
app_awk = awk;
if (ase_awk_addbfn (awk,
ASE_T("sleep"), 5, 0,
1, 1, ASE_NULL, bfn_sleep) == ASE_NULL)
{
ase_awk_close (awk);
#ifdef _WIN32
HeapDestroy (mmgr_data.heap);
#endif
return -1;
}
ase_awk_setoption (awk, opt);
/*
@ -1040,6 +1088,9 @@ static int awk_main (int argc, ase_char_t* argv[])
(unsigned int)ase_awk_geterrlin(awk),
ase_awk_geterrmsg(awk));
ase_awk_close (awk);
#ifdef _WIN32
HeapDestroy (mmgr_data.heap);
#endif
return -1;
}
@ -1078,6 +1129,9 @@ static int awk_main (int argc, ase_char_t* argv[])
if (run_awk (awk, mfn, runarg) == -1)
{
ase_awk_close (awk);
#ifdef _WIN32
HeapDestroy (mmgr_data.heap);
#endif
return -1;
}
#endif

View File

@ -9,6 +9,8 @@ BEGIN {
END {
for (c in pop)
printf ("%15s %6d\n",c,pop[c]) | "sort -t' ' +1rn";
close ("sort -t' ' +1rn");
sleep (1);
}
Asia 2173
North America 340