From 573570870a5b9a9868aaa3c3daa8e2bfb9c3f250 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 12 May 2007 02:21:00 +0000 Subject: [PATCH] Recovered from cvs revision 2007-05-11 12:14:00 --- ase/awk/Awk.cpp | 147 +++++++++++++++++++++++------------------------- ase/awk/Awk.hpp | 30 +++++----- ase/awk/jni.c | 12 ++-- ase/awk/val.c | 135 ++++---------------------------------------- ase/awk/val.h | 4 +- 5 files changed, 105 insertions(+), 223 deletions(-) diff --git a/ase/awk/Awk.cpp b/ase/awk/Awk.cpp index 9956c095..982b47bc 100644 --- a/ase/awk/Awk.cpp +++ b/ase/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.16 2007/05/09 16:07:44 bacon Exp $ + * $Id: Awk.cpp,v 1.17 2007/05/10 16:08:37 bacon Exp $ */ #include @@ -75,41 +75,102 @@ namespace ASE return this->mode; } - Awk::Value::Value (): type (ASE_AWK_VAL_NIL) + Awk::Value::Value (): run (ASE_NULL), val (ASE_NULL) { - this->str.awk = ASE_NULL; this->str.ptr = ASE_NULL; + this->str.len = 0; } 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); + ASE_ASSERT (this->run != ASE_NULL); + ase_awk_free ( + ase_awk_getrunawk(this->run), this->str.ptr); this->str.ptr = ASE_NULL; - this->str.awk = ASE_NULL; + this->str.len = 0; } + + if (this->val != ASE_NULL) + { + ASE_ASSERT (this->run != ASE_NULL); + ase_awk_refdownval ( + ase_awk_getrunawk(this->run), this->val); + } + } + + void Awk::Value::init (ase_awk_run_t* run, ase_awk_val_t* val) + { + /* this method is used internally. + * and should never be called more than once */ + ASE_ASSERT (this->run == ASE_NULL && this->val == ASE_NULL && + run != ASE_NULL && val != ASE_NULL); + + this->run = run; + this->val = val; + + ase_awk_refupval (this->run, this->val); } bool Awk::Value::isNil () const { - return this->type == ASE_AWK_VAL_NIL; + return val == ASE_NULL || val->type == ASE_AWK_VAL_NIL; } bool Awk::Value::isInt () const { - return this->type == ASE_AWK_VAL_INT; + return val != ASE_NULL && val->type == ASE_AWK_VAL_INT; } bool Awk::Value::isReal () const { - return this->type == ASE_AWK_VAL_REAL; + return val != ASE_NULL && val->type == ASE_AWK_VAL_REAL; } bool Awk::Value::isStr () const { - return this->type == ASE_AWK_VAL_STR; + return val != ASE_NULL && val->type == ASE_AWK_VAL_STR; + } + + Awk::long_t Awk::Value::toInt () const + { + if (this->val == ASE_NULL) return 0; + + long_t l; + real_t r; + + n = ase_awk_valtonum (this->run, this->val, &l, &r); + if (n == 0) return 1; + if (n == 1) return (long_t)r; + + return 0; /* return 0 on error */ + } + + Awk::real_t Awk::Value::toReal () const + { + if (this->val == ASE_NULL) return 0; + + long_t l; + real_t r; + + n = ase_awk_valtonum (this->run, this->val, &l, &r); + if (n == 0) return (real_t)1; + if (n == 1) return r; + + return 0.0; /* return 0 on error */ + } + + const Awk::char_t* Awk::Value::toStr (ase_awk_t* awk, size_t* len) const + { + if (this->val == ASE_NULL) + { + tmp = ase_awk_strxdup( + ase_awk_getrunawk(this->run), ASE_T(""), 0); + } + else + { + } } void Awk::Value::setInt (long_t l) @@ -167,72 +228,6 @@ namespace ASE 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) diff --git a/ase/awk/Awk.hpp b/ase/awk/Awk.hpp index 01361471..97ec141c 100644 --- a/ase/awk/Awk.hpp +++ b/ase/awk/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp,v 1.15 2007/05/09 16:07:44 bacon Exp $ + * $Id: Awk.hpp,v 1.16 2007/05/10 16:08:37 bacon Exp $ */ #ifndef _ASE_AWK_AWK_HPP_ @@ -115,35 +115,33 @@ namespace ASE Value (); ~Value (); + protected: + void init (ase_awk_run_t* run, ase_awk_val_t* v); + + public: bool isNil () const; bool isInt () const; bool isReal () const; bool isStr () const; - void setInt (long_t l); - void setReal (real_t r); - const char_t* setStr ( - ase_awk_t* awk, const char_t* ptr, size_t len); - long_t toInt () const; real_t toReal () const; const char_t* toStr ( ase_awk_t* awk, size_t* len) const; - protected: - mutable int type; + void setInt (long_t l); + void setReal (real_t r); + const char_t* setStr ( + ase_awk_t* awk, const char_t* ptr, size_t len); - mutable union - { - long_t l; - real_t r; - } num; + protected: + ase_awk_run_t* run; + ase_awk_val_t* val; mutable struct { - ase_awk_t* awk; - char_t* ptr; - size_t len; + char_t* ptr; + size_t len; } str; }; diff --git a/ase/awk/jni.c b/ase/awk/jni.c index 6bdf316c..e760a002 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.5 2007/05/09 16:07:44 bacon Exp $ + * $Id: jni.c,v 1.6 2007/05/10 16:08:37 bacon Exp $ * * {License} */ @@ -2126,9 +2126,6 @@ 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); @@ -2160,12 +2157,15 @@ 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 (awk, tmp, len, &lv, &rv); + n = ase_awk_strtonum ( + (ase_awk_run_t*)runid, tmp, len, &lv, &rv); free (tmp); } else { - n = ase_awk_strtonum (awk, (ase_char_t*)ptr, len, &lv, &rv); + n = ase_awk_strtonum ( + (ase_awk_run_t*)runid, + (ase_char_t*)ptr, len, &lv, &rv); } (*env)->ReleaseStringChars (env, str, ptr); diff --git a/ase/awk/val.c b/ase/awk/val.c index 27375b66..0ed541e2 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.5 2007/05/09 16:07:44 bacon Exp $ + * $Id: val.c,v 1.6 2007/05/10 16:08:37 bacon Exp $ * * {License} */ @@ -15,12 +15,12 @@ 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* int_to_str ( - ase_awk_t* awk, ase_long_t v, +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); -static ase_char_t* real_to_str ( - ase_awk_t* awk, ase_real_t v, +static ase_char_t* val_real_to_str ( + ase_awk_run_t* run, ase_awk_val_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 }; @@ -436,7 +436,6 @@ 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) @@ -446,22 +445,14 @@ ase_char_t* ase_awk_valtostr ( opt, buf, len); } else - { - */ - 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; + {*/ + return val_int_to_str (run, vi, opt, buf, len); /*}*/ } 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) @@ -472,13 +463,7 @@ ase_char_t* ase_awk_valtostr ( } else {*/ - 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; + return val_real_to_str (run, vr, opt, buf, len); /*}*/ } @@ -536,7 +521,6 @@ 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) @@ -634,89 +618,6 @@ 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, @@ -811,7 +712,7 @@ int ase_awk_valtonum ( if (v->type == ASE_AWK_VAL_STR) { - return ase_awk_strtonum (run->awk, + return ase_awk_strtonum (run, ((ase_awk_val_str_t*)v)->buf, ((ase_awk_val_str_t*)v)->len, l, r); } @@ -826,30 +727,18 @@ 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_t* awk, const ase_char_t* ptr, ase_size_t len, + ase_awk_run_t* run, 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 (awk, ptr, len, 0, &endptr); + *l = ase_awk_strxtolong (run->awk, ptr, len, 0, &endptr); if (*endptr == ASE_T('.') || *endptr == ASE_T('E') || *endptr == ASE_T('e')) { - *r = ase_awk_strxtoreal (awk, ptr, len, ASE_NULL); + *r = ase_awk_strxtoreal (run->awk, ptr, len, ASE_NULL); /* TODO: need to check if it is a valid number using * endptr for strxtoreal? */ return 1; /* real */ diff --git a/ase/awk/val.h b/ase/awk/val.h index c93a520c..240c3e43 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.4 2007/05/09 16:07:44 bacon Exp $ + * $Id: val.h,v 1.5 2007/05/10 16:08:37 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_t* awk, const ase_char_t* ptr, ase_size_t len, + ase_awk_run_t* run, 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);