qse/qse/samples/awk/awk07.cpp

152 lines
4.0 KiB
C++
Raw Normal View History

2009-07-10 06:46:14 +00:00
/*
2009-09-16 04:01:02 +00:00
* $Id$
2009-07-10 06:46:14 +00:00
*
2011-04-23 08:28:43 +00:00
Copyright 2006-2011 Chung, Hyung-Hwan.
2009-09-16 04:01:02 +00:00
This file is part of QSE.
2009-07-10 06:46:14 +00:00
2009-09-16 04:01:02 +00:00
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
2009-07-10 06:46:14 +00:00
2009-09-16 04:01:02 +00:00
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2009-07-10 06:46:14 +00:00
2009-09-16 04:01:02 +00:00
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
2009-07-10 06:46:14 +00:00
*/
#include <qse/awk/StdAwk.hpp>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
2009-08-26 07:07:54 +00:00
static void print_error (
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
2009-07-10 06:46:14 +00:00
{
if (loc.line > 0 || loc.colm > 0)
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
else
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), msg);
2009-07-10 06:46:14 +00:00
}
2009-07-10 06:46:14 +00:00
static int run_awk (QSE::StdAwk& awk)
{
QSE::StdAwk::Run* run;
2009-07-10 06:46:14 +00:00
QSE::StdAwk::SourceString in (QSE_T(
"function pa (x) {\n"
" reset ret;\n"
" for (i in x) { print i, \"=>\", x[i]; ret += x[i]; }\n"
2009-07-17 06:43:47 +00:00
" return ret + FOO++;\n"
"}\n"
"function pb (x) {\n"
" reset ret;\n"
" for (i in x) { ret[-i] = -x[i]; }\n"
" return ret;\n"
"}"
));
2009-07-10 06:46:14 +00:00
2009-07-17 06:43:47 +00:00
// add a global variable 'FOO'
int foo = awk.addGlobal (QSE_T("FOO"));
if (foo <= -1) return -1;
// parse the script and perform no deparsing
run = awk.parse (in, QSE::StdAwk::Source::NONE);
if (run == QSE_NULL) return -1;
2009-07-10 06:46:14 +00:00
2009-07-17 06:43:47 +00:00
// 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
QSE::StdAwk::Value arg[1];
for (int i = 1; i <= 5; i++)
2009-07-10 06:46:14 +00:00
{
2009-07-17 06:43:47 +00:00
if (arg[0].setIndexedInt (run,
QSE::StdAwk::Value::IntIndex(i), i*20) <= -1) return -1;
2009-07-10 06:46:14 +00:00
}
if (arg[0].setIndexedStr (run,
QSE::StdAwk::Value::IntIndex(99), QSE_T("-2345")) <= -1) return -1;
QSE::StdAwk::Value dummy;
if (dummy.setStr (run, QSE_T("4567")) <= -1) return -1;
if (arg[0].setIndexedVal (run,
QSE::StdAwk::Value::IntIndex(999), dummy) <= -1) return -1;
2009-07-10 06:46:14 +00:00
2009-07-17 06:43:47 +00:00
// prepare a variable to hold the return value
QSE::StdAwk::Value r;
2009-07-10 06:46:14 +00:00
// call the 'pa' function
2009-07-17 06:43:47 +00:00
if (awk.call (QSE_T("pa"), &r, arg, 1) <= -1) return -1;
2009-07-10 06:46:14 +00:00
// 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));
2009-07-10 06:46:14 +00:00
2009-07-17 06:43:47 +00:00
// 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());
2009-07-17 06:43:47 +00:00
qse_printf (QSE_T(" (str) [%s]\n"), foov.toStr(QSE_NULL));
// call the 'pb' function
if (awk.call (QSE_T("pb"), &r, arg, QSE_COUNTOF(arg)) <= -1) return -1;
2009-07-10 06:46:14 +00:00
// output the returned map.
QSE_ASSERT (r.isIndexed());
2009-07-10 06:46:14 +00:00
QSE::StdAwk::Value::IndexIterator iter;
QSE::StdAwk::Value::Index idx;
QSE::StdAwk::Value v;
2009-07-10 06:46:14 +00:00
qse_printf (QSE_T("RESULT:\n"));
2009-07-10 06:46:14 +00:00
iter = r.getFirstIndex (&idx);
while (iter != QSE::StdAwk::Value::IndexIterator::END)
2009-07-10 06:46:14 +00:00
{
if (r.getIndexed (idx, &v) <= -1) return -1;
qse_printf (QSE_T("\t[%.*s]=>[%lld]\n"),
2011-05-25 09:14:58 +00:00
(int)idx.length(), idx.pointer(),
(long long)v.toInt()
);
iter = r.getNextIndex (&idx, iter);
2009-07-10 06:46:14 +00:00
}
return 0;
2009-07-10 06:46:14 +00:00
}
static int awk_main (int argc, qse_char_t* argv[])
{
QSE::StdAwk awk;
2009-07-10 06:46:14 +00:00
int ret = awk.open();
2009-07-10 06:46:14 +00:00
// allow returning a map from a function and enable 'reset'
awk.setOption (
awk.getOption() |
2009-09-16 04:01:02 +00:00
QSE_AWK_MAPTOVAR |
QSE_AWK_RESET);
2009-07-10 06:46:14 +00:00
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1)
{
2009-08-26 07:07:54 +00:00
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
2009-07-10 06:46:14 +00:00
awk.close ();
return -1;
2009-07-10 06:46:14 +00:00
}
int qse_main (int argc, qse_achar_t* argv[])
{
return qse_runmain (argc,argv,awk_main);
}