2007-05-06 19:44:00 +00:00
|
|
|
/*
|
2008-12-11 04:19:59 +00:00
|
|
|
* $Id: StdAwk.cpp 468 2008-12-10 10:19:59Z baconevi $
|
2007-09-25 20:25:00 +00:00
|
|
|
*
|
|
|
|
* {License}
|
2007-05-06 19:44:00 +00:00
|
|
|
*/
|
|
|
|
|
2008-03-22 05:57:29 +00:00
|
|
|
#if defined(hpux) || defined(__hpux) || defined(__hpux__)
|
|
|
|
#define _INCLUDE__STDC_A1_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2007-05-06 19:44:00 +00:00
|
|
|
#include <ase/awk/StdAwk.hpp>
|
2007-05-18 01:21:00 +00:00
|
|
|
#include <ase/cmn/str.h>
|
|
|
|
#include <ase/utl/stdio.h>
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-05-12 19:14:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2007-09-11 22:42:00 +00:00
|
|
|
#include <tchar.h>
|
2007-05-12 19:14:00 +00:00
|
|
|
#else
|
2007-09-11 22:42:00 +00:00
|
|
|
#include <wchar.h>
|
2007-05-12 19:14:00 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
ASE_BEGIN_NAMESPACE(ASE)
|
|
|
|
/////////////////////////////////
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
StdAwk::StdAwk ()
|
|
|
|
{
|
|
|
|
seed = (unsigned int)::time(NULL);
|
|
|
|
::srand (seed);
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
#define ADD_FUNC(name,min,max,impl) \
|
|
|
|
do { \
|
|
|
|
if (addFunction (name, min, max, \
|
|
|
|
(FunctionHandler)impl) == -1) \
|
|
|
|
{ \
|
|
|
|
Awk::close (); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
int StdAwk::open ()
|
|
|
|
{
|
|
|
|
int n = Awk::open ();
|
|
|
|
if (n == -1) return n;
|
|
|
|
|
|
|
|
ADD_FUNC (ASE_T("sin"), 1, 1, &StdAwk::sin);
|
|
|
|
ADD_FUNC (ASE_T("cos"), 1, 1, &StdAwk::cos);
|
|
|
|
ADD_FUNC (ASE_T("tan"), 1, 1, &StdAwk::tan);
|
|
|
|
ADD_FUNC (ASE_T("atan"), 1, 1, &StdAwk::atan);
|
|
|
|
ADD_FUNC (ASE_T("atan2"), 2, 2, &StdAwk::atan2);
|
|
|
|
ADD_FUNC (ASE_T("log"), 1, 1, &StdAwk::log);
|
|
|
|
ADD_FUNC (ASE_T("exp"), 1, 1, &StdAwk::exp);
|
|
|
|
ADD_FUNC (ASE_T("sqrt"), 1, 1, &StdAwk::sqrt);
|
|
|
|
ADD_FUNC (ASE_T("int"), 1, 1, &StdAwk::fnint);
|
|
|
|
ADD_FUNC (ASE_T("rand"), 0, 0, &StdAwk::rand);
|
2007-12-05 08:13:38 +00:00
|
|
|
ADD_FUNC (ASE_T("srand"), 0, 1, &StdAwk::srand);
|
2007-09-11 22:42:00 +00:00
|
|
|
ADD_FUNC (ASE_T("systime"), 0, 0, &StdAwk::systime);
|
|
|
|
ADD_FUNC (ASE_T("strftime"), 0, 2, &StdAwk::strftime);
|
|
|
|
ADD_FUNC (ASE_T("strfgmtime"), 0, 2, &StdAwk::strfgmtime);
|
|
|
|
ADD_FUNC (ASE_T("system"), 1, 1, &StdAwk::system);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::sin(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::cos (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::cos(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::tan (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::tan(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-09-02 00:43:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::atan (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::atan(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::atan2 (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::atan2(args[0].toReal(), args[1].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::log (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::log(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::exp (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::exp(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::sqrt (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((real_t)::sqrt(args[0].toReal()));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::fnint (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set (args[0].toInt());
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::rand (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((long_t)::rand());
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
|
|
|
unsigned int prevSeed = seed;
|
2007-12-05 08:13:38 +00:00
|
|
|
|
|
|
|
seed = (nargs == 0)?
|
|
|
|
(unsigned int)::time(NULL):
|
|
|
|
(unsigned int)args[0].toInt();
|
2007-09-11 22:42:00 +00:00
|
|
|
::srand (seed);
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((long_t)prevSeed);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-07 23:14:00 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER) && (_MSC_VER>=1400)
|
|
|
|
#define time_t __time64_t
|
|
|
|
#define time _time64
|
|
|
|
#define localtime _localtime64
|
|
|
|
#define gmtime _gmtime64
|
|
|
|
#endif
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::systime (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((long_t)::time(NULL));
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::strftime (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
|
|
|
const char_t* fmt;
|
|
|
|
size_t fln;
|
|
|
|
|
|
|
|
fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln);
|
|
|
|
time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt();
|
|
|
|
|
|
|
|
char_t buf[128];
|
|
|
|
struct tm* tm;
|
|
|
|
#ifdef _WIN32
|
|
|
|
tm = ::localtime (&t);
|
|
|
|
#else
|
|
|
|
struct tm tmb;
|
|
|
|
tm = ::localtime_r (&t, &tmb);
|
|
|
|
#endif
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
#ifdef ASE_CHAR_IS_MCHAR
|
|
|
|
size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm);
|
|
|
|
#else
|
|
|
|
size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm);
|
|
|
|
#endif
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set (buf, l);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::strfgmtime (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
|
|
|
const char_t* fmt;
|
|
|
|
size_t fln;
|
|
|
|
|
|
|
|
fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln);
|
|
|
|
time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt();
|
|
|
|
|
|
|
|
char_t buf[128];
|
|
|
|
struct tm* tm;
|
|
|
|
#ifdef _WIN32
|
|
|
|
tm = ::gmtime (&t);
|
|
|
|
#else
|
|
|
|
struct tm tmb;
|
|
|
|
tm = ::gmtime_r (&t, &tmb);
|
|
|
|
#endif
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
#ifdef ASE_CHAR_IS_MCHAR
|
|
|
|
size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm);
|
|
|
|
#else
|
|
|
|
size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm);
|
|
|
|
#endif
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set (buf, l);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int StdAwk::system (Run& run, Return& ret, const Argument* args, size_t nargs,
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* name, size_t len)
|
|
|
|
{
|
|
|
|
size_t l;
|
|
|
|
const char_t* ptr = args[0].toStr(&l);
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
#ifdef _WIN32
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((long_t)::_tsystem(ptr));
|
2007-09-11 22:42:00 +00:00
|
|
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
2007-09-25 00:12:00 +00:00
|
|
|
return ret.set ((long_t)::system(ptr));
|
2007-09-11 22:42:00 +00:00
|
|
|
#else
|
2008-08-21 04:58:19 +00:00
|
|
|
char* mbs = (char*) ase_awk_alloc (awk, l*5+1);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (mbs == ASE_NULL) return -1;
|
|
|
|
|
|
|
|
::size_t mbl = ::wcstombs (mbs, ptr, l*5);
|
2007-09-25 00:12:00 +00:00
|
|
|
if (mbl == (::size_t)-1)
|
|
|
|
{
|
|
|
|
ase_awk_free (awk, mbs);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
mbs[mbl] = '\0';
|
2007-09-25 00:12:00 +00:00
|
|
|
int n = ret.set ((long_t)::system(mbs));
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_free (awk, mbs);
|
|
|
|
return n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int StdAwk::openPipe (Pipe& io)
|
|
|
|
{
|
|
|
|
Awk::Pipe::Mode mode = io.getMode();
|
|
|
|
FILE* fp = NULL;
|
|
|
|
|
|
|
|
switch (mode)
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
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;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (fp == NULL) return -1;
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
io.setHandle (fp);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int StdAwk::closePipe (Pipe& io)
|
|
|
|
{
|
|
|
|
fclose ((FILE*)io.getHandle());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
|
|
|
|
{
|
|
|
|
FILE* fp = (FILE*)io.getHandle();
|
|
|
|
ssize_t n = 0;
|
|
|
|
|
|
|
|
while (n < (ssize_t)len)
|
|
|
|
{
|
|
|
|
ase_cint_t c = ase_fgetc (fp);
|
2008-01-16 00:36:07 +00:00
|
|
|
if (c == ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
if (ase_ferror(fp)) n = -1;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
buf[n++] = c;
|
|
|
|
if (c == ASE_T('\n')) break;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
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'))
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
|
|
|
|
if (fputc ('\0', fp) == EOF)
|
|
|
|
#else
|
|
|
|
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF)
|
|
|
|
#endif
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
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.
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
|
|
|
int n = fprintf (fp, "%.*ls", chunk, buf);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (n >= 0)
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
size_t x;
|
2007-11-10 00:20:00 +00:00
|
|
|
for (x = 0; x < chunk; x++)
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
if (buf[x] == ASE_T('\0')) break;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
n = x;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
#else
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
|
|
|
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, buf);
|
2007-09-11 22:42:00 +00:00
|
|
|
#endif
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-11-10 00:20:00 +00:00
|
|
|
if (n < 0 || n > chunk) return -1;
|
2007-09-11 22:42:00 +00:00
|
|
|
left -= n; buf += n;
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return len;
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int StdAwk::flushPipe (Pipe& io)
|
|
|
|
{
|
|
|
|
return ::fflush ((FILE*)io.getHandle());
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
// file io handlers
|
|
|
|
int StdAwk::openFile (File& io)
|
|
|
|
{
|
|
|
|
Awk::File::Mode mode = io.getMode();
|
|
|
|
FILE* fp = NULL;
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
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;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (fp == NULL) return -1;
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
io.setHandle (fp);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int StdAwk::closeFile (File& io)
|
|
|
|
{
|
|
|
|
fclose ((FILE*)io.getHandle());
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len)
|
|
|
|
{
|
|
|
|
FILE* fp = (FILE*)io.getHandle();
|
|
|
|
ssize_t n = 0;
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
while (n < (ssize_t)len)
|
|
|
|
{
|
|
|
|
ase_cint_t c = ase_fgetc (fp);
|
2008-01-16 00:36:07 +00:00
|
|
|
if (c == ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
if (ase_ferror(fp)) n = -1;
|
|
|
|
break;
|
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
|
|
|
|
buf[n++] = c;
|
|
|
|
if (c == ASE_T('\n')) break;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len)
|
|
|
|
{
|
|
|
|
FILE* fp = (FILE*)io.getHandle();
|
|
|
|
size_t left = len;
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*buf == ASE_T('\0'))
|
2007-05-18 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) return -1;
|
|
|
|
left -= 1; buf += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
|
|
|
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, buf);
|
|
|
|
if (n < 0 || n > chunk) return -1;
|
2007-09-11 22:42:00 +00:00
|
|
|
left -= n; buf += n;
|
2007-05-18 01:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return len;
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int StdAwk::flushFile (File& io)
|
|
|
|
{
|
|
|
|
return ::fflush ((FILE*)io.getHandle());
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
// memory allocation primitives
|
|
|
|
void* StdAwk::allocMem (size_t n)
|
|
|
|
{
|
|
|
|
return ::malloc (n);
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void* StdAwk::reallocMem (void* ptr, size_t n)
|
|
|
|
{
|
|
|
|
return ::realloc (ptr, n);
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void StdAwk::freeMem (void* ptr)
|
|
|
|
{
|
|
|
|
::free (ptr);
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2008-08-21 04:58:19 +00:00
|
|
|
// character handling primitive
|
|
|
|
Awk::bool_t StdAwk::isType (cint_t c, ccls_type_t type)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2008-10-15 04:08:31 +00:00
|
|
|
return ase_ccls_is (c, (ase_ccls_type_t)type);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2008-08-21 04:58:19 +00:00
|
|
|
Awk::cint_t StdAwk::transCase (cint_t c, ccls_type_t type)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2008-10-15 04:08:31 +00:00
|
|
|
return ase_ccls_to (c, (ase_ccls_type_t)type);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
// miscellaneous primitive
|
|
|
|
StdAwk::real_t StdAwk::pow (real_t x, real_t y)
|
|
|
|
{
|
|
|
|
return ::pow (x, y);
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int StdAwk::vsprintf (
|
|
|
|
char_t* buf, size_t size, const char_t* fmt, va_list arg)
|
|
|
|
{
|
|
|
|
return ase_vsprintf (buf, size, fmt, arg);
|
|
|
|
}
|
2007-05-18 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
ASE_END_NAMESPACE(ASE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|