Recovered from cvs revision 2007-05-19 04:43:00
This commit is contained in:
parent
314b089f0d
commit
c29572a781
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: StdAwk.cpp,v 1.14 2007/05/16 14:44:13 bacon Exp $
|
* $Id: StdAwk.cpp,v 1.15 2007/05/18 15:36:02 bacon Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ase/awk/StdAwk.hpp>
|
#include <ase/awk/StdAwk.hpp>
|
||||||
@ -26,11 +26,17 @@ namespace ASE
|
|||||||
::srand (seed);
|
::srand (seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ADD_FUNC(name,min,max,impl) \
|
||||||
|
do { \
|
||||||
|
if (addFunction (name, min, max, \
|
||||||
|
(FunctionHandler)impl) == -1) return -1; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
int StdAwk::open ()
|
int StdAwk::open ()
|
||||||
{
|
{
|
||||||
int n = Awk::open ();
|
int n = Awk::open ();
|
||||||
if (n == 0)
|
if (n == -1) return n;
|
||||||
{
|
|
||||||
int opt =
|
int opt =
|
||||||
ASE_AWK_IMPLICIT |
|
ASE_AWK_IMPLICIT |
|
||||||
ASE_AWK_EXPLICIT |
|
ASE_AWK_EXPLICIT |
|
||||||
@ -47,23 +53,22 @@ namespace ASE
|
|||||||
ase_awk_setoption (awk, opt);
|
ase_awk_setoption (awk, opt);
|
||||||
|
|
||||||
|
|
||||||
addFunction (ASE_T("sin"), 1, 1, (FunctionHandler)&StdAwk::sin);
|
ADD_FUNC (ASE_T("sin"), 1, 1, &StdAwk::sin);
|
||||||
addFunction (ASE_T("cos"), 1, 1, (FunctionHandler)&StdAwk::cos);
|
ADD_FUNC (ASE_T("cos"), 1, 1, &StdAwk::cos);
|
||||||
addFunction (ASE_T("tan"), 1, 1, (FunctionHandler)&StdAwk::tan);
|
ADD_FUNC (ASE_T("tan"), 1, 1, &StdAwk::tan);
|
||||||
addFunction (ASE_T("atan2"), 2, 2, (FunctionHandler)&StdAwk::atan2);
|
ADD_FUNC (ASE_T("atan2"), 2, 2, &StdAwk::atan2);
|
||||||
addFunction (ASE_T("log"), 1, 1, (FunctionHandler)&StdAwk::log);
|
ADD_FUNC (ASE_T("log"), 1, 1, &StdAwk::log);
|
||||||
addFunction (ASE_T("exp"), 1, 1, (FunctionHandler)&StdAwk::exp);
|
ADD_FUNC (ASE_T("exp"), 1, 1, &StdAwk::exp);
|
||||||
addFunction (ASE_T("sqrt"), 1, 1, (FunctionHandler)&StdAwk::sqrt);
|
ADD_FUNC (ASE_T("sqrt"), 1, 1, &StdAwk::sqrt);
|
||||||
addFunction (ASE_T("int"), 1, 1, (FunctionHandler)&StdAwk::fnint);
|
ADD_FUNC (ASE_T("int"), 1, 1, &StdAwk::fnint);
|
||||||
addFunction (ASE_T("rand"), 0, 0, (FunctionHandler)&StdAwk::rand);
|
ADD_FUNC (ASE_T("rand"), 0, 0, &StdAwk::rand);
|
||||||
addFunction (ASE_T("srand"), 1, 1, (FunctionHandler)&StdAwk::srand);
|
ADD_FUNC (ASE_T("srand"), 1, 1, &StdAwk::srand);
|
||||||
addFunction (ASE_T("systime"), 0, 0, (FunctionHandler)&StdAwk::systime);
|
ADD_FUNC (ASE_T("systime"), 0, 0, &StdAwk::systime);
|
||||||
addFunction (ASE_T("strftime"), 0, 2, (FunctionHandler)&StdAwk::strftime);
|
ADD_FUNC (ASE_T("strftime"), 0, 2, &StdAwk::strftime);
|
||||||
addFunction (ASE_T("strfgmtime"), 0, 2, (FunctionHandler)&StdAwk::strfgmtime);
|
ADD_FUNC (ASE_T("strfgmtime"), 0, 2, &StdAwk::strfgmtime);
|
||||||
addFunction (ASE_T("system"), 1, 1, (FunctionHandler)&StdAwk::system);
|
ADD_FUNC (ASE_T("system"), 1, 1, &StdAwk::system);
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int StdAwk::sin (Return* ret, const Argument* args, size_t nargs)
|
int StdAwk::sin (Return* ret, const Argument* args, size_t nargs)
|
||||||
@ -228,8 +233,20 @@ namespace ASE
|
|||||||
|
|
||||||
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
|
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
|
||||||
{
|
{
|
||||||
// TODO: change this implementation...
|
|
||||||
FILE* fp = (FILE*)io.getHandle();
|
FILE* fp = (FILE*)io.getHandle();
|
||||||
|
ssize_t n = 0;
|
||||||
|
|
||||||
|
while (n < len)
|
||||||
|
{
|
||||||
|
ase_cint_t c = ase_fgetc (fp);
|
||||||
|
if (c == ASE_CHAR_EOF) break;
|
||||||
|
|
||||||
|
buf[n++] = c;
|
||||||
|
if (c == ASE_T('\n')) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
/*
|
||||||
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
||||||
{
|
{
|
||||||
if (ferror(fp)) return -1;
|
if (ferror(fp)) return -1;
|
||||||
@ -237,6 +254,7 @@ namespace ASE
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ase_strlen(buf);
|
return ase_strlen(buf);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -326,8 +344,21 @@ namespace ASE
|
|||||||
|
|
||||||
StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len)
|
StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len)
|
||||||
{
|
{
|
||||||
// TODO: replace ase_fgets by something else
|
|
||||||
FILE* fp = (FILE*)io.getHandle();
|
FILE* fp = (FILE*)io.getHandle();
|
||||||
|
ssize_t n = 0;
|
||||||
|
|
||||||
|
while (n < len)
|
||||||
|
{
|
||||||
|
ase_cint_t c = ase_fgetc (fp);
|
||||||
|
if (c == ASE_CHAR_EOF) break;
|
||||||
|
|
||||||
|
buf[n++] = c;
|
||||||
|
if (c == ASE_T('\n')) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
|
||||||
|
/*
|
||||||
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
if (ase_fgets (buf, len, fp) == ASE_NULL)
|
||||||
{
|
{
|
||||||
if (ferror(fp)) return -1;
|
if (ferror(fp)) return -1;
|
||||||
@ -335,6 +366,7 @@ namespace ASE
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ase_strlen(buf);
|
return ase_strlen(buf);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len)
|
StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: extio.c,v 1.3 2007/04/30 05:47:33 bacon Exp $
|
* $Id: extio.c,v 1.4 2007/05/18 16:19:20 bacon Exp $
|
||||||
*
|
*
|
||||||
* {License}
|
* {License}
|
||||||
*/
|
*/
|
||||||
@ -247,7 +247,44 @@ int ase_awk_readextio (
|
|||||||
if (n == 0)
|
if (n == 0)
|
||||||
{
|
{
|
||||||
p->in.eof = ase_true;
|
p->in.eof = ase_true;
|
||||||
|
|
||||||
if (ASE_STR_LEN(buf) == 0) ret = 0;
|
if (ASE_STR_LEN(buf) == 0) ret = 0;
|
||||||
|
else if (rs_len >= 2)
|
||||||
|
{
|
||||||
|
/* when RS is multiple characters, it needs to check
|
||||||
|
* for the match at the end of the input stream as
|
||||||
|
* the buffer has been appened with the last character
|
||||||
|
* after the previous matchrex has failed */
|
||||||
|
|
||||||
|
const ase_char_t* match_ptr;
|
||||||
|
ase_size_t match_len;
|
||||||
|
|
||||||
|
ASE_ASSERT (run->global.rs != ASE_NULL);
|
||||||
|
|
||||||
|
n = ase_awk_matchrex (
|
||||||
|
run->awk, run->global.rs,
|
||||||
|
((run->global.ignorecase)? ASE_AWK_REX_IGNORECASE: 0),
|
||||||
|
ASE_STR_BUF(buf), ASE_STR_LEN(buf),
|
||||||
|
&match_ptr, &match_len, &run->errnum);
|
||||||
|
if (n == -1)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 1)
|
||||||
|
{
|
||||||
|
/* the match should be found at the end of
|
||||||
|
* the current buffer */
|
||||||
|
ASE_ASSERT (
|
||||||
|
ASE_STR_BUF(buf) + ASE_STR_LEN(buf) ==
|
||||||
|
match_ptr + match_len);
|
||||||
|
|
||||||
|
ASE_STR_LEN(buf) -= match_len;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +357,7 @@ int ase_awk_readextio (
|
|||||||
if (n == -1)
|
if (n == -1)
|
||||||
{
|
{
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
p->in.pos--; /* unread the character in c */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,6 +370,7 @@ int ase_awk_readextio (
|
|||||||
match_ptr + match_len);
|
match_ptr + match_len);
|
||||||
|
|
||||||
ASE_STR_LEN(buf) -= match_len;
|
ASE_STR_LEN(buf) -= match_len;
|
||||||
|
p->in.pos--; /* unread the character in c */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
* fixed bug (nextofile not allowed in the BEGIN and END block)
|
* fixed bug (nextofile not allowed in the BEGIN and END block)
|
||||||
* fixed bug (ASE_MALLOC/ASE_FREE defined wrongly causing test programs
|
* fixed bug (ASE_MALLOC/ASE_FREE defined wrongly causing test programs
|
||||||
to crash in the debug mode when compiled with MSVC/C++.
|
to crash in the debug mode when compiled with MSVC/C++.
|
||||||
|
* fixed bug (the first character of the next record lost when RS are
|
||||||
|
multiple characters)
|
||||||
|
* fixed bug (the last field of the last record lost when RS are
|
||||||
|
multiple characters)
|
||||||
|
|
||||||
* added awk c++ classes (awk/Awk.hpp awk/Awk.cpp awk/StdAwk.hpp awk/StdAwk.cpp)
|
* added awk c++ classes (awk/Awk.hpp awk/Awk.cpp awk/StdAwk.hpp awk/StdAwk.cpp)
|
||||||
* added c++ test program (test/awk/Awk.cpp)
|
* added c++ test program (test/awk/Awk.cpp)
|
||||||
|
9
ase/test/awk/adr-001.awk
Normal file
9
ase/test/awk/adr-001.awk
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/awk
|
||||||
|
|
||||||
|
BEGIN {
|
||||||
|
RS = "\n\n";
|
||||||
|
FS = "\n";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
print $1, $NF;
|
||||||
|
}
|
12
ase/test/awk/adr-001.out
Normal file
12
ase/test/awk/adr-001.out
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
BEGIN {
|
||||||
|
RS = "\n\n";
|
||||||
|
FS = "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
print $1,$NF;
|
||||||
|
}
|
||||||
|
|
||||||
|
James Brown 012-345-678
|
||||||
|
Richie Ren 02-3473-9192
|
||||||
|
Toh WeeKung 9102-1203
|
15
ase/test/awk/adr-en.data
Normal file
15
ase/test/awk/adr-en.data
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
James Brown
|
||||||
|
IBM
|
||||||
|
Somewhere over the rainbow
|
||||||
|
012-345-678
|
||||||
|
|
||||||
|
Richie Ren
|
||||||
|
Ezsystem
|
||||||
|
Taipei Taiwan
|
||||||
|
02-3473-9192
|
||||||
|
|
||||||
|
Toh WeeKung
|
||||||
|
Topaz
|
||||||
|
Singapore
|
||||||
|
9102-1203
|
||||||
|
|
@ -21,6 +21,11 @@ run_init()
|
|||||||
run_script_for_init "$script" "cou-en.data"
|
run_script_for_init "$script" "cou-en.data"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
for script in adr-???.awk
|
||||||
|
do
|
||||||
|
run_script_for_init "$script" "adr-en.data"
|
||||||
|
done
|
||||||
|
|
||||||
for script in err-???.awk
|
for script in err-???.awk
|
||||||
do
|
do
|
||||||
run_script_for_init "$script" "err-en.data"
|
run_script_for_init "$script" "err-en.data"
|
||||||
@ -88,6 +93,24 @@ run_test()
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
for script in adr-???.awk
|
||||||
|
do
|
||||||
|
run_script_for_test "$script" "adr-en.data"
|
||||||
|
if [ $? -ne 0 ]
|
||||||
|
then
|
||||||
|
echo "###################################"
|
||||||
|
echo "PROBLEM(S) DETECTED IN $script.".
|
||||||
|
echo "###################################"
|
||||||
|
|
||||||
|
echo "Do you want to abort? [y/n]"
|
||||||
|
read ans
|
||||||
|
if [ "$ans" = "y" -o "$ans" = "Y" ]
|
||||||
|
then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
for script in err-???.awk
|
for script in err-???.awk
|
||||||
do
|
do
|
||||||
run_script_for_test "$script" "err-en.data"
|
run_script_for_test "$script" "err-en.data"
|
||||||
|
Loading…
Reference in New Issue
Block a user