touched up sample codes

This commit is contained in:
hyung-hwan 2009-08-14 07:26:46 +00:00
parent ce695db836
commit 1caca1fa41
5 changed files with 151 additions and 112 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.hpp 251 2009-08-10 07:11:16Z hyunghwan.chung $ * $Id: Awk.hpp 254 2009-08-13 13:26:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -25,6 +25,11 @@
#include <qse/Mmgr.hpp> #include <qse/Mmgr.hpp>
#include <stdarg.h> #include <stdarg.h>
/** @file
* AWK Interpreter
*/
///////////////////////////////// /////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
@ -475,6 +480,9 @@ public:
void operator delete (void* p); void operator delete (void* p);
void operator delete[] (void* p); void operator delete[] (void* p);
/**
* Represents an index of an arrayed value
*/
class Index class Index
{ {
public: public:
@ -488,6 +496,9 @@ public:
size_t len; size_t len;
}; };
/**
* Represents a numeric index of an arrayed value
*/
class IntIndex: public Index class IntIndex: public Index
{ {
public: public:
@ -512,6 +523,9 @@ public:
#endif #endif
}; };
/**
* helper class to iterate over array elements
*/
class IndexIterator class IndexIterator
{ {
public: public:
@ -656,6 +670,7 @@ public:
const char_t* str const char_t* str
); );
/** determines if a value is arrayed */
bool isIndexed () const; bool isIndexed () const;
int getIndexed ( int getIndexed (

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdAwk.hpp 250 2009-08-10 03:29:59Z hyunghwan.chung $ * $Id: StdAwk.hpp 254 2009-08-13 13:26:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -21,13 +21,16 @@
#include <qse/awk/Awk.hpp> #include <qse/awk/Awk.hpp>
/** /** @file
*
* @example awk05.cpp * @example awk05.cpp
* This program demonstrates how to embed QSE::StdAwk::loop(). * This program demonstrates how to use QSE::StdAwk::loop().
* @example awk06.cpp * @example awk06.cpp
* This program demonstrates how to use QSE::StdAwk::call(). * This program demonstrates how to use QSE::StdAwk::call().
* @example awk07.cpp * @example awk07.cpp
* This program demonstrates how to handle an indexed value. * This program demonstrates how to handle an indexed value.
* @example awk08.cpp
* This program shows how to add intrinsic functions.
*/ */
///////////////////////////////// /////////////////////////////////
@ -35,13 +38,16 @@ QSE_BEGIN_NAMESPACE(QSE)
//////////////////////////////// ////////////////////////////////
/** /**
* Provides a more useful AWK interpreter by overriding primitive methods, * Provides a more useful interpreter by overriding primitive methods,
* the file handler, the pipe handler and implementing common AWK intrinsic * the file handler, the pipe handler and implementing common intrinsic
* functions. * functions.
*/ */
class StdAwk: public Awk class StdAwk: public Awk
{ {
public: public:
/**
* Implements script input from a file and deparsing into a file.
*/
class SourceFile: public Source class SourceFile: public Source
{ {
public: public:
@ -60,6 +66,10 @@ public:
qse_cstr_t dir; qse_cstr_t dir;
}; };
/**
* Implements script input from a string. The deparsing is not
* supported.
*/
class SourceString: public Source class SourceString: public Source
{ {
public: public:

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Sed.hpp 235 2009-07-15 10:43:31Z hyunghwan.chung $ * $Id: Sed.hpp 254 2009-08-13 13:26:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -22,7 +22,7 @@
#include <qse/Mmgr.hpp> #include <qse/Mmgr.hpp>
#include <qse/sed/sed.h> #include <qse/sed/sed.h>
/**@file /** @file
* A stream editor * A stream editor
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* $Id: StdSed.hpp 235 2009-07-15 10:43:31Z hyunghwan.chung $ * $Id: StdSed.hpp 254 2009-08-13 13:26:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -21,6 +21,10 @@
#include <qse/sed/Sed.hpp> #include <qse/sed/Sed.hpp>
/** @file
* standard sed
*/
///////////////////////////////// /////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////

View File

@ -17,15 +17,10 @@
*/ */
#include <qse/awk/StdAwk.hpp> #include <qse/awk/StdAwk.hpp>
#include <qse/cmn/str.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/opt.h> #include <qse/cmn/opt.h>
#include <qse/cmn/mem.h> #include <qse/cmn/main.h>
#include <qse/cmn/stdio.h>
#include <stdlib.h> #include <cstring>
#include <string.h>
#include <math.h>
#if defined(_WIN32) #if defined(_WIN32)
# include <windows.h> # include <windows.h>
@ -35,30 +30,12 @@
# include <errno.h> # include <errno.h>
#endif #endif
class MyAwk; /* these three definitions for doxygen cross-reference */
#ifdef _WIN32 typedef QSE::StdAwk StdAwk;
static BOOL WINAPI stop_run (DWORD ctrl_type); typedef QSE::StdAwk::Run Run;
#else typedef QSE::StdAwk::Value Value;
static void stop_run (int sig);
#endif
static void set_intr_run (void); class MyAwk: public StdAwk
static void unset_intr_run (void);
MyAwk* app_awk = QSE_NULL;
static void print_error (const qse_char_t* fmt, ...)
{
va_list va;
qse_fprintf (QSE_STDERR, QSE_T("ERROR: "));
va_start (va, fmt);
qse_vfprintf (QSE_STDERR, fmt, va);
va_end (va);
}
class MyAwk: public QSE::StdAwk
{ {
public: public:
MyAwk () { } MyAwk () { }
@ -69,24 +46,26 @@ public:
if (StdAwk::open () <= -1) return -1; if (StdAwk::open () <= -1) return -1;
idLastSleep = addGlobal (QSE_T("LAST_SLEEP")); idLastSleep = addGlobal (QSE_T("LAST_SLEEP"));
if (idLastSleep <= -1) goto failure; if (idLastSleep <= -1) goto oops;
if (addFunction (QSE_T("sleep"), 1, 1, if (addFunction (QSE_T("sleep"), 1, 1,
(FunctionHandler)&MyAwk::sleep) <= -1) goto failure; (FunctionHandler)&MyAwk::sleep) <= -1) goto oops;
if (addFunction (QSE_T("sumintarray"), 1, 1, if (addFunction (QSE_T("sumintarray"), 1, 1,
(FunctionHandler)&MyAwk::sumintarray) <= -1) goto failure; (FunctionHandler)&MyAwk::sumintarray) <= -1) goto oops;
if (addFunction (QSE_T("arrayindices"), 1, 1, if (addFunction (QSE_T("arrayindices"), 1, 1,
(FunctionHandler)&MyAwk::arrayindices) <= -1) goto failure; (FunctionHandler)&MyAwk::arrayindices) <= -1) goto oops;
return 0; return 0;
failure: oops:
StdAwk::close (); StdAwk::close ();
return -1; return -1;
} }
int sleep (Run& run, Value& ret, const Value* args, size_t nargs, int sleep (
Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
if (args[0].isIndexed()) if (args[0].isIndexed())
@ -113,9 +92,14 @@ public:
#endif #endif
} }
int sumintarray (Run& run, Value& ret, const Value* args, size_t nargs, int sumintarray (
Run& run, Value& ret, const Value* args, size_t nargs,
const char_t* name, size_t len) const char_t* name, size_t len)
{ {
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// print sumintarray(x);
// }
long_t x = 0; long_t x = 0;
if (args[0].isIndexed()) if (args[0].isIndexed())
@ -138,9 +122,20 @@ public:
return ret.setInt (x); return ret.setInt (x);
} }
int arrayindices (Run& run, Value& ret, const Value* args, size_t nargs, int arrayindices (
const char_t* name, size_t len) Run& run,
Value& ret,
const Value* args,
size_t nargs,
const char_t* name,
size_t len)
{ {
// create another array composed of array indices
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// y=arrayindices(x);
// for (i in y) print y[i];
// }
if (!args[0].isIndexed()) return 0; if (!args[0].isIndexed()) return 0;
Value::Index idx; Value::Index idx;
@ -159,32 +154,32 @@ public:
return 0; return 0;
} }
protected:
#if 0
bool onLoopEnter (Run& run)
{
set_intr_run ();
return true;
}
void onLoopExit (Run& run, const Value& ret)
{
unset_intr_run ();
if (verbose)
{
size_t len;
const char_t* ptr = ret.toStr (&len);
qse_printf (QSE_T("*** return [%.*s] ***\n"), (int)len, ptr);
}
}
#endif
private: private:
int idLastSleep; int idLastSleep;
}; };
static MyAwk* app_awk = QSE_NULL;
static void print_error (const qse_char_t* fmt, ...)
{
va_list va;
qse_fprintf (QSE_STDERR, QSE_T("ERROR: "));
va_start (va, fmt);
qse_vfprintf (QSE_STDERR, fmt, va);
va_end (va);
}
static void print_error (MyAwk& awk)
{
print_error (
QSE_T("LINE [%u] %s\n"),
(unsigned)awk.getErrorLine(),
awk.getErrorMessage()
);
}
#ifdef _WIN32 #ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type) static BOOL WINAPI stop_run (DWORD ctrl_type)
{ {
@ -231,7 +226,7 @@ static void stop_run (int sig)
} }
#endif #endif
static void set_intr_run (void) static void set_signal (void)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE); SetConsoleCtrlHandler (stop_run, TRUE);
@ -241,7 +236,7 @@ static void set_intr_run (void)
#endif #endif
} }
static void unset_intr_run (void) static void unset_signal (void)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetConsoleCtrlHandler (stop_run, FALSE); SetConsoleCtrlHandler (stop_run, FALSE);
@ -269,7 +264,8 @@ struct cmdline_t
qse_char_t* fs; qse_char_t* fs;
}; };
static int handle_cmdline (MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t* cmdline) static int handle_cmdline (
MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t* cmdline)
{ {
static qse_opt_t opt = static qse_opt_t opt =
{ {
@ -278,7 +274,7 @@ static int handle_cmdline (MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t*
}; };
qse_cint_t c; qse_cint_t c;
memset (cmdline, 0, QSE_SIZEOF(*cmdline)); std::memset (cmdline, 0, QSE_SIZEOF(*cmdline));
while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF) while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
{ {
switch (c) switch (c)
@ -318,41 +314,39 @@ static int handle_cmdline (MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t*
while (opt.ind < argc) while (opt.ind < argc)
{ {
if (awk.addArgument (argv[opt.ind++]) <= -1) return -1; if (awk.addArgument (argv[opt.ind++]) <= -1)
{
print_error (awk);
return -1;
}
}
if (!cmdline->ins && !cmdline->inf)
{
print_usage (QSE_STDERR, argv[0]);
return -1;
} }
return 1; return 1;
} }
static int awk_main (int argc, qse_char_t* argv[])
static int awk_main_2 (MyAwk& awk, int argc, qse_char_t* argv[])
{ {
MyAwk awk;
MyAwk::Run* run; MyAwk::Run* run;
cmdline_t cmdline; cmdline_t cmdline;
int n; int n;
if (awk.open() <= -1) awk.setOption (awk.getOption() | awk.OPT_INCLUDE | awk.OPT_MAPTOVAR);
{
print_error (QSE_T("%s\n"), awk.getErrorMessage());
return -1;
}
awk.setOption (awk.getOption() | awk.OPT_INCLUDE);
// ARGV[0] // ARGV[0]
if (awk.addArgument (QSE_T("awk08")) <= -1) if (awk.addArgument (QSE_T("awk08")) <= -1)
{ {
print_error (QSE_T("%s\n"), awk.getErrorMessage()); print_error (awk);
awk.close (); return -1;
return -1;
}
if ((n = handle_cmdline (awk, argc, argv, &cmdline)) <= 0)
{
awk.close ();
return n;
} }
if ((n = handle_cmdline (awk, argc, argv, &cmdline)) <= 0) return n;
MyAwk::Source* in, * out; MyAwk::Source* in, * out;
MyAwk::SourceString in_str (cmdline.ins); MyAwk::SourceString in_str (cmdline.ins);
@ -362,39 +356,55 @@ static int awk_main (int argc, qse_char_t* argv[])
in = (cmdline.ins)? (MyAwk::Source*)&in_str: (MyAwk::Source*)&in_file; in = (cmdline.ins)? (MyAwk::Source*)&in_str: (MyAwk::Source*)&in_file;
out = (cmdline.outf)? (MyAwk::Source*)&out_file: &MyAwk::Source::NONE; out = (cmdline.outf)? (MyAwk::Source*)&out_file: &MyAwk::Source::NONE;
run = awk.parse (*in, *out); run = awk.parse (*in, *out);
if (run == QSE_NULL) if (run == QSE_NULL)
{ {
print_error ( print_error (awk);
QSE_T("ERROR: LINE[%d] %s\n"), return -1;
awk.getErrorLine(), awk.getErrorMessage());
awk.close ();
return -1;
} }
if (cmdline.fs) if (cmdline.fs)
{ {
// TODO: print error.... handle error properly
MyAwk::Value fs (run); MyAwk::Value fs (run);
if (fs.setStr (cmdline.fs) <= -1) return -1; if (fs.setStr (cmdline.fs) <= -1)
if (awk.setGlobal (awk.GBL_FS, fs) <= -1) return -1; {
print_error (awk);
return -1;
}
if (awk.setGlobal (awk.GBL_FS, fs) <= -1)
{
print_error (awk);
return -1;
}
} }
app_awk = &awk;
MyAwk::Value ret; MyAwk::Value ret;
if (awk.loop (&ret) <= -1) if (awk.loop (&ret) <= -1)
{
print_error (awk);
return -1;
}
return 0;
}
static int awk_main (int argc, qse_char_t* argv[])
{
MyAwk awk;
if (awk.open() <= -1)
{ {
print_error ( print_error (awk);
QSE_T("ERROR: LINE[%d] %s\n"),
awk.getErrorLine(), awk.getErrorMessage());
awk.close ();
return -1; return -1;
} }
app_awk = &awk;
set_signal ();
int n = awk_main_2 (awk, argc, argv);
unset_signal ();
app_awk = QSE_NULL; app_awk = QSE_NULL;
awk.close (); awk.close ();
return n;
return 0;
} }
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])