added mbs handling methods to Awk::Value

This commit is contained in:
hyung-hwan 2019-08-29 03:13:57 +00:00
parent 76d6999f37
commit d5320cfd30
4 changed files with 306 additions and 133 deletions

View File

@ -697,6 +697,9 @@ public:
operator int_t () const;
operator flt_t () const;
operator const char_t* () const;
#if defined(QSE_CHAR_IS_WCHAR)
operator const qse_mchar_t* () const;
#endif
val_t* toVal () const
{
@ -718,13 +721,28 @@ public:
const char_t* p;
size_t l;
if (getStr (&p, &l) == -1)
if (this->getStr(&p, &l) <= -1)
{
p = getEmptyStr();
p = this->getEmptyStr();
l = 0;
}
if (len != QSE_NULL) *len = l;
if (len) *len = l;
return p;
}
const qse_mchar_t* toMbs (size_t* len) const
{
const qse_mchar_t* p;
size_t l;
if (this->getMbs(&p, &l) <= -1)
{
p = this->getEmptyMbs();
l = 0;
}
if (len) *len = l;
return p;
}
@ -732,6 +750,7 @@ public:
int getFlt (flt_t* v) const;
int getNum (int_t* lv, flt_t* fv) const;
int getStr (const char_t** str, size_t* len) const;
int getMbs (const qse_mchar_t** str, size_t* len) const;
int setVal (val_t* v);
int setVal (Run* r, val_t* v);
@ -740,70 +759,33 @@ public:
int setInt (Run* r, int_t v);
int setFlt (flt_t v);
int setFlt (Run* r, flt_t v);
int setStr (const char_t* str, size_t len, bool numeric = false);
int setStr (Run* r, const char_t* str, size_t len, bool numeric = false);
int setStr (const char_t* str, bool numeric = false);
int setStr (Run* r, const char_t* str, bool numeric = false);
int setIndexedVal (
const Index& idx,
val_t* v
);
int setMbs (const qse_mchar_t* str, size_t len);
int setMbs (Run* r, const qse_mchar_t* str, size_t len);
int setMbs (const qse_mchar_t* str);
int setMbs (Run* r, const qse_mchar_t* str);
int setIndexedVal (
Run* r,
const Index& idx,
val_t* v
);
int setIndexedVal (const Index& idx, val_t* v);
int setIndexedVal (Run* r, const Index& idx, val_t* v);
int setIndexedInt (const Index& idx, int_t v);
int setIndexedInt (Run* r, const Index& idx, int_t v);
int setIndexedFlt (const Index& idx, flt_t v);
int setIndexedFlt (Run* r, const Index& idx, flt_t v);
int setIndexedInt (
const Index& idx,
int_t v
);
int setIndexedStr (const Index& idx, const char_t* str, size_t len, bool numeric = false);
int setIndexedStr (Run* r, const Index& idx, const char_t* str, size_t len, bool numeric = false);
int setIndexedStr (const Index& idx, const char_t* str, bool numeric = false);
int setIndexedStr (Run* r, const Index& idx, const char_t* str, bool numeric = false);
int setIndexedInt (
Run* r,
const Index& idx,
int_t v);
int setIndexedFlt (
const Index& idx,
flt_t v
);
int setIndexedFlt (
Run* r,
const Index& idx,
flt_t v
);
int setIndexedStr (
const Index& idx,
const char_t* str,
size_t len,
bool numeric = false
);
int setIndexedStr (
Run* r,
const Index& idx,
const char_t* str,
size_t len,
bool numeric = false
);
int setIndexedStr (
const Index& idx,
const char_t* str,
bool numeric = false
);
int setIndexedStr (
Run* r,
const Index& idx,
const char_t* str,
bool numeric = false
);
int setIndexedMbs (const Index& idx, const qse_mchar_t* str, size_t len);
int setIndexedMbs (Run* r, const Index& idx, const qse_mchar_t* str, size_t len);
int setIndexedMbs (const Index& idx, const qse_mchar_t* str);
int setIndexedMbs (Run* r, const Index& idx, const qse_mchar_t* str);
///
/// The isIndexed() function determines if a value is arrayed.
@ -850,10 +832,12 @@ public:
mutable struct
{
qse_cstr_t str;
qse_mcstr_t mbs;
} cached;
public:
static const char_t* getEmptyStr();
static const qse_mchar_t* getEmptyMbs();
};
public:

View File

