touched up sample codes
This commit is contained in:
parent
ce695db836
commit
1caca1fa41
@ -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.
|
||||
|
||||
@ -25,6 +25,11 @@
|
||||
#include <qse/Mmgr.hpp>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
/** @file
|
||||
* AWK Interpreter
|
||||
*/
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
@ -475,6 +480,9 @@ public:
|
||||
void operator delete (void* p);
|
||||
void operator delete[] (void* p);
|
||||
|
||||
/**
|
||||
* Represents an index of an arrayed value
|
||||
*/
|
||||
class Index
|
||||
{
|
||||
public:
|
||||
@ -488,6 +496,9 @@ public:
|
||||
size_t len;
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents a numeric index of an arrayed value
|
||||
*/
|
||||
class IntIndex: public Index
|
||||
{
|
||||
public:
|
||||
@ -512,6 +523,9 @@ public:
|
||||
#endif
|
||||
};
|
||||
|
||||
/**
|
||||
* helper class to iterate over array elements
|
||||
*/
|
||||
class IndexIterator
|
||||
{
|
||||
public:
|
||||
@ -656,6 +670,7 @@ public:
|
||||
const char_t* str
|
||||
);
|
||||
|
||||
/** determines if a value is arrayed */
|
||||
bool isIndexed () const;
|
||||
|
||||
int getIndexed (
|
||||
|
@ -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.
|
||||
|
||||
@ -21,13 +21,16 @@
|
||||
|
||||
#include <qse/awk/Awk.hpp>
|
||||
|
||||
/**
|
||||
/** @file
|
||||
*
|
||||
* @example awk05.cpp
|
||||
* This program demonstrates how to embed QSE::StdAwk::loop().
|
||||
* This program demonstrates how to use QSE::StdAwk::loop().
|
||||
* @example awk06.cpp
|
||||
* This program demonstrates how to use QSE::StdAwk::call().
|
||||
* @example awk07.cpp
|
||||
* 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,
|
||||
* the file handler, the pipe handler and implementing common AWK intrinsic
|
||||
* Provides a more useful interpreter by overriding primitive methods,
|
||||
* the file handler, the pipe handler and implementing common intrinsic
|
||||
* functions.
|
||||
*/
|
||||
class StdAwk: public Awk
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Implements script input from a file and deparsing into a file.
|
||||
*/
|
||||
class SourceFile: public Source
|
||||
{
|
||||
public:
|
||||
@ -60,6 +66,10 @@ public:
|
||||
qse_cstr_t dir;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implements script input from a string. The deparsing is not
|
||||
* supported.
|
||||
*/
|
||||
class SourceString: public Source
|
||||
{
|
||||
public:
|
||||
|
@ -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.
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
#include <qse/Mmgr.hpp>
|
||||
#include <qse/sed/sed.h>
|
||||
|
||||
/**@file
|
||||
/** @file
|
||||
* A stream editor
|
||||
*/
|
||||
|
||||
|
@ -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.
|
||||
|
||||
@ -21,6 +21,10 @@
|
||||
|
||||
#include <qse/sed/Sed.hpp>
|
||||
|
||||
/** @file
|
||||
* standard sed
|
||||
*/
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
@ -17,15 +17,10 @@
|
||||
*/
|
||||
|
||||
#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/mem.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(_WIN32)
|
||||
# include <windows.h>
|
||||
@ -35,30 +30,12 @@
|
||||
# include <errno.h>
|
||||
#endif
|
||||
|
||||
class MyAwk;
|
||||
#ifdef _WIN32
|
||||
static BOOL WINAPI stop_run (DWORD ctrl_type);
|
||||
#else
|
||||
static void stop_run (int sig);
|
||||
#endif
|
||||
/* these three definitions for doxygen cross-reference */
|
||||
typedef QSE::StdAwk StdAwk;
|
||||
typedef QSE::StdAwk::Run Run;
|
||||
typedef QSE::StdAwk::Value Value;
|
||||
|
||||
static void set_intr_run (void);
|
||||
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
|
||||
class MyAwk: public StdAwk
|
||||
{
|
||||
public:
|
||||
MyAwk () { }
|
||||
@ -69,24 +46,26 @@ public:
|
||||
if (StdAwk::open () <= -1) return -1;
|
||||
|
||||
idLastSleep = addGlobal (QSE_T("LAST_SLEEP"));
|
||||
if (idLastSleep <= -1) goto failure;
|
||||
if (idLastSleep <= -1) goto oops;
|
||||
|
||||
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,
|
||||
(FunctionHandler)&MyAwk::sumintarray) <= -1) goto failure;
|
||||
(FunctionHandler)&MyAwk::sumintarray) <= -1) goto oops;
|
||||
|
||||
if (addFunction (QSE_T("arrayindices"), 1, 1,
|
||||
(FunctionHandler)&MyAwk::arrayindices) <= -1) goto failure;
|
||||
(FunctionHandler)&MyAwk::arrayindices) <= -1) goto oops;
|
||||
|
||||
return 0;
|
||||
|
||||
failure:
|
||||
oops:
|
||||
StdAwk::close ();
|
||||
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)
|
||||
{
|
||||
if (args[0].isIndexed())
|
||||
@ -113,9 +92,14 @@ public:
|
||||
#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)
|
||||
{
|
||||
// BEGIN {
|
||||
// for(i=0;i<=10;i++) x[i]=i;
|
||||
// print sumintarray(x);
|
||||
// }
|
||||
long_t x = 0;
|
||||
|
||||
if (args[0].isIndexed())
|
||||
@ -138,9 +122,20 @@ public:
|
||||
return ret.setInt (x);
|
||||
}
|
||||
|
||||
int arrayindices (Run& run, Value& ret, const Value* args, size_t nargs,
|
||||
const char_t* name, size_t len)
|
||||
int arrayindices (
|
||||
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;
|
||||
|
||||
Value::Index idx;
|
||||
@ -159,32 +154,32 @@ public:
|
||||
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:
|
||||
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
|
||||
static BOOL WINAPI stop_run (DWORD ctrl_type)
|
||||
{
|
||||
@ -231,7 +226,7 @@ static void stop_run (int sig)
|
||||
}
|
||||
#endif
|
||||
|
||||
static void set_intr_run (void)
|
||||
static void set_signal (void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetConsoleCtrlHandler (stop_run, TRUE);
|
||||
@ -241,7 +236,7 @@ static void set_intr_run (void)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void unset_intr_run (void)
|
||||
static void unset_signal (void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetConsoleCtrlHandler (stop_run, FALSE);
|
||||
@ -269,7 +264,8 @@ struct cmdline_t
|
||||
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 =
|
||||
{
|
||||
@ -278,7 +274,7 @@ static int handle_cmdline (MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t*
|
||||
};
|
||||
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)
|
||||
{
|
||||
switch (c)
|
||||
@ -318,41 +314,39 @@ static int handle_cmdline (MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t*
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
cmdline_t cmdline;
|
||||
int n;
|
||||
|
||||
if (awk.open() <= -1)
|
||||
{
|
||||
print_error (QSE_T("%s\n"), awk.getErrorMessage());
|
||||
return -1;
|
||||
}
|
||||
|
||||
awk.setOption (awk.getOption() | awk.OPT_INCLUDE);
|
||||
awk.setOption (awk.getOption() | awk.OPT_INCLUDE | awk.OPT_MAPTOVAR);
|
||||
|
||||
// ARGV[0]
|
||||
if (awk.addArgument (QSE_T("awk08")) <= -1)
|
||||
{
|
||||
print_error (QSE_T("%s\n"), awk.getErrorMessage());
|
||||
awk.close ();
|
||||
print_error (awk);
|
||||
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::SourceString in_str (cmdline.ins);
|
||||
@ -364,37 +358,53 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
run = awk.parse (*in, *out);
|
||||
if (run == QSE_NULL)
|
||||
{
|
||||
print_error (
|
||||
QSE_T("ERROR: LINE[%d] %s\n"),
|
||||
awk.getErrorLine(), awk.getErrorMessage());
|
||||
awk.close ();
|
||||
print_error (awk);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cmdline.fs)
|
||||
{
|
||||
// TODO: print error.... handle error properly
|
||||
MyAwk::Value fs (run);
|
||||
if (fs.setStr (cmdline.fs) <= -1) return -1;
|
||||
if (awk.setGlobal (awk.GBL_FS, fs) <= -1) return -1;
|
||||
if (fs.setStr (cmdline.fs) <= -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;
|
||||
if (awk.loop (&ret) <= -1)
|
||||
{
|
||||
print_error (
|
||||
QSE_T("ERROR: LINE[%d] %s\n"),
|
||||
awk.getErrorLine(), awk.getErrorMessage());
|
||||
awk.close ();
|
||||
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 (awk);
|
||||
return -1;
|
||||
}
|
||||
app_awk = &awk;
|
||||
|
||||
set_signal ();
|
||||
int n = awk_main_2 (awk, argc, argv);
|
||||
unset_signal ();
|
||||
|
||||
app_awk = QSE_NULL;
|
||||
awk.close ();
|
||||
|
||||
return 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
|
Loading…
Reference in New Issue
Block a user