From ce8b23ace2680925ea56fddd6006982bfbcc8446 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 11 May 2007 10:07:00 +0000 Subject: [PATCH] Recovered from cvs revision 2007-05-10 06:17:00 --- ase/awk/Awk.cpp | 176 +++++++++++++++++++++++++++++++++++++++++++-- ase/awk/Awk.hpp | 40 ++++++----- ase/awk/StdAwk.cpp | 8 +-- ase/awk/StdAwk.hpp | 8 +-- ase/awk/awk.h | 5 +- ase/awk/jni.c | 12 ++-- ase/awk/misc.c | 8 ++- ase/awk/val.c | 161 ++++++++++++++++++++++++++++++++--------- ase/awk/val.h | 4 +- 9 files changed, 347 insertions(+), 75 deletions(-) diff --git a/ase/awk/Awk.cpp b/ase/awk/Awk.cpp index 4daedd33..9956c095 100644 --- a/ase/awk/Awk.cpp +++ b/ase/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.14 2007/05/08 15:09:38 bacon Exp $ + * $Id: Awk.cpp,v 1.16 2007/05/09 16:07:44 bacon Exp $ */ #include @@ -75,6 +75,165 @@ namespace ASE return this->mode; } + Awk::Value::Value (): type (ASE_AWK_VAL_NIL) + { + this->str.awk = ASE_NULL; + this->str.ptr = ASE_NULL; + } + + Awk::Value::~Value () + { + if (this->str.ptr != ASE_NULL) + { + ASE_ASSERT (this->str.awk != ASE_NULL); + ase_awk_free (this->str.awk, this->str.ptr); + this->str.ptr = ASE_NULL; + this->str.awk = ASE_NULL; + } + } + + bool Awk::Value::isNil () const + { + return this->type == ASE_AWK_VAL_NIL; + } + + bool Awk::Value::isInt () const + { + return this->type == ASE_AWK_VAL_INT; + } + + bool Awk::Value::isReal () const + { + return this->type == ASE_AWK_VAL_REAL; + } + + bool Awk::Value::isStr () const + { + return this->type == ASE_AWK_VAL_STR; + } + + void Awk::Value::setInt (long_t l) + { + if (this->type == ASE_AWK_VAL_INT && + this->num.l == l) return; + + if (this->str.ptr != ASE_NULL) + { + ASE_ASSERT (this->str.awk != ASE_NULL); + ase_awk_free (this->str.awk, this->str.ptr); + this->str.ptr = ASE_NULL; + this->str.awk = ASE_NULL; + } + + this->type = ASE_AWK_VAL_INT; + this->num.l = l; + } + + void Awk::Value::setReal (real_t r) + { + if (this->type == ASE_AWK_VAL_REAL && + this->num.r == r) return; + + if (this->str.ptr != ASE_NULL) + { + ASE_ASSERT (this->str.awk != ASE_NULL); + ase_awk_free (this->str.awk, this->str.ptr); + this->str.ptr = ASE_NULL; + this->str.awk = ASE_NULL; + } + + this->type = ASE_AWK_VAL_REAL; + this->num.r = r; + } + + const Awk::char_t* Awk::Value::setStr ( + ase_awk_t* awk, const char_t* ptr, size_t len) + { + char_t* tmp = ase_awk_strxdup (awk, ptr, len); + if (tmp == ASE_NULL) return ASE_NULL; + + if (this->str.ptr != ASE_NULL) + { + ASE_ASSERT (this->str.awk != ASE_NULL); + ase_awk_free (this->str.awk, this->str.ptr); + this->str.ptr = ASE_NULL; + this->str.awk = ASE_NULL; + } + + this->str.awk = awk; + this->str.ptr = tmp; + this->str.len = len; + + return tmp; + } + + Awk::long_t Awk::Value::toInt () const + { + switch (this->type) + { + case ASE_AWK_VAL_NIL: + return 0; + + case ASE_AWK_VAL_INT: + return this->num.l; + + case ASE_AWK_VAL_REAL: + return (long_t)this->num.r; + + case ASE_AWK_VAL_STR: + return (ase_awk_strtonum ( + this->str.awk, + this->str.ptr, this->str.len, + &this->num.l, &this->num.r) == 0)? + this->num.l: (long_t)this->num.r; + } + + return 0; + } + + Awk::real_t Awk::Value::toReal () const + { + switch (this->type) + { + case ASE_AWK_VAL_NIL: + return 0.0; + + case ASE_AWK_VAL_INT: + return (real_t)this->num.l; + + case ASE_AWK_VAL_REAL: + return this->num.r; + + case ASE_AWK_VAL_STR: + return (ase_awk_strtonum ( + this->str.awk, + this->str.ptr, this->str.len, + &this->num.l, &this->num.r) == 0)? + (real_t)this->num.l: this->num.r; + } + + return 0.0; + } + + const Awk::char_t* Awk::Value::toStr (ase_awk_t* awk, size_t* len) const + { + switch (this->type) + { + case ASE_AWK_VAL_NIL: + tmp = ase_awk_strxdup(awk, ASE_T(""), 0); + + case ASE_AWK_VAL_INT: + + case ASE_AWK_VAL_REAL: + + case ASE_AWK_VAL_STR: + *len = this->str.len; + return this->str.ptr; + } + + return ASE_NULL; + } + Awk::Awk (): awk (ASE_NULL), functionMap (ASE_NULL), sourceIn (Source::READ), sourceOut (Source::WRITE) { @@ -192,7 +351,7 @@ namespace ASE size_t i, nargs = ase_awk_getnargs(run); - Value* args = new Value[nargs]; + Value* args = new Value [nargs]; for (i = 0; i < nargs; i++) { @@ -210,22 +369,25 @@ namespace ASE break; case ASE_AWK_VAL_STR: - args[i].setStr ( + if (args[i].setStr (awk, ((ase_awk_val_str_t*)v)->buf, - ((ase_awk_val_str_t*)v)->len); + ((ase_awk_val_str_t*)v)->len) == ASE_NULL) + { + // TODO: handle error... + } break; case ASE_AWK_VAL_NIL: - args[i].setNil (); break; } } - Value* ret = (this->*handler) (nargs, args); + Value ret; + int n = (this->*handler) (nargs, args, &ret); delete[] args; - if (ret == ASE_NULL) return -1; + if (n <= -1) return -1; // TODO: convert the return value to normal value... diff --git a/ase/awk/Awk.hpp b/ase/awk/Awk.hpp index f4b111bf..01361471 100644 --- a/ase/awk/Awk.hpp +++ b/ase/awk/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp,v 1.13 2007/05/08 15:09:38 bacon Exp $ + * $Id: Awk.hpp,v 1.15 2007/05/09 16:07:44 bacon Exp $ */ #ifndef _ASE_AWK_AWK_HPP_ @@ -113,33 +113,38 @@ namespace ASE { public: Value (); - Value (long_t l); - Value (real_t r); - Value (char_t* ptr, size_t len); ~Value (); + bool isNil () const; + bool isInt () const; + bool isReal () const; + bool isStr () const; + void setInt (long_t l); void setReal (real_t r); - void setStr (char_t* ptr, size_t len); + const char_t* setStr ( + ase_awk_t* awk, const char_t* ptr, size_t len); - char_t* toStr (size_t* len); - long_t toInt (); - real_t toReal (); + long_t toInt () const; + real_t toReal () const; + const char_t* toStr ( + ase_awk_t* awk, size_t* len) const; protected: - int type; + mutable int type; - union + mutable union { long_t l; real_t r; + } num; - struct - { - char_t* ptr; - size_t len; - } s; - } v; + mutable struct + { + ase_awk_t* awk; + char_t* ptr; + size_t len; + } str; }; Awk (); @@ -152,7 +157,8 @@ namespace ASE virtual int run (const char_t* main = ASE_NULL, const char_t** args = ASE_NULL); - typedef Value* (Awk::*FunctionHandler) (size_t nargs, Value** args); + typedef int (Awk::*FunctionHandler) ( + size_t nargs, const Value* args, Value* ret); virtual int addFunction ( const char_t* name, size_t minArgs, size_t maxArgs, diff --git a/ase/awk/StdAwk.cpp b/ase/awk/StdAwk.cpp index af1db553..06b2f306 100644 --- a/ase/awk/StdAwk.cpp +++ b/ase/awk/StdAwk.cpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.cpp,v 1.5 2007/05/07 09:30:28 bacon Exp $ + * $Id: StdAwk.cpp,v 1.6 2007/05/09 16:07:44 bacon Exp $ */ #include @@ -39,17 +39,17 @@ namespace ASE return n; } - StdAwk::Value* StdAwk::sin (size_t nargs, Value** args) + int StdAwk::sin (size_t nargs, const Value* args, Value* ret) { return 0; } - StdAwk::Value* StdAwk::cos (size_t nargs, Value** args) + int StdAwk::cos (size_t nargs, const Value* args, Value* ret) { return 0; } - StdAwk::Value* StdAwk::tan (size_t nargs, Value** args) + int StdAwk::tan (size_t nargs, const Value* args, Value* ret) { return 0; } diff --git a/ase/awk/StdAwk.hpp b/ase/awk/StdAwk.hpp index 58756724..cc421d05 100644 --- a/ase/awk/StdAwk.hpp +++ b/ase/awk/StdAwk.hpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.hpp,v 1.4 2007/05/07 09:30:28 bacon Exp $ + * $Id: StdAwk.hpp,v 1.5 2007/05/09 16:07:44 bacon Exp $ */ #ifndef _ASE_AWK_STDAWK_HPP_ @@ -18,9 +18,9 @@ namespace ASE int open (); protected: - Value* sin (size_t nargs, Value** args); - Value* cos (size_t nargs, Value** args); - Value* tan (size_t nargs, Value** args); + int sin (size_t nargs, const Value* args, Value* ret); + int cos (size_t nargs, const Value* args, Value* ret); + int tan (size_t nargs, const Value* args, Value* ret); }; } diff --git a/ase/awk/awk.h b/ase/awk/awk.h index d62dec7b..cc19f80e 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.3 2007/04/30 05:47:33 bacon Exp $ + * $Id: awk.h,v 1.4 2007/05/09 16:07:44 bacon Exp $ * * {License} */ @@ -487,6 +487,9 @@ int ase_awk_setrec (ase_awk_run_t* run, ase_size_t idx, const ase_char_t* str, a void* ase_awk_malloc (ase_awk_t* awk, ase_size_t size); void ase_awk_free (ase_awk_t* awk, void* ptr); +ase_char_t* ase_awk_strxdup ( + ase_awk_t* awk, const ase_char_t* ptr, ase_size_t len); + ase_long_t ase_awk_strxtolong ( ase_awk_t* awk, const ase_char_t* str, ase_size_t len, int base, const ase_char_t** endptr); diff --git a/ase/awk/jni.c b/ase/awk/jni.c index 2c96c5e3..6bdf316c 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.4 2007/05/05 12:03:35 bacon Exp $ + * $Id: jni.c,v 1.5 2007/05/09 16:07:44 bacon Exp $ * * {License} */ @@ -2126,6 +2126,9 @@ JNIEXPORT jobject JNICALL Java_ase_awk_Awk_strtonum ( ase_real_t rv; jobject ret; run_data_t* run_data; + ase_awk_t* awk; + + awk = ase_awk_getrunawk ((ase_awk_run_t*)runid); len = (*env)->GetStringLength (env, str); ptr = (*env)->GetStringChars (env, str, JNI_FALSE); @@ -2157,15 +2160,12 @@ JNIEXPORT jobject JNICALL Java_ase_awk_Awk_strtonum ( } for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i]; - n = ase_awk_strtonum ( - (ase_awk_run_t*)runid, tmp, len, &lv, &rv); + n = ase_awk_strtonum (awk, tmp, len, &lv, &rv); free (tmp); } else { - n = ase_awk_strtonum ( - (ase_awk_run_t*)runid, - (ase_char_t*)ptr, len, &lv, &rv); + n = ase_awk_strtonum (awk, (ase_char_t*)ptr, len, &lv, &rv); } (*env)->ReleaseStringChars (env, str, ptr); diff --git a/ase/awk/misc.c b/ase/awk/misc.c index 98b6632e..d872e215 100644 --- a/ase/awk/misc.c +++ b/ase/awk/misc.c @@ -1,5 +1,5 @@ /* - * $Id: misc.c,v 1.3 2007/04/30 05:47:33 bacon Exp $ + * $Id: misc.c,v 1.4 2007/05/09 16:07:44 bacon Exp $ * * {License} */ @@ -16,6 +16,12 @@ void ase_awk_free (ase_awk_t* awk, void* ptr) ASE_AWK_FREE (awk, ptr); } +ase_char_t* ase_awk_strxdup ( + ase_awk_t* awk, const ase_char_t* ptr, ase_size_t len) +{ + return ase_strxdup (ptr, len, &awk->prmfns.mmgr); +} + ase_long_t ase_awk_strxtolong ( ase_awk_t* awk, const ase_char_t* str, ase_size_t len, int base, const ase_char_t** endptr) diff --git a/ase/awk/val.c b/ase/awk/val.c index 35ab57bd..27375b66 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.4 2007/05/05 16:32:46 bacon Exp $ + * $Id: val.c,v 1.5 2007/05/09 16:07:44 bacon Exp $ * * {License} */ @@ -14,11 +14,13 @@ static ase_char_t* str_to_str ( ase_awk_run_t* run, const ase_char_t* str, ase_size_t str_len, int opt, ase_str_t* buf, ase_size_t* len); -static ase_char_t* val_int_to_str ( - ase_awk_run_t* run, ase_awk_val_int_t* v, + +static ase_char_t* int_to_str ( + ase_awk_t* awk, ase_long_t v, int opt, ase_str_t* buf, ase_size_t* len); -static ase_char_t* val_real_to_str ( - ase_awk_run_t* run, ase_awk_val_real_t* v, + +static ase_char_t* real_to_str ( + ase_awk_t* awk, ase_real_t v, int opt, ase_str_t* buf, ase_size_t* len); static ase_awk_val_nil_t awk_nil = { ASE_AWK_VAL_NIL, 0 }; @@ -434,6 +436,7 @@ ase_char_t* ase_awk_valtostr ( if (v->type == ASE_AWK_VAL_INT) { ase_awk_val_int_t* vi = (ase_awk_val_int_t*)v; + ase_char_t* r; /* if (vi->nde != ASE_NULL && vi->nde->str != ASE_NULL) @@ -445,13 +448,20 @@ ase_char_t* ase_awk_valtostr ( else { */ - return val_int_to_str (run, vi, opt, buf, len); + r = int_to_str (run->awk, vi->val, opt, buf, len); + if (r == ASE_NULL) + { + ase_awk_setrunerror ( + run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); + } + return r; /*}*/ } if (v->type == ASE_AWK_VAL_REAL) { ase_awk_val_real_t* vr = (ase_awk_val_real_t*)v; + ase_char_t* r; /* if (vr->nde != ASE_NULL && vr->nde->str != ASE_NULL) @@ -462,7 +472,13 @@ ase_char_t* ase_awk_valtostr ( } else {*/ - return val_real_to_str (run, vr, opt, buf, len); + r = real_to_str (run->awk, vr->val, opt, buf, len); + if (r == ASE_NULL) + { + ase_awk_setrunerror ( + run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); + } + return r; /*}*/ } @@ -520,6 +536,7 @@ static ase_char_t* str_to_str ( } } +#if 0 static ase_char_t* val_int_to_str ( ase_awk_run_t* run, ase_awk_val_int_t* v, int opt, ase_str_t* buf, ase_size_t* len) @@ -617,6 +634,89 @@ static ase_char_t* val_int_to_str ( return tmp; } +#endif + +static ase_char_t* int_to_str ( + ase_awk_t* awk, ase_long_t v, + int opt, ase_str_t* buf, ase_size_t* len) +{ + ase_char_t* tmp; + ase_long_t t; + ase_size_t l = 0; + + t = v; + if (t == 0) + { + /* handle zero */ + if (buf == ASE_NULL) + { + tmp = ASE_AWK_MALLOC ( + awk, 2 * ASE_SIZEOF(ase_char_t)); + if (tmp == ASE_NULL) return ASE_NULL; + + tmp[0] = ASE_T('0'); + tmp[1] = ASE_T('\0'); + if (len != ASE_NULL) *len = 1; + return tmp; + } + else + { + if (opt & ASE_AWK_VALTOSTR_CLEAR) ase_str_clear (buf); + if (ase_str_cat (buf, ASE_T("0")) == (ase_size_t)-1) + { + return ASE_NULL; + } + + if (len != ASE_NULL) *len = ASE_STR_LEN(buf); + return ASE_STR_BUF(buf); + } + } + + /* non-zero values */ + if (t < 0) { t = -t; l++; } + while (t > 0) { l++; t /= 10; } + + if (buf == ASE_NULL) + { + tmp = ASE_AWK_MALLOC (awk, (l + 1) * ASE_SIZEOF(ase_char_t)); + if (tmp == ASE_NULL) return ASE_NULL; + + tmp[l] = ASE_T('\0'); + if (len != ASE_NULL) *len = l; + } + else + { + /* clear the buffer */ + if (opt & ASE_AWK_VALTOSTR_CLEAR) ase_str_clear (buf); + + tmp = ASE_STR_BUF(buf) + ASE_STR_LEN(buf); + + /* extend the buffer */ + if (ase_str_nccat (buf, ASE_T(' '), l) == (ase_size_t)-1) + { + return ASE_NULL; + } + } + + t = v; + if (t < 0) t = -t; + + while (t > 0) + { + tmp[--l] = (ase_char_t)(t % 10) + ASE_T('0'); + t /= 10; + } + + if (v < 0) tmp[--l] = ASE_T('-'); + + if (buf != ASE_NULL) + { + tmp = ASE_STR_BUF(buf); + if (len != ASE_NULL) *len = ASE_STR_LEN(buf); + } + + return tmp; +} static ase_char_t* val_real_to_str ( ase_awk_run_t* run, ase_awk_val_real_t* v, @@ -711,29 +811,9 @@ int ase_awk_valtonum ( if (v->type == ASE_AWK_VAL_STR) { - return ase_awk_strtonum (run, + return ase_awk_strtonum (run->awk, ((ase_awk_val_str_t*)v)->buf, ((ase_awk_val_str_t*)v)->len, l, r); - -#if 0 - const ase_char_t* endptr; - - *l = ase_awk_strxtolong (run->awk, - ((ase_awk_val_str_t*)v)->buf, - ((ase_awk_val_str_t*)v)->len, 0, &endptr); - if (*endptr == ASE_T('.') || - *endptr == ASE_T('E') || - *endptr == ASE_T('e')) - { - *r = ase_awk_strxtoreal (run->awk, - ((ase_awk_val_str_t*)v)->buf, - ((ase_awk_val_str_t*)v)->len, ASE_NULL); -/* TODO: need to check if it is a valid number using endptr for strxtoreal? */ - return 1; /* real */ - } -/* TODO: do should i handle strings ending with invalid number characters like "123xx" or "dkdkdkd"? */ - return 0; /* long */ -#endif } #ifdef DEBUG_VAL @@ -746,22 +826,37 @@ int ase_awk_valtonum ( return -1; /* error */ } +ase_char_t* ase_awk_inumtostr ( + ase_awk_t* awk, ase_long_t v, int opt, ase_str_t* buf, ase_size_t* len) +{ + return int_to_str (awk, v, 0, ASE_NULL, len); +} + +ase_char_t* ase_awk_rnumtostr ( + ase_awk_t* awk, ase_real_t v, int opt, ase_str_t* buf, ase_size_t* len) +{ + return real_to_str (awk, v, 0, ASE_NULL, len); +} + int ase_awk_strtonum ( - ase_awk_run_t* run, const ase_char_t* ptr, ase_size_t len, + ase_awk_t* awk, const ase_char_t* ptr, ase_size_t len, ase_long_t* l, ase_real_t* r) { const ase_char_t* endptr; - *l = ase_awk_strxtolong (run->awk, ptr, len, 0, &endptr); + *l = ase_awk_strxtolong (awk, ptr, len, 0, &endptr); if (*endptr == ASE_T('.') || *endptr == ASE_T('E') || *endptr == ASE_T('e')) { - *r = ase_awk_strxtoreal (run->awk, ptr, len, ASE_NULL); -/* TODO: need to check if it is a valid number using endptr for strxtoreal? */ + *r = ase_awk_strxtoreal (awk, ptr, len, ASE_NULL); + /* TODO: need to check if it is a valid number using + * endptr for strxtoreal? */ return 1; /* real */ } -/* TODO: do should i handle strings ending with invalid number characters like "123xx" or "dkdkdkd"? */ + + /* TODO: do should i handle strings ending with invalid number + * characters like "123xx" or "dkdkdkd"? */ return 0; /* long */ } diff --git a/ase/awk/val.h b/ase/awk/val.h index 1ee8693f..c93a520c 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.3 2007/04/30 05:48:37 bacon Exp $ + * $Id: val.h,v 1.4 2007/05/09 16:07:44 bacon Exp $ * * {License} */ @@ -194,7 +194,7 @@ ase_char_t* ase_awk_valtostr ( int ase_awk_valtonum ( ase_awk_run_t* run, ase_awk_val_t* v, ase_long_t* l, ase_real_t* r); int ase_awk_strtonum ( - ase_awk_run_t* run, const ase_char_t* ptr, ase_size_t len, + ase_awk_t* awk, const ase_char_t* ptr, ase_size_t len, ase_long_t* l, ase_real_t* r); void ase_awk_dprintval (ase_awk_run_t* run, ase_awk_val_t* val);