updated sed docs

This commit is contained in:
2013-01-10 14:17:53 +00:00
parent 686f63f665
commit 4581bf1e4e
13 changed files with 509 additions and 617 deletions

View File

@ -1,35 +1,71 @@
/*
* $Id$
*
Copyright 2006-2012 Chung, Hyung-Hwan.
This file is part of QSE.
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.
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.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/awk/StdAwk.hpp>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <string>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#if defined(QSE_CHAR_IS_WCHAR)
typedef std::wstring String;
#else
typedef std::string String;
#endif
typedef QSE::StdAwk StdAwk;
typedef QSE::StdAwk::Run Run;
typedef QSE::StdAwk::Value Value;
class MyAwk: public StdAwk
{
public:
//
// this class overrides console methods to use
// string buffers for console input and output.
//
MyAwk () { }
~MyAwk () { close (); }
void setInput (const char_t* instr)
{
this->input = instr;
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
void clearOutput () { this->output.clear (); }
const char_t* getOutput () { return this->output.c_str(); }
protected:
String input; // console input buffer
const char_t* inptr;
const char_t* inend;
String output; // console output buffer
int openConsole (Console& io) { return 0; }
int closeConsole (Console& io) { return 0; }
int flushConsole (Console& io) { return 0; }
int nextConsole (Console& io) { return 0; }
ssize_t readConsole (Console& io, char_t* data, size_t size)
{
if (this->inptr >= this->inend) return 0; // EOF
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
this->inptr += x;
return x;
}
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
{
try { this->output.append (data, size); }
catch (...)
{
((Run*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
return size;
}
};
static void print_error (
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
{
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);
@ -38,41 +74,55 @@ static void print_error (
}
static int run_awk (QSE::StdAwk& awk)
static int run_awk (MyAwk& awk)
{
// sample input string
const qse_char_t* instr = QSE_T(
"aardvark 555-5553 1200/300 B\n"
"alpo-net 555-3412 2400/1200/300 A\n"
"barfly 555-7685 1200/300 A\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 C\n"
"fooey 555-1234 2400/1200/300 B\n"
"foot 555-6699 1200/300 B\n"
"macfoo 555-6480 1200/300 A\n"
"sdace 555-3430 2400/1200/300 A\n"
"sabafoo 555-2127 1200/300 C\n");
// ARGV[0]
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
if (awk.addArgument (QSE_T("awk22")) <= -1) return -1;
// ARGV[1] and/or the first console input file
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
// prepare a script to print the second and the first column
MyAwk::SourceString in (QSE_T("{ print $2, $1; }"));
// parse the script.
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
MyAwk::Value r;
const qse_char_t* script = QSE_T(
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
"length($0) > 0 { print $0; count++; }\n"
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
);
awk.setInput (instr); // locate the input string
awk.clearOutput (); // clear the output string
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"));
}
// parse the script string and deparse it to awk05.out.
if (awk.parse (in, out) == QSE_NULL) return -1;
QSE::StdAwk::Value r;
// execute the BEGIN, pattern-action, END blocks.
return awk.loop (&r);
return x;
}
static int awk_main (int argc, qse_char_t* argv[])
int main (int argc, char* argv[])
{
QSE::StdAwk awk;
MyAwk awk;
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1)
{
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
MyAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
@ -80,26 +130,3 @@ static int awk_main (int argc, qse_char_t* argv[])
return ret;
}
int qse_main (int argc, qse_achar_t* argv[])
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
}
#else
setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif
return qse_runmain (argc,argv,awk_main);
}

View File

@ -1,35 +1,98 @@
/*
* $Id$
*
Copyright 2006-2012 Chung, Hyung-Hwan.
This file is part of QSE.
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.
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.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/awk/StdAwk.hpp>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <string>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#if defined(QSE_CHAR_IS_WCHAR)
typedef std::wstring String;
#else
typedef std::string String;
#endif
typedef QSE::StdAwk StdAwk;
typedef QSE::StdAwk::Run Run;
typedef QSE::StdAwk::Value Value;
class MyAwk: public StdAwk
{
public:
//
// this class overrides console methods to use
// string buffers for console input and output.
//
MyAwk () { }
~MyAwk () { close (); }
void setInput (const char_t* instr)
{
this->input = instr;
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
const char_t* getOutput () { return this->output.c_str(); }
protected:
String input; // console input buffer
const char_t* inptr;
const char_t* inend;
String output; // console output buffer
int openConsole (Console& io)
{
if (io.getMode() == Console::READ)
{
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
else
{
this->output.clear ();
}
return 1; // return open-success
}
int closeConsole (Console& io)
{
return 0; // return success
}
int flushConsole (Console& io)
{
// there is nothing to flush since a string buffer
// is used for a console output. just return success.
return 0;
}
int nextConsole (Console& io)
{
// this stripped-down awk doesn't honor the nextfile statement
// or the nextofile statement. just return failure.
return -1;
}
ssize_t readConsole (Console& io, char_t* data, size_t size)
{
if (this->inptr >= this->inend) return 0; // EOF
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
this->inptr += x;
return x;
}
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
{
try { this->output.append (data, size); }
catch (...)
{
((Run*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
return size;
}
};
static void print_error (
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
{
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);
@ -38,94 +101,85 @@ static void print_error (
}
static int run_awk (QSE::StdAwk& awk)
static int run_awk (MyAwk& awk)
{
QSE::StdAwk::Run* run;
// sample input string
const qse_char_t* instr = QSE_T(
"aardvark 555-5553 1200/300 B\n"
"alpo-net 555-3412 2400/1200/300 A\n"
"barfly 555-7685 1200/300 A\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 C\n"
"fooey 555-1234 2400/1200/300 B\n"
"foot 555-6699 1200/300 B\n"
"macfoo 555-6480 1200/300 A\n"
"sdace 555-3430 2400/1200/300 A\n"
"sabafoo 555-2127 1200/300 C\n");
const qse_char_t* script = QSE_T(
"function add (a, b) { return a + b }\n"
"function mul (a, b) { return a * b }\n"
"function div (a, b) { return a / b }\n"
"function sine (a) { return sin(a) }\n"
);
const qse_char_t* instr2 = QSE_T(
"aardvark 555-5553 1200/300 A\n"
"alpo-net 555-3412 2400/1200/300 B\n"
"barfly 555-7685 1200/300 C\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 B\n"
"fooey 555-1234 2400/1200/300 A\n"
"foot 555-6699 1200/300 A\n"
"macfoo 555-6480 1200/300 B\n"
"sdace 555-3430 2400/1200/300 B\n"
"sabafoo 555-2127 1200/300 A\n");
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
// ARGV[0]
if (awk.addArgument (QSE_T("awk23")) <= -1) return -1;
// parse the script and deparse it to awk06.out
run = awk.parse (in, out);
if (run == QSE_NULL) return -1;
// prepare a string to print lines with A in the fourth column
MyAwk::SourceString in (QSE_T("$4 == \"A\" { print $2, $1, $3; }"));
// parse the script.
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
MyAwk::Value r;
QSE::StdAwk::Value arg[2];
if (arg[0].setInt (run, -20) <= -1) return -1;
if (arg[1].setStr (run, QSE_T("51")) <= -1) return -1;
awk.setInput (instr); // locate the input string
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
// ret = add (-20, 51)
QSE::StdAwk::Value ret;
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"));
// ret = mul (ret, 51);
arg[0] = ret;
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
awk.setInput (instr2);
// ret = div (ret, 2);
arg[0] = ret;
if (arg[1].setFlt (run, 2) <= -1) return -1;
if (awk.call (QSE_T("div"), &ret, arg, 2) <= -1) return -1;
// reset the runtime context so that the next loop() method
// is performed over a new console stream.
if (awk.resetRunContext() == QSE_NULL) return -1;
// 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));
int x = awk.loop (&r);
// ret = sine (ret);
arg[0] = ret;
if (awk.call (QSE_T("sine"), &ret, arg, 1) <= -1) return -1;
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}
// 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));
return 0;
return x;
}
static int awk_main (int argc, qse_char_t* argv[])
int main (int argc, char* argv[])
{
QSE::StdAwk awk;
int ret = awk.open();
MyAwk awk;
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1)
{
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
MyAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
awk.close ();
return -1;
return ret;
}
int qse_main (int argc, qse_achar_t* argv[])
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
}
#else
setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif
return qse_runmain (argc,argv,awk_main);
}

View File

@ -22,96 +22,14 @@
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/str.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <string>
#if defined(QSE_CHAR_IS_WCHAR)
typedef std::wstring String;
#else
typedef std::string String;
#endif
typedef QSE::StdAwk StdAwk;
typedef QSE::StdAwk::Run Run;
typedef QSE::StdAwk::Value Value;
class MyAwk: public StdAwk
{
public:
//
// this class overrides console methods to use
// string buffers for console input and output.
//
MyAwk () { }
~MyAwk () { close (); }
void setInput (const char_t* instr)
{
this->input = instr;
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
void clearOutput () { this->output.clear (); }
const char_t* getOutput () { return this->output.c_str(); }
protected:
String input; // console input buffer
const char_t* inptr;
const char_t* inend;
String output; // console output buffer
int openConsole (Console& io)
{
return 1; // return open-success
}
int closeConsole (Console& io)
{
return 0; // return success
}
int flushConsole (Console& io)
{
// there is nothing to flush since a string buffer
// is used for a console output. just return success.
return 0;
}
int nextConsole (Console& io)
{
// this stripped-down awk doesn't honor the nextfile statement
// or the nextofile statement. just return success.
return 0;
}
ssize_t readConsole (Console& io, char_t* data, size_t size)
{
if (this->inptr >= this->inend) return 0; // EOF
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
this->inptr += x;
return x;
}
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
{
try { this->output.append (data, size); }
catch (...)
{
((Run*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
return size;
}
};
static void print_error (
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
{
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);
@ -120,70 +38,41 @@ static void print_error (
}
static int run_awk (MyAwk& awk)
static int run_awk (QSE::StdAwk& awk)
{
// sample input string
const qse_char_t* instr = QSE_T(
"aardvark 555-5553 1200/300 B\n"
"alpo-net 555-3412 2400/1200/300 A\n"
"barfly 555-7685 1200/300 A\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 C\n"
"fooey 555-1234 2400/1200/300 B\n"
"foot 555-6699 1200/300 B\n"
"macfoo 555-6480 1200/300 A\n"
"sdace 555-3430 2400/1200/300 A\n"
"sabafoo 555-2127 1200/300 C\n");
// ARGV[0]
if (awk.addArgument (QSE_T("awk12")) <= -1) return -1;
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
// prepare a script to print the second and the first column
MyAwk::SourceString in (QSE_T("{ print $2, $1; }"));
// parse the script.
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
MyAwk::Value r;
// ARGV[1] and/or the first console input file
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
awk.setInput (instr); // locate the input string
awk.clearOutput (); // clear the output string
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
const qse_char_t* script = QSE_T(
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
"length($0) > 0 { print $0; count++; }\n"
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
);
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"));
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
// prepare a string to print lines with A in the fourth column
MyAwk::SourceString in2 (QSE_T("$4 == \"A\" { print $1; }"));
if (awk.parse (in2, MyAwk::Source::NONE) == QSE_NULL) return -1;
// parse the script string and deparse it to awk05.out.
if (awk.parse (in, out) == QSE_NULL) return -1;
awk.setInput (instr);
awk.clearOutput ();
int x = awk.loop (&r);
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}
return x;
QSE::StdAwk::Value r;
// execute the BEGIN, pattern-action, END blocks.
return awk.loop (&r);
}
static int awk_main (int argc, qse_char_t* argv[])
{
MyAwk awk;
QSE::StdAwk awk;
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1)
{
MyAwk::loc_t loc = awk.getErrorLocation();
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}

View File

@ -22,105 +22,14 @@
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/str.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <string>
#if defined(QSE_CHAR_IS_WCHAR)
typedef std::wstring String;
#else
typedef std::string String;
#endif
typedef QSE::StdAwk StdAwk;
typedef QSE::StdAwk::Run Run;
typedef QSE::StdAwk::Value Value;
class MyAwk: public StdAwk
{
public:
//
// this class overrides console methods to use
// string buffers for console input and output.
//
MyAwk () { }
~MyAwk () { close (); }
void setInput (const char_t* instr)
{
this->input = instr;
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
const char_t* getOutput () { return this->output.c_str(); }
protected:
String input; // console input buffer
const char_t* inptr;
const char_t* inend;
String output; // console output buffer
int openConsole (Console& io)
{
if (io.getMode() == Console::READ)
{
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
else
{
this->output.clear ();
}
return 1; // return open-success
}
int closeConsole (Console& io)
{
return 0; // return success
}
int flushConsole (Console& io)
{
// there is nothing to flush since a string buffer
// is used for a console output. just return success.
return 0;
}
int nextConsole (Console& io)
{
// this stripped-down awk doesn't honor the nextfile statement
// or the nextofile statement. just return failure.
return -1;
}
ssize_t readConsole (Console& io, char_t* data, size_t size)
{
if (this->inptr >= this->inend) return 0; // EOF
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
this->inptr += x;
return x;
}
ssize_t writeConsole (Console& io, const char_t* data, size_t size)
{
try { this->output.append (data, size); }
catch (...)
{
((Run*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
return size;
}
};
static void print_error (
const MyAwk::loc_t& loc, const MyAwk::char_t* msg)
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
{
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);
@ -129,86 +38,73 @@ static void print_error (
}
static int run_awk (MyAwk& awk)
static int run_awk (QSE::StdAwk& awk)
{
// sample input string
const qse_char_t* instr = QSE_T(
"aardvark 555-5553 1200/300 B\n"
"alpo-net 555-3412 2400/1200/300 A\n"
"barfly 555-7685 1200/300 A\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 C\n"
"fooey 555-1234 2400/1200/300 B\n"
"foot 555-6699 1200/300 B\n"
"macfoo 555-6480 1200/300 A\n"
"sdace 555-3430 2400/1200/300 A\n"
"sabafoo 555-2127 1200/300 C\n");
QSE::StdAwk::Run* run;
const qse_char_t* instr2 = QSE_T(
"aardvark 555-5553 1200/300 A\n"
"alpo-net 555-3412 2400/1200/300 B\n"
"barfly 555-7685 1200/300 C\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 B\n"
"fooey 555-1234 2400/1200/300 A\n"
"foot 555-6699 1200/300 A\n"
"macfoo 555-6480 1200/300 B\n"
"sdace 555-3430 2400/1200/300 B\n"
"sabafoo 555-2127 1200/300 A\n");
const qse_char_t* script = QSE_T(
"function add (a, b) { return a + b }\n"
"function mul (a, b) { return a * b }\n"
"function div (a, b) { return a / b }\n"
"function sine (a) { return sin(a) }\n"
);
// ARGV[0]
if (awk.addArgument (QSE_T("awk13")) <= -1) return -1;
QSE::StdAwk::SourceString in (script);
QSE::StdAwk::SourceFile out (QSE_T("awk06.out"));
// prepare a string to print lines with A in the fourth column
MyAwk::SourceString in (QSE_T("$4 == \"A\" { print $2, $1, $3; }"));
// parse the script.
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) return -1;
MyAwk::Value r;
// parse the script and deparse it to awk06.out
run = awk.parse (in, out);
if (run == QSE_NULL) return -1;
awk.setInput (instr); // locate the input string
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
QSE::StdAwk::Value arg[2];
if (arg[0].setInt (run, -20) <= -1) return -1;
if (arg[1].setStr (run, QSE_T("51")) <= -1) return -1;
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"));
// ret = add (-20, 51)
QSE::StdAwk::Value ret;
if (awk.call (QSE_T("add"), &ret, arg, 2) <= -1) return -1;
awk.setInput (instr2);
// ret = mul (ret, 51);
arg[0] = ret;
if (awk.call (QSE_T("mul"), &ret, arg, 2) <= -1) return -1;
// reset the runtime context so that the next loop() method
// is performed over a new console stream.
if (awk.resetRunContext() == QSE_NULL) return -1;
// ret = div (ret, 2);
arg[0] = ret;
if (arg[1].setFlt (run, 2) <= -1) return -1;
if (awk.call (QSE_T("div"), &ret, arg, 2) <= -1) return -1;
int x = awk.loop (&r);
// 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));
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}
// ret = sine (ret);
arg[0] = ret;
if (awk.call (QSE_T("sine"), &ret, arg, 1) <= -1) return -1;
return x;
// 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));
return 0;
}
static int awk_main (int argc, qse_char_t* argv[])
{
MyAwk awk;
QSE::StdAwk awk;
int ret = awk.open();
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
if (ret <= -1)
{
MyAwk::loc_t loc = awk.getErrorLocation();
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
awk.close ();
return ret;
return -1;
}
int qse_main (int argc, qse_achar_t* argv[])
@ -231,6 +127,5 @@ int qse_main (int argc, qse_achar_t* argv[])
setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif
return qse_runmain (argc,argv,awk_main);
}