added QSE_AWK_STRIPSTRSPC

This commit is contained in:
hyung-hwan 2009-07-17 06:43:47 +00:00
parent 6b31c85427
commit 602a14a54e
6 changed files with 68 additions and 47 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c 236 2009-07-16 08:27:53Z hyunghwan.chung $ * $Id: awk.c 237 2009-07-16 12:43:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -261,11 +261,13 @@ static void dprint_return (qse_awk_rtx_t* rtx, qse_awk_val_t* ret)
dprint (QSE_T("[END NAMED VARIABLES]\n")); dprint (QSE_T("[END NAMED VARIABLES]\n"));
} }
#if 0
static void on_statement ( static void on_statement (
qse_awk_rtx_t* run, qse_size_t line, void* data) qse_awk_rtx_t* run, qse_size_t line, void* data)
{ {
/*dprint (L"running %d\n", (int)line);*/ /*dprint (L"running %d\n", (int)line);*/
} }
#endif
static int fnc_sleep (qse_awk_rtx_t* run, const qse_cstr_t* fnm) static int fnc_sleep (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{ {
@ -329,7 +331,8 @@ struct opttab_t
{ QSE_T("rio"), QSE_AWK_RIO, QSE_T("enable builtin I/O including getline & print") }, { QSE_T("rio"), QSE_AWK_RIO, QSE_T("enable builtin I/O including getline & print") },
{ QSE_T("rwpipe"), QSE_AWK_RWPIPE, QSE_T("allow a dual-directional pipe") }, { QSE_T("rwpipe"), QSE_AWK_RWPIPE, QSE_T("allow a dual-directional pipe") },
{ QSE_T("newline"), QSE_AWK_NEWLINE, QSE_T("enable a newline to terminate a statement") }, { QSE_T("newline"), QSE_AWK_NEWLINE, QSE_T("enable a newline to terminate a statement") },
{ QSE_T("stripspaces"), QSE_AWK_STRIPSPACES, QSE_T("strip leading and trailing space of a record") }, { QSE_T("striprecspc"), QSE_AWK_STRIPRECSPC, QSE_T("strip spaces in splitting a record") },
{ QSE_T("stripstrspc"), QSE_AWK_STRIPSTRSPC, QSE_T("strip spaces in converting a string to a number") },
{ QSE_T("nextofile"), QSE_AWK_NEXTOFILE, QSE_T("enable 'nextofile'") }, { QSE_T("nextofile"), QSE_AWK_NEXTOFILE, QSE_T("enable 'nextofile'") },
{ QSE_T("reset"), QSE_AWK_RESET, QSE_T("enable 'reset'") }, { QSE_T("reset"), QSE_AWK_RESET, QSE_T("enable 'reset'") },
{ QSE_T("crlf"), QSE_AWK_CRLF, QSE_T("use CRLF for a newline") }, { QSE_T("crlf"), QSE_AWK_CRLF, QSE_T("use CRLF for a newline") },
@ -674,7 +677,9 @@ static int awk_main (int argc, qse_char_t* argv[])
qse_awk_t* awk = QSE_NULL; qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL; qse_awk_rtx_t* rtx = QSE_NULL;
qse_awk_val_t* retv; qse_awk_val_t* retv;
#if 0
qse_awk_rcb_t rcb; qse_awk_rcb_t rcb;
#endif
int i; int i;
struct arg_t arg; struct arg_t arg;
@ -744,8 +749,10 @@ static int awk_main (int argc, qse_char_t* argv[])
goto oops; goto oops;
} }
#if 0
rcb.on_statement = on_statement; rcb.on_statement = on_statement;
rcb.udd = &arg; rcb.udd = &arg;
#endif
rtx = qse_awk_rtx_openstd (awk, 0, QSE_T("qseawk"), arg.icf, QSE_NULL); rtx = qse_awk_rtx_openstd (awk, 0, QSE_T("qseawk"), arg.icf, QSE_NULL);
if (rtx == QSE_NULL) if (rtx == QSE_NULL)
@ -761,7 +768,9 @@ static int awk_main (int argc, qse_char_t* argv[])
} }
app_rtx = rtx; app_rtx = rtx;
#if 0
qse_awk_rtx_setrcb (rtx, &rcb); qse_awk_rtx_setrcb (rtx, &rcb);
#endif
set_intr_run (); set_intr_run ();

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.hpp 236 2009-07-16 08:27:53Z hyunghwan.chung $ * $Id: Awk.hpp 237 2009-07-16 12:43:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -852,7 +852,8 @@ public:
/** Can terminate a statement with a new line */ /** Can terminate a statement with a new line */
OPT_NEWLINE = QSE_AWK_NEWLINE, OPT_NEWLINE = QSE_AWK_NEWLINE,
OPT_STRIPSPACES = QSE_AWK_STRIPSPACES, OPT_STRIPRECSPC = QSE_AWK_STRIPRECSPC,
OPT_STRIPSTRSPC = QSE_AWK_STRIPSTRSPC,
/** Support the nextofile statement */ /** Support the nextofile statement */
OPT_NEXTOFILE = QSE_AWK_NEXTOFILE, OPT_NEXTOFILE = QSE_AWK_NEXTOFILE,

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 236 2009-07-16 08:27:53Z hyunghwan.chung $ * $Id: awk.h 237 2009-07-16 12:43:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -528,28 +528,33 @@ enum qse_awk_option_t
* for (i = 0; i < NF; i++) print i " [" $(i+1) "]"; * for (i = 0; i < NF; i++) print i " [" $(i+1) "]";
* } * }
* @endcode * @endcode
* " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPSPACES is on. * " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPRSPC is on.
* Otherwise, it is split to [], [a], [b], [c], []. * Otherwise, it is split to [], [a], [b], [c], [].
*/ */
QSE_AWK_STRIPSPACES = (1 << 10), QSE_AWK_STRIPRECSPC = (1 << 10),
/**
* strips off leading spaces when converting a string to a number.
*/
QSE_AWK_STRIPSTRSPC = (1 << 11),
/** enables @b nextofile */ /** enables @b nextofile */
QSE_AWK_NEXTOFILE = (1 << 11), QSE_AWK_NEXTOFILE = (1 << 12),
/** enables @b reset */ /** enables @b reset */
QSE_AWK_RESET = (1 << 12), QSE_AWK_RESET = (1 << 13),
/** CR + LF by default */ /** CR + LF by default */
QSE_AWK_CRLF = (1 << 13), QSE_AWK_CRLF = (1 << 14),
/** allows the assignment of a map value to a variable */ /** allows the assignment of a map value to a variable */
QSE_AWK_MAPTOVAR = (1 << 14), QSE_AWK_MAPTOVAR = (1 << 15),
/** allows @b BEGIN, @b END, pattern-action blocks */ /** allows @b BEGIN, @b END, pattern-action blocks */
QSE_AWK_PABLOCK = (1 << 15), QSE_AWK_PABLOCK = (1 << 16),
/** allows {n,m} in a regular expression. */ /** allows {n,m} in a regular expression. */
QSE_AWK_REXBOUND = (1 << 16), QSE_AWK_REXBOUND = (1 << 17),
/** /**
* performs numeric comparison when a string convertable * performs numeric comparison when a string convertable
@ -559,14 +564,14 @@ enum qse_awk_option_t
* - 9 is greater if #QSE_AWK_NCMPONSTR is off; * - 9 is greater if #QSE_AWK_NCMPONSTR is off;
* - "10.9" is greater if #QSE_AWK_NCMPONSTR is on * - "10.9" is greater if #QSE_AWK_NCMPONSTR is on
*/ */
QSE_AWK_NCMPONSTR = (1 << 17), QSE_AWK_NCMPONSTR = (1 << 18),
/** /**
* enables the strict naming rule. * enables the strict naming rule.
* - a parameter can not be the same as the owning function name. * - a parameter can not be the same as the owning function name.
* - a local variable can not be the same as the owning function name. * - a local variable can not be the same as the owning function name.
*/ */
QSE_AWK_STRICTNAMING = (1 << 18), QSE_AWK_STRICTNAMING = (1 << 19),
/** /**
* makes #qse_awk_t to behave as compatibly as classical AWK * makes #qse_awk_t to behave as compatibly as classical AWK
@ -574,7 +579,7 @@ enum qse_awk_option_t
*/ */
QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO | QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO |
QSE_AWK_NEWLINE | QSE_AWK_PABLOCK | QSE_AWK_NEWLINE | QSE_AWK_PABLOCK |
QSE_AWK_STRICTNAMING QSE_AWK_STRIPSTRSPC | QSE_AWK_STRICTNAMING
}; };
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* $Id: misc.c 230 2009-07-13 08:51:23Z hyunghwan.chung $ * $Id: misc.c 237 2009-07-16 12:43:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -58,8 +58,11 @@ qse_long_t qse_awk_strxtolong (
p = str; p = str;
end = str + len; end = str + len;
if (awk->option & QSE_AWK_STRIPSTRSPC)
{
/* strip off leading spaces */ /* strip off leading spaces */
/*while (QSE_AWK_ISSPACE(awk,*p)) p++;*/ while (QSE_AWK_ISSPACE(awk,*p)) p++;
}
/* check for a sign */ /* check for a sign */
while (p < end) while (p < end)
@ -182,8 +185,11 @@ qse_real_t qse_awk_strtoreal (qse_awk_t* awk, const qse_char_t* str)
p = str; p = str;
/* strip off leading blanks */ if (awk->option & QSE_AWK_STRIPSTRSPC)
/*while (QSE_AWK_ISSPACE(awk,*p)) p++;*/ {
/* strip off leading spaces */
while (QSE_AWK_ISSPACE(awk,*p)) p++;
}
/* check for a sign */ /* check for a sign */
while (*p != QSE_T('\0')) while (*p != QSE_T('\0'))
@ -871,7 +877,7 @@ qse_char_t* qse_awk_rtx_strxntokbyrex (
ptr++; ptr++;
left--; left--;
} }
else if (rtx->awk->option & QSE_AWK_STRIPSPACES) else if (rtx->awk->option & QSE_AWK_STRIPRECSPC)
{ {
/* match at the beginning of the input string */ /* match at the beginning of the input string */
if (match.ptr == substr) if (match.ptr == substr)
@ -917,7 +923,7 @@ exit_loop:
*errnum = QSE_AWK_ENOERR; *errnum = QSE_AWK_ENOERR;
if (rtx->awk->option & QSE_AWK_STRIPSPACES) if (rtx->awk->option & QSE_AWK_STRIPRECSPC)
{ {
return (match.ptr+match.len >= substr+sublen)? return (match.ptr+match.len >= substr+sublen)?
QSE_NULL: ((qse_char_t*)match.ptr+match.len); QSE_NULL: ((qse_char_t*)match.ptr+match.len);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: std.c 235 2009-07-15 10:43:31Z hyunghwan.chung $ * $Id: std.c 237 2009-07-16 12:43:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1316,8 +1316,8 @@ static int fnc_srand (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
a0 = qse_awk_rtx_getarg (run, 0); a0 = qse_awk_rtx_getarg (run, 0);
n = qse_awk_rtx_valtonum (run, a0, &lv, &rv); n = qse_awk_rtx_valtonum (run, a0, &lv, &rv);
if (n == -1) return -1; if (n <= -1) return -1;
if (n == 1) lv = (qse_long_t)rv; if (n >= 1) lv = (qse_long_t)rv;
rxtn->seed = lv; rxtn->seed = lv;
} }
@ -1325,7 +1325,7 @@ static int fnc_srand (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{ {
qse_ntime_t now; qse_ntime_t now;
if (qse_gettime(&now) == -1) rxtn->seed >>= 1; if (qse_gettime(&now) <= -1) rxtn->seed >>= 1;
else rxtn->seed = (unsigned int)now; else rxtn->seed = (unsigned int)now;
} }

View File

@ -37,7 +37,7 @@ static int run_awk (QSE::StdAwk& awk)
"function pa (x) {\n" "function pa (x) {\n"
" reset ret;\n" " reset ret;\n"
" for (i in x) { print i, \"=>\", x[i]; ret += x[i]; }\n" " for (i in x) { print i, \"=>\", x[i]; ret += x[i]; }\n"
" return ret + DAMN++;\n" " return ret + FOO++;\n"
"}\n" "}\n"
"function pb (x) {\n" "function pb (x) {\n"
" reset ret;\n" " reset ret;\n"
@ -46,43 +46,43 @@ static int run_awk (QSE::StdAwk& awk)
"}" "}"
)); ));
// add a global variable 'DAMN' // add a global variable 'FOO'
int damn = awk.addGlobal (QSE_T("DAMN")); int foo = awk.addGlobal (QSE_T("FOO"));
if (damn <= -1) return -1; if (foo <= -1) return -1;
// parse the script and perform no deparsing // parse the script and perform no deparsing
run = awk.parse (in, QSE::StdAwk::Source::NONE); run = awk.parse (in, QSE::StdAwk::Source::NONE);
if (run == QSE_NULL) return -1; if (run == QSE_NULL) return -1;
// set 'FOO' to 100000
QSE::StdAwk::Value foov (run);
if (foov.setInt (100000) <= -1) return -1;
if (awk.setGlobal (foo, foov) <= -1) return -1;
// prepare an indexed parameter // prepare an indexed parameter
QSE::StdAwk::Value arg[1]; QSE::StdAwk::Value arg[1];
for (int i = 1; i <= 5; i++) for (int i = 1; i <= 5; i++)
{ {
if (arg[0].setIndexedInt ( if (arg[0].setIndexedInt (run,
run, QSE::StdAwk::Value::IntIndex(i), i*20) <= -1) return -1; QSE::StdAwk::Value::IntIndex(i), i*20) <= -1) return -1;
} }
// set 'DAMN' to 100000 // prepare a variable to hold the return value
QSE::StdAwk::Value damnv (run);
if (damnv.setInt (100000) <= -1) return -1;
if (awk.setGlobal (damn, damnv) <= -1) return -1;
QSE::StdAwk::Value r; QSE::StdAwk::Value r;
// call the 'pa' function // call the 'pa' function
if (awk.call (QSE_T("pa"), &r, arg, QSE_COUNTOF(arg)) <= -1) return -1; if (awk.call (QSE_T("pa"), &r, arg, 1) <= -1) return -1;
// output the result in various types // output the result in various types
qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)r.toInt()); qse_printf (QSE_T("RESULT: (int) [%lld]\n"), (long long)r.toInt());
qse_printf (QSE_T(" (real)[%Lf]\n"), (long double)r.toReal()); qse_printf (QSE_T(" (real)[%Lf]\n"), (long double)r.toReal());
qse_printf (QSE_T(" (str) [%s]\n"), r.toStr(QSE_NULL)); qse_printf (QSE_T(" (str) [%s]\n"), r.toStr(QSE_NULL));
// get the value of 'DAMN' // get the value of 'FOO'
if (awk.getGlobal (damn, damnv) <= -1) return -1; if (awk.getGlobal (foo, foov) <= -1) return -1;
qse_printf (QSE_T("DAMN: (int) [%lld]\n"), (long long)damnv.toInt()); qse_printf (QSE_T("FOO: (int) [%lld]\n"), (long long)foov.toInt());
qse_printf (QSE_T(" (real) [%Lf]\n"), (long double)damnv.toReal()); qse_printf (QSE_T(" (real)[%Lf]\n"), (long double)foov.toReal());
qse_printf (QSE_T(" (str) [%s]\n"), damnv.toStr(QSE_NULL)); qse_printf (QSE_T(" (str) [%s]\n"), foov.toStr(QSE_NULL));
// call the 'pb' function // call the 'pb' function
if (awk.call (QSE_T("pb"), &r, arg, QSE_COUNTOF(arg)) <= -1) return -1; if (awk.call (QSE_T("pb"), &r, arg, QSE_COUNTOF(arg)) <= -1) return -1;