Recovered from cvs revision 2007-05-17 03:03:00

This commit is contained in:
2007-05-18 01:21:00 +00:00
parent b564cc2d8a
commit a4d7743175
10 changed files with 425 additions and 250 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp,v 1.29 2007/05/16 06:43:32 bacon Exp $
* $Id: Awk.cpp,v 1.30 2007/05/16 07:13:32 bacon Exp $
*/
#include <ase/awk/Awk.hpp>
@ -7,9 +7,6 @@
#include <ase/cmn/str.h>
#include <ase/cmn/mem.h>
#include <stdio.h>
#include <tchar.h>
namespace ASE
{
@ -158,7 +155,7 @@ namespace ASE
}
}
void* Awk::Argument::operator new (size_t n, awk_t* awk)
void* Awk::Argument::operator new (size_t n, awk_t* awk) throw ()
{
void* ptr = ase_awk_malloc (awk, ASE_SIZEOF(awk) + n);
if (ptr == ASE_NULL) return ASE_NULL;
@ -167,7 +164,7 @@ namespace ASE
return (char*)ptr+ASE_SIZEOF(awk);
}
void* Awk::Argument::operator new[] (size_t n, awk_t* awk)
void* Awk::Argument::operator new[] (size_t n, awk_t* awk) throw ()
{
void* ptr = ase_awk_malloc (awk, ASE_SIZEOF(awk) + n);
if (ptr == ASE_NULL) return ASE_NULL;

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.hpp,v 1.27 2007/05/16 06:43:32 bacon Exp $
* $Id: Awk.hpp,v 1.28 2007/05/16 07:13:32 bacon Exp $
*/
#ifndef _ASE_AWK_AWK_HPP_
@ -136,8 +136,8 @@ namespace ASE
~Argument ();
// initialization
void* operator new (size_t n, awk_t* awk);
void* operator new[] (size_t n, awk_t* awk);
void* operator new (size_t n, awk_t* awk) throw ();
void* operator new[] (size_t n, awk_t* awk) throw ();
// deletion when initialization fails
void operator delete (void* p, awk_t* awk);

View File

@ -1,8 +1,11 @@
/*
* $Id: StdAwk.cpp,v 1.12 2007/05/13 10:49:32 bacon Exp $
* $Id: StdAwk.cpp,v 1.14 2007/05/16 14:44:13 bacon Exp $
*/
#include <ase/awk/StdAwk.hpp>
#include <ase/cmn/str.h>
#include <ase/utl/stdio.h>
#include <ase/utl/ctype.h>
#include <stdlib.h>
#include <math.h>
@ -195,4 +198,270 @@ namespace ASE
return n;
#endif
}
int StdAwk::openPipe (Pipe& io)
{
Awk::Pipe::Mode mode = io.getMode();
FILE* fp = NULL;
switch (mode)
{
case Awk::Pipe::READ:
fp = ase_popen (io.getName(), ASE_T("r"));
break;
case Awk::Pipe::WRITE:
fp = ase_popen (io.getName(), ASE_T("w"));
break;
}
if (fp == NULL) return -1;
io.setHandle (fp);
return 1;
}
int StdAwk::closePipe (Pipe& io)
{
fclose ((FILE*)io.getHandle());
return 0;
}
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
{
// TODO: change this implementation...
FILE* fp = (FILE*)io.getHandle();
if (ase_fgets (buf, len, fp) == ASE_NULL)
{
if (ferror(fp)) return -1;
return 0;
}
return ase_strlen(buf);
}
StdAwk::ssize_t StdAwk::writePipe (Pipe& io, char_t* buf, size_t len)
{
FILE* fp = (FILE*)io.getHandle();
size_t left = len;
while (left > 0)
{
if (*buf == ASE_T('\0'))
{
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
if (fputc ('\0', fp) == EOF)
#else
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF)
#endif
{
return -1;
}
left -= 1; buf += 1;
}
else
{
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
// fwprintf seems to return an error with the file
// pointer opened by popen, as of this writing.
// anyway, hopefully the following replacement
// will work all the way.
int n = fprintf (fp, "%.*ls", left, buf);
if (n >= 0)
{
size_t x;
for (x = 0; x < left; x++)
{
if (buf[x] == ASE_T('\0')) break;
}
n = x;
}
#else
int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf);
#endif
if (n < 0 || n > left) return -1;
left -= n; buf += n;
}
}
return len;
}
int StdAwk::flushPipe (Pipe& io)
{
return ::fflush ((FILE*)io.getHandle());
}
// file io handlers
int StdAwk::openFile (File& io)
{
Awk::File::Mode mode = io.getMode();
FILE* fp = NULL;
switch (mode)
{
case Awk::File::READ:
fp = ase_fopen (io.getName(), ASE_T("r"));
break;
case Awk::File::WRITE:
fp = ase_fopen (io.getName(), ASE_T("w"));
break;
case Awk::File::APPEND:
fp = ase_fopen (io.getName(), ASE_T("a"));
break;
}
if (fp == NULL) return -1;
io.setHandle (fp);
return 1;
}
int StdAwk::closeFile (File& io)
{
fclose ((FILE*)io.getHandle());
return 0;
}
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();
if (ase_fgets (buf, len, fp) == ASE_NULL)
{
if (ferror(fp)) return -1;
return 0;
}
return ase_strlen(buf);
}
StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len)
{
FILE* fp = (FILE*)io.getHandle();
size_t left = len;
while (left > 0)
{
if (*buf == ASE_T('\0'))
{
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) return -1;
left -= 1; buf += 1;
}
else
{
int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf);
if (n < 0 || n > left) return -1;
left -= n; buf += n;
}
}
return len;
}
int StdAwk::flushFile (File& io)
{
return ::fflush ((FILE*)io.getHandle());
}
// memory allocation primitives
void* StdAwk::allocMem (size_t n)
{
return ::malloc (n);
}
void* StdAwk::reallocMem (void* ptr, size_t n)
{
return ::realloc (ptr, n);
}
void StdAwk::freeMem (void* ptr)
{
::free (ptr);
}
// character class primitives
StdAwk::bool_t StdAwk::isUpper (cint_t c)
{
return ase_isupper (c);
}
StdAwk::bool_t StdAwk::isLower (cint_t c)
{
return ase_islower (c);
}
StdAwk::bool_t StdAwk::isAlpha (cint_t c)
{
return ase_isalpha (c);
}
StdAwk::bool_t StdAwk::isDigit (cint_t c)
{
return ase_isdigit (c);
}
StdAwk::bool_t StdAwk::isXdigit (cint_t c)
{
return ase_isxdigit (c);
}
StdAwk::bool_t StdAwk::isAlnum (cint_t c)
{
return ase_isalnum (c);
}
StdAwk::bool_t StdAwk::isSpace (cint_t c)
{
return ase_isspace (c);
}
StdAwk::bool_t StdAwk::isPrint (cint_t c)
{
return ase_isprint (c);
}
StdAwk::bool_t StdAwk::isGraph (cint_t c)
{
return ase_isgraph (c);
}
StdAwk::bool_t StdAwk::isCntrl (cint_t c)
{
return ase_iscntrl (c);
}
StdAwk::bool_t StdAwk::isPunct (cint_t c)
{
return ase_ispunct (c);
}
StdAwk::cint_t StdAwk::toUpper (cint_t c)
{
return ase_toupper (c);
}
StdAwk::cint_t StdAwk::toLower (cint_t c)
{
return ase_tolower (c);
}
// miscellaneous primitive
StdAwk::real_t StdAwk::pow (real_t x, real_t y)
{
return ::pow (x, y);
}
int StdAwk::vsprintf (
char_t* buf, size_t size, const char_t* fmt, va_list arg)
{
return ase_vsprintf (buf, size, fmt, arg);
}
void StdAwk::vdprintf (const char_t* fmt, va_list arg)
{
ase_vfprintf (stderr, fmt, arg);
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.hpp,v 1.7 2007/05/11 16:25:38 bacon Exp $
* $Id: StdAwk.hpp,v 1.9 2007/05/16 14:44:13 bacon Exp $
*/
#ifndef _ASE_AWK_STDAWK_HPP_
@ -7,7 +7,6 @@
#include <ase/awk/Awk.hpp>
namespace ASE
{
class StdAwk: public Awk
@ -18,6 +17,8 @@ namespace ASE
int open ();
protected:
// builtin functions
int sin (Return* ret, const Argument* args, size_t nargs);
int cos (Return* ret, const Argument* args, size_t nargs);
int tan (Return* ret, const Argument* args, size_t nargs);
@ -33,6 +34,45 @@ namespace ASE
int strfgmtime (Return* ret, const Argument* args, size_t nargs);
int system (Return* ret, const Argument* args, size_t nargs);
// pipe io handlers
int openPipe (Pipe& io);
int closePipe (Pipe& io);
ssize_t readPipe (Pipe& io, char_t* buf, size_t len);
ssize_t writePipe (Pipe& io, char_t* buf, size_t len);
int flushPipe (Pipe& io);
// file io handlers
int openFile (File& io);
int closeFile (File& io);
ssize_t readFile (File& io, char_t* buf, size_t len);
ssize_t writeFile (File& io, char_t* buf, size_t len);
int flushFile (File& io);
// primitive handlers
void* allocMem (size_t n);
void* reallocMem (void* ptr, size_t n);
void freeMem (void* ptr);
bool_t isUpper (cint_t c);
bool_t isLower (cint_t c);
bool_t isAlpha (cint_t c);
bool_t isDigit (cint_t c);
bool_t isXdigit (cint_t c);
bool_t isAlnum (cint_t c);
bool_t isSpace (cint_t c);
bool_t isPrint (cint_t c);
bool_t isGraph (cint_t c);
bool_t isCntrl (cint_t c);
bool_t isPunct (cint_t c);
cint_t toUpper (cint_t c);
cint_t toLower (cint_t c);
real_t pow (real_t x, real_t y);
int vsprintf (char_t* buf, size_t size,
const char_t* fmt, va_list arg);
void vdprintf (const char_t* fmt, va_list arg);
protected:
unsigned int seed;
};