qse/qse/samples/awk/awk22.cpp

106 lines
2.6 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
*
Copyright 2006-2012 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>
2012-01-03 14:41:15 +00:00
#include <qse/cmn/mbwc.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
2009-07-10 06:46:14 +00:00
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);
2009-07-10 06:46:14 +00:00
else
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), msg);
}
static int run_awk (QSE::StdAwk& awk)
2009-07-10 06:46:14 +00:00
{
2013-01-09 08:03:21 +00:00
// ARGV[0]
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
// ARGV[1] and/or the first console input file
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
2009-07-10 06:46:14 +00:00
const qse_char_t* script = QSE_T(
2013-01-09 08:03:21 +00:00
"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"
2009-07-10 06:46:14 +00:00
);
QSE::StdAwk::SourceString in (script);
2013-01-09 08:03:21 +00:00
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
2013-01-09 08:03:21 +00:00
// parse the script string and deparse it to awk05.out.
if (awk.parse (in, out) == QSE_NULL) return -1;
2013-01-09 08:03:21 +00:00
QSE::StdAwk::Value r;
// execute the BEGIN, pattern-action, END blocks.
return awk.loop (&r);
2009-07-10 06:46:14 +00:00
}
static int awk_main (int argc, qse_char_t* argv[])
{
QSE::StdAwk awk;
2013-01-09 08:03:21 +00:00
int ret = awk.open ();
if (ret >= 0) ret = run_awk (awk);
2013-01-09 08:03:21 +00:00
if (ret <= -1)
{
2009-08-26 07:07:54 +00:00
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
awk.close ();
2013-01-09 08:03:21 +00:00
return ret;
}
2009-07-10 06:46:14 +00:00
int qse_main (int argc, qse_achar_t* argv[])
{
2012-01-03 14:41:15 +00:00
#if defined(_WIN32)
char locale[100];
2012-01-03 14:41:15 +00:00
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
2012-10-15 09:36:39 +00:00
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
2012-01-03 14:41:15 +00:00
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
2012-01-03 14:41:15 +00:00
}
#else
setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB);
2012-01-03 14:41:15 +00:00
#endif
2013-01-09 08:03:21 +00:00
2009-07-10 06:46:14 +00:00
return qse_runmain (argc,argv,awk_main);
}