@ -210,6 +210,11 @@ const Awk::char_t* Awk::Value::getEmptyStr()
static const Awk::char_t* EMPTY_STRING = QSE_T("");
return EMPTY_STRING;
}
const qse_mchar_t* Awk::Value::getEmptyMbs()
{
static const qse_mchar_t* EMPTY_STRING = QSE_MT("");
return EMPTY_STRING;
}
Awk::Value::IntIndex::IntIndex (int_t x)
{
@ -309,27 +314,34 @@ Awk::Value::Value (): run (QSE_NULL), val (qse_get_awk_nil_val())
{
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
Awk::Value::Value (Run& run): run (&run), val (qse_get_awk_nil_val())
{
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
Awk::Value::Value (Run* run): run (run), val (qse_get_awk_nil_val())
{
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
Awk::Value::Value (const Value& v): run(v.run), val(v.val)
{
if (run != QSE_NULL)
qse_awk_rtx_refupval (run->rtx, val);
if (run) qse_awk_rtx_refupval (run->rtx, val);
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
Awk::Value::~Value ()
@ -337,8 +349,8 @@ Awk::Value::~Value ()
if (run != QSE_NULL)
{
qse_awk_rtx_refdownval (run->rtx, val);
if (cached.str.ptr != QSE_NULL)
qse_awk_rtx_freemem (run->rtx, cached.str.ptr);
if (cached.str.ptr) qse_awk_rtx_freemem (run->rtx, cached.str.ptr);
if (cached.mbs.ptr) qse_awk_rtx_freemem (run->rtx, cached.mbs.ptr);
}
}
@ -346,38 +358,49 @@ Awk::Value& Awk::Value::operator= (const Value& v)
{
if (this == &v) return *this;
if (run != QSE_NULL)
if (run)
{
qse_awk_rtx_refdownval (run->rtx, val);
if (cached.str.ptr != QSE_NULL)
if (cached.str.ptr)
{
qse_awk_rtx_freemem (run->rtx, cached.str.ptr);
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
}
if (cached.mbs.ptr)
{
qse_awk_rtx_freemem (run->rtx, cached.mbs.ptr);
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
}
run = v.run;
val = v.val;
if (run != QSE_NULL)
qse_awk_rtx_refupval (run->rtx, val);
if (run) qse_awk_rtx_refupval (run->rtx, val);
return *this;
}
void Awk::Value::clear ()
{
if (run != QSE_NULL)
if (run)
{
qse_awk_rtx_refdownval (run->rtx, val);
if (cached.str.ptr != QSE_NULL)
if (cached.str.ptr)
{
qse_awk_rtx_freemem (run->rtx, cached.str.ptr);
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
}
if (cached.mbs.ptr)
{
qse_awk_rtx_freemem (run->rtx, cached.mbs.ptr);
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
run = QSE_NULL;
val = qse_get_awk_nil_val();
@ -387,14 +410,14 @@ void Awk::Value::clear ()
Awk::Value::operator Awk::int_t () const
{
int_t v;
if (getInt (&v) <= -1) v = 0;
if (this->getInt(&v) <= -1) v = 0;
return v;
}
Awk::Value::operator Awk::flt_t () const
{
flt_t v;
if (getFlt (&v) <= -1) v = 0.0;
if (this->getFlt(&v) <= -1) v = 0.0;
return v;
}
@ -406,13 +429,23 @@ Awk::Value::operator const Awk::char_t* () const
return ptr;
}
#if defined(QSE_CHAR_IS_WCHAR)
Awk::Value::operator const qse_mchar_t* () const
{
const qse_mchar_t* ptr;
size_t len;
if (Awk::Value::getMbs(&ptr, &len) <= -1) ptr = getEmptyMbs();
return ptr;
}
#endif
int Awk::Value::getInt (int_t* v) const
{
int_t lv = 0;
QSE_ASSERT (this->val != QSE_NULL);
if (run != QSE_NULL)
if (this->run)
{
int n = qse_awk_rtx_valtoint(this->run->rtx, this->val, &lv);
if (n <= -1)
@ -450,7 +483,7 @@ int Awk::Value::getNum (int_t* lv, flt_t* fv) const
{
QSE_ASSERT (this->val != QSE_NULL);
if (this->run != QSE_NULL)
if (this->run)
{
int n = qse_awk_rtx_valtonum(this->run->rtx, this->val, lv, fv);
if (n <= -1)
@ -481,7 +514,7 @@ int Awk::Value::getStr (const char_t** str, size_t* len) const
}
else
{
if (cached.str.ptr == QSE_NULL)
if (!cached.str.ptr)
{
qse_awk_rtx_valtostr_out_t out;
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
@ -511,6 +544,48 @@ int Awk::Value::getStr (const char_t** str, size_t* len) const
return 0;
}
int Awk::Value::getMbs (const qse_mchar_t** str, size_t* len) const
{
const qse_mchar_t* p = getEmptyMbs();
size_t l = 0;
QSE_ASSERT (this->val != QSE_NULL);
if (this->run)
{
if (QSE_AWK_RTX_GETVALTYPE(this->run->rtx, this->val) == QSE_AWK_VAL_MBS)
{
p = ((qse_awk_val_mbs_t*)this->val)->val.ptr;
l = ((qse_awk_val_mbs_t*)this->val)->val.len;
}
else
{
if (!cached.mbs.ptr)
{
p = qse_awk_rtx_valtombsdup(this->run->rtx, this->val, &l);
if (!p)
{
run->awk->retrieveError (this->run);
return -1;
}
cached.mbs.ptr = (qse_mchar_t*)p;
cached.mbs.len = l;
}
else
{
p = cached.mbs.ptr;
l = cached.mbs.len;
}
}
}
*str = p;
*len = l;
return 0;
}
int Awk::Value::setVal (val_t* v)
{
if (this->run == QSE_NULL)
@ -519,7 +594,7 @@ int Awk::Value::setVal (val_t* v)
* set an error number for the same reason */
return -1;
}
return setVal (this->run, v);
return this->setVal(this->run, v);
}
int Awk::Value::setVal (Run* r, val_t* v)
@ -527,15 +602,22 @@ int Awk::Value::setVal (Run* r, val_t* v)
if (this->run != QSE_NULL)
{
qse_awk_rtx_refdownval (this->run->rtx, val);
if (cached.str.ptr != QSE_NULL)
if (cached.str.ptr)
{
qse_awk_rtx_freemem (this->run->rtx, cached.str.ptr);
cached.str.ptr = QSE_NULL;
cached.str.len = 0;
}
if (cached.mbs.ptr)
{
qse_awk_rtx_freemem (this->run->rtx, cached.mbs.ptr);
cached.mbs.ptr = QSE_NULL;
cached.mbs.len = 0;
}
}
QSE_ASSERT (cached.str.ptr == QSE_NULL);
QSE_ASSERT (cached.mbs.ptr == QSE_NULL);
qse_awk_rtx_refupval (r->rtx, v);
this->run = r;
@ -552,7 +634,7 @@ int Awk::Value::setInt (int_t v)
* set an error number for the same reason */
return -1;
}
return setInt (this->run, v);
return this->setInt(this->run, v);
}
int Awk::Value::setInt (Run* r, int_t v)
@ -565,7 +647,7 @@ int Awk::Value::setInt (Run* r, int_t v)
return -1;
}
int n = setVal (r, tmp);
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
@ -578,7 +660,7 @@ int Awk::Value::setFlt (flt_t v)
* set an error number for the same reason */
return -1;
}
return setFlt (this->run, v);
return this->setFlt(this->run, v);
}
int Awk::Value::setFlt (Run* r, flt_t v)
@ -591,7 +673,7 @@ int Awk::Value::setFlt (Run* r, flt_t v)
return -1;
}
int n = setVal (r, tmp);
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
@ -604,7 +686,7 @@ int Awk::Value::setStr (const char_t* str, size_t len, bool numeric)
* set an error number for the same reason */
return -1;
}
return setStr (this->run, str, len, numeric);
return this->setStr(this->run, str, len, numeric);
}
int Awk::Value::setStr (Run* r, const char_t* str, size_t len, bool numeric)
@ -623,7 +705,7 @@ int Awk::Value::setStr (Run* r, const char_t* str, size_t len, bool numeric)
return -1;
}
int n = setVal (r, tmp);
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
@ -631,7 +713,7 @@ int Awk::Value::setStr (Run* r, const char_t* str, size_t len, bool numeric)
int Awk::Value::setStr (const char_t* str, bool numeric)
{
if (this->run == QSE_NULL) return -1;
return setStr (this->run, str, numeric);
return this->setStr(this->run, str, numeric);
}
int Awk::Value::setStr (Run* r, const char_t* str, bool numeric)
@ -645,7 +727,60 @@ int Awk::Value::setStr (Run* r, const char_t* str, bool numeric)
return -1;
}
int n = setVal (r, tmp);
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
int Awk::Value::setMbs (const qse_mchar_t* str, size_t len)
{
if (this->run == QSE_NULL)
{
/* no runtime context assoicated. unfortunately, i can't
* set an error number for the same reason */
return -1;
}
return this->setMbs(this->run, str, len);
}
int Awk::Value::setMbs (Run* r, const qse_mchar_t* str, size_t len)
{
val_t* tmp;
qse_mcstr_t cstr;
cstr.ptr = (qse_mchar_t*)str;
cstr.len = len;
tmp = qse_awk_rtx_makembsvalwithmcstr(r->rtx, &cstr);
if (!tmp)
{
r->awk->retrieveError (r);
return -1;
}
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
int Awk::Value::setMbs (const qse_mchar_t* str)
{
if (this->run == QSE_NULL) return -1;
return this->setMbs(this->run, str);
}
int Awk::Value::setMbs (Run* r, const qse_mchar_t* str)
{
val_t* tmp;
tmp = qse_awk_rtx_makembsval(r->rtx, str, qse_mbslen(str));
if (!tmp)
{
r->awk->retrieveError (r);
return -1;
}
int n = this->setVal(r, tmp);
QSE_ASSERT (n == 0);
return n;
}
@ -653,7 +788,7 @@ int Awk::Value::setStr (Run* r, const char_t* str, bool numeric)
int Awk::Value::setIndexedVal (const Index& idx, val_t* v)
{
if (this->run == QSE_NULL) return -1;
return setIndexedVal (this->run, idx, v);
return this->setIndexedVal (this->run, idx, v);
}
int Awk::Value::setIndexedVal (Run* r, const Index& idx, val_t* v)
@ -720,7 +855,7 @@ int Awk::Value::setIndexedVal (Run* r, const Index& idx, val_t* v)
int Awk::Value::setIndexedInt (const Index& idx, int_t v)
{
if (run == QSE_NULL) return -1;
return setIndexedInt (run, idx, v);
return this->setIndexedInt (run, idx, v);
}
int Awk::Value::setIndexedInt (Run* r, const Index& idx, int_t v)
@ -733,7 +868,7 @@ int Awk::Value::setIndexedInt (Run* r, const Index& idx, int_t v)
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = setIndexedVal (r, idx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
@ -742,7 +877,7 @@ int Awk::Value::setIndexedInt (Run* r, const Index& idx, int_t v)
int Awk::Value::setIndexedFlt (const Index& idx, flt_t v)
{
if (run == QSE_NULL) return -1;
return setIndexedFlt (run, idx, v);
return this->setIndexedFlt(run, idx, v);
}
int Awk::Value::setIndexedFlt (Run* r, const Index& idx, flt_t v)
@ -755,7 +890,7 @@ int Awk::Value::setIndexedFlt (Run* r, const Index& idx, flt_t v)
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = setIndexedVal (r, idx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
@ -764,7 +899,7 @@ int Awk::Value::setIndexedFlt (Run* r, const Index& idx, flt_t v)
int Awk::Value::setIndexedStr (const Index& idx, const char_t* str, size_t len, bool numeric)
{
if (run == QSE_NULL) return -1;
return setIndexedStr (run, idx, str, len, numeric);
return this->setIndexedStr(run, idx, str, len, numeric);
}
int Awk::Value::setIndexedStr (
@ -785,7 +920,7 @@ int Awk::Value::setIndexedStr (
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = setIndexedVal (r, idx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
@ -794,7 +929,7 @@ int Awk::Value::setIndexedStr (
int Awk::Value::setIndexedStr (const Index& idx, const char_t* str, bool numeric)
{
if (run == QSE_NULL) return -1;
return setIndexedStr (run, idx, str, numeric);
return this->setIndexedStr(run, idx, str, numeric);
}
int Awk::Value::setIndexedStr (Run* r, const Index& idx, const char_t* str, bool numeric)
@ -809,12 +944,64 @@ int Awk::Value::setIndexedStr (Run* r, const Index& idx, const char_t* str, bool
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = setIndexedVal (r, idx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
}
int Awk::Value::setIndexedMbs (const Index& idx, const qse_mchar_t* str, size_t len)
{
if (run == QSE_NULL) return -1;
return this->setIndexedMbs(run, idx, str, len);
}
int Awk::Value::setIndexedMbs (Run* r, const Index& idx, const qse_mchar_t* str, size_t len)
{
val_t* tmp;
qse_mcstr_t cstr;
cstr.ptr = (qse_mchar_t*)str;
cstr.len = len;
tmp = qse_awk_rtx_makembsvalwithmcstr(r->rtx, &cstr);
if (!tmp)
{
r->awk->retrieveError (r);
return -1;
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
}
int Awk::Value::setIndexedMbs (const Index& idx, const qse_mchar_t* str)
{
if (run == QSE_NULL) return -1;
return this->setIndexedMbs(run, idx, str);
}
int Awk::Value::setIndexedMbs (Run* r, const Index& idx, const qse_mchar_t* str)
{
val_t* tmp;
tmp = qse_awk_rtx_makembsval(r->rtx, str, qse_mbslen(str));
if (tmp == QSE_NULL)
{
r->awk->retrieveError (r);
return -1;
}
qse_awk_rtx_refupval (r->rtx, tmp);
int n = this->setIndexedVal(r, idx, tmp);
qse_awk_rtx_refdownval (r->rtx, tmp);
return n;
}
bool Awk::Value::isIndexed () const
{
QSE_ASSERT (val != QSE_NULL);
@ -833,8 +1020,7 @@ int Awk::Value::getIndexed (const Index& idx, Value* v) const
}
// get the value from the map.
val_t* fv = qse_awk_rtx_getmapvalfld (
this->run->rtx, val, (char_t*)idx.ptr, idx.len);
val_t* fv = qse_awk_rtx_getmapvalfld(this->run->rtx, val, (char_t*)idx.ptr, idx.len);
// the key is not found. it is not an error. v is just nil
if (fv == QSE_NULL)
@ -1647,8 +1833,7 @@ int Awk::xstrs_t::add (awk_t* awk, const char_t* arg, size_t len)
size_t capa = this->capa;
capa += 64;
ptr = (qse_cstr_t*) qse_awk_reallocmem (
awk, this->ptr, QSE_SIZEOF(qse_cstr_t)*(capa+1));
ptr = (qse_cstr_t*)qse_awk_reallocmem(awk, this->ptr, QSE_SIZEOF(qse_cstr_t)*(capa+1));
if (ptr == QSE_NULL) return -1;
this->ptr = ptr;

View File

@ -99,13 +99,15 @@ static int run_awk (QSE::StdAwk& awk)
// output the result in various types
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)r.toInt());
qse_printf (QSE_T(" (flt)[%Lf]\n"), (long double)r.toFlt());
qse_printf (QSE_T(" (str) [%s]\n"), r.toStr(QSE_NULL));
qse_printf (QSE_T(" (str) [%js]\n"), r.toStr(QSE_NULL));
qse_printf (QSE_T(" (mbs) [%hs]\n"), r.toMbs(QSE_NULL));
// get the value of 'FOO'
if (awk.getGlobal (foo, foov) <= -1) return -1;
qse_printf (QSE_T("FOO: (int) [%lld]\n"), (long long)foov.toInt());
qse_printf (QSE_T(" (flt)[%Lf]\n"), (long double)foov.toFlt());
qse_printf (QSE_T(" (str) [%s]\n"), foov.toStr(QSE_NULL));
qse_printf (QSE_T(" (str) [%js]\n"), foov.toStr(QSE_NULL));
qse_printf (QSE_T(" (mbs) [%hs]\n"), foov.toMbs(QSE_NULL));
// call the 'pb' function
if (awk.call (QSE_T("pb"), &r, arg, QSE_COUNTOF(arg)) <= -1) return -1;

View File

@ -82,7 +82,8 @@ static int run_awk (QSE::StdAwk& awk)
// output the result in various types
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
qse_printf (QSE_T(" (str) [%js]\n"), ret.toStr(QSE_NULL));
qse_printf (QSE_T(" (mbs) [%hs]\n"), ret.toMbs(QSE_NULL));
// ret = sine (ret);
arg[0] = ret;
@ -91,7 +92,8 @@ static int run_awk (QSE::StdAwk& awk)
// output the result in various types
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)ret.toInt());
qse_printf (QSE_T(" (flt) [%Lf]\n"), (long double)ret.toFlt());
qse_printf (QSE_T(" (str) [%s]\n"), ret.toStr(QSE_NULL));
qse_printf (QSE_T(" (str) [%js]\n"), ret.toStr(QSE_NULL));
qse_printf (QSE_T(" (mbs) [%hs]\n"), ret.toMbs(QSE_NULL));
return 0;
}