touched up code a little

This commit is contained in:
2009-07-02 07:14:39 +00:00
parent dd36ca0725
commit d725c01bac
15 changed files with 858 additions and 438 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.cpp 195 2009-06-10 13:18:25Z hyunghwan.chung $
* $Id: StdAwk.cpp 220 2009-07-01 13:14:39Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -65,19 +65,13 @@ int StdAwk::open ()
ADDFNC (QSE_T("srand"), 0, 1, &StdAwk::srand);
ADDFNC (QSE_T("system"), 1, 1, &StdAwk::system);
return 0;
}
int StdAwk::run (const char_t** args, size_t nargs)
{
qse_ntime_t now;
if (qse_gettime(&now) == -1) this->seed = 0;
else this->seed = (unsigned int)now;
::srand (this->seed);
return Awk::run (args, nargs);
return 0;
}
int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,
@ -391,6 +385,346 @@ int StdAwk::flushFile (File& io)
return qse_fio_flush ((qse_fio_t*)io.getHandle());
}
#if 0
// console io handlers
int StdAwk::openConsole (Console& io)
{
qse_sio_t* fp;
Console::Mode mode = io.getMode();
switch (mode)
{
case Console::READ:
{
if (runarg.ptr == QSE_NULL)
{
io.setHandle (qse_sio_in);
return 1;
}
else
{
qse_sio_t* sio;
const qse_char_t* file;
qse_awk_val_t* argv;
qse_map_t* map;
qse_map_pair_t* pair;
qse_char_t ibuf[128];
qse_size_t ibuflen;
qse_awk_val_t* v;
qse_awk_rtx_valtostr_out_t out;
nextfile:
file = runarg.ptr[runarg_index];
if (file == QSE_NULL)
{
/* no more input file */
if (runarg_count == 0)
{
/* all ARGVs are empty strings.
* so no console files were opened.
* open the standard input here.
*
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
io.setHandle (qse_sio_in);
runarg_count++;
return 1;
}
return 0;
}
/* handle special case when ARGV[x] has been altered.
* so from here down, the file name gotten from
* rxtn->c.in.files is not important and is overridden
* from ARGV.
* 'BEGIN { ARGV[1]="file3"; }
* { print $0; }' file1 file2
*/
argv = qse_awk_rtx_getgbl (rtx, QSE_AWK_GBL_ARGV);
QSE_ASSERT (argv != QSE_NULL);
QSE_ASSERT (argv->type == QSE_AWK_VAL_MAP);
map = ((qse_awk_val_map_t*)argv)->map;
QSE_ASSERT (map != QSE_NULL);
ibuflen = qse_awk_longtostr (
rtx->awk, rxtn->c.in.index + 1, 10, QSE_NULL,
ibuf, QSE_COUNTOF(ibuf));
pair = qse_map_search (map, ibuf, ibuflen);
QSE_ASSERT (pair != QSE_NULL);
v = QSE_MAP_VPTR(pair);
QSE_ASSERT (v != QSE_NULL);
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return -1;
if (out.u.cpldup.len == 0)
{
/* the name is empty */
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
rxtn->c.in.index++;
goto nextfile;
}
if (qse_strlen(out.u.cpldup.ptr) < out.u.cpldup.len)
{
/* the name contains one or more '\0' */
qse_cstr_t errarg;
errarg.ptr = out.u.cpldup.ptr;
/* use this length not to contains '\0'
* in an error message */
errarg.len = qse_strlen(out.u.cpldup.ptr);
qse_awk_rtx_seterror (
rtx, QSE_AWK_EIONMNL, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
file = out.u.cpldup.ptr;
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
{
/* special file name '-' */
sio = qse_sio_in;
}
else
{
sio = qse_sio_open (
rtx->awk->mmgr, 0, file, QSE_SIO_READ);
if (sio == QSE_NULL)
{
qse_cstr_t errarg;
errarg.ptr = file;
errarg.len = qse_strlen(file);
qse_awk_rtx_seterror (
rtx, QSE_AWK_EOPEN, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
}
if (qse_awk_rtx_setfilename (
rtx, file, qse_strlen(file)) == -1)
{
if (sio != qse_sio_in) qse_sio_close (sio);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
riod->handle = sio;
/* increment the counter of files successfully opened */
rxtn->c.in.count++;
}
rxtn->c.in.index++;
}
break;
}
case Console::WRITE:
break;
}
#if 0
FILE* fp = QSE_NULL;
const char_t* fn = QSE_NULL;
switch (mode)
{
case StdAwk::Console::READ:
if (numConInFiles == 0) fp = stdin;
else
{
fn = conInFile[0];
fp = qse_fopen (fn, QSE_T("r"));
}
break;
case StdAwk::Console::WRITE:
if (numConOutFiles == 0) fp = stdout;
else
{
fn = conOutFile[0];
fp = qse_fopen (fn, QSE_T("w"));
}
break;
}
if (fp == NULL) return -1;
ConTrack* t = (ConTrack*)
qse_awk_alloc (awk, QSE_SIZEOF(ConTrack));
if (t == QSE_NULL)
{
if (fp != stdin && fp != stdout) fclose (fp);
return -1;
}
t->handle = fp;
t->nextConIdx = 1;
if (fn != QSE_NULL)
{
if (io.setFileName(fn) == -1)
{
if (fp != stdin && fp != stdout) fclose (fp);
qse_awk_free (awk, t);
return -1;
}
}
io.setHandle (t);
return 1;
#endif
}
int StdAwk::closeConsole (Console& io)
{
ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle;
if (fp == stdout || fp == stderr) fflush (fp);
if (fp != stdin && fp != stdout && fp != stderr) fclose (fp);
qse_awk_free (awk, t);
return 0;
}
ssize_t StdAwk::readConsole (Console& io, char_t* buf, size_t len)
{
ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle;
ssize_t n = 0;
while (n < (ssize_t)len)
{
qse_cint_t c = qse_fgetc (fp);
if (c == QSE_CHAR_EOF)
{
if (qse_ferror(fp)) return -1;
if (t->nextConIdx >= numConInFiles) break;
const char_t* fn = conInFile[t->nextConIdx];
FILE* nfp = qse_fopen (fn, QSE_T("r"));
if (nfp == QSE_NULL) return -1;
if (io.setFileName(fn) == -1 || io.setFNR(0) == -1)
{
fclose (nfp);
return -1;
}
fclose (fp);
fp = nfp;
t->nextConIdx++;
t->handle = fp;
if (n == 0) continue;
else break;
}
buf[n++] = c;
if (c == QSE_T('\n')) break;
}
return n;
}
ssize_t StdAwk::writeConsole (Console& io, const char_t* buf, size_t len)
{
ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle;
size_t left = len;
while (left > 0)
{
if (*buf == QSE_T('\0'))
{
if (qse_fputc(*buf,fp) == QSE_CHAR_EOF) return -1;
left -= 1; buf += 1;
}
else
{
int chunk = (left > QSE_TYPE_MAX(int))? QSE_TYPE_MAX(int): (int)left;
int n = qse_fprintf (fp, QSE_T("%.*s"), chunk, buf);
if (n < 0 || n > chunk) return -1;
left -= n; buf += n;
}
}
return len;
}
int StdAwk::flushConsole (Console& io)
{
ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle;
return ::fflush (fp);
}
int StdAwk::nextConsole (Console& io)
{
StdAwk::Console::Mode mode = io.getMode();
ConTrack* t = (ConTrack*)io.getHandle();
FILE* ofp = t->handle;
FILE* nfp = QSE_NULL;
const char_t* fn = QSE_NULL;
switch (mode)
{
case StdAwk::Console::READ:
if (t->nextConIdx >= numConInFiles) return 0;
fn = conInFile[t->nextConIdx];
nfp = qse_fopen (fn, QSE_T("r"));
break;
case StdAwk::Console::WRITE:
if (t->nextConIdx >= numConOutFiles) return 0;
fn = conOutFile[t->nextConIdx];
nfp = qse_fopen (fn, QSE_T("w"));
break;
}
if (nfp == QSE_NULL) return -1;
if (fn != QSE_NULL)
{
if (io.setFileName (fn) == -1)
{
fclose (nfp);
return -1;
}
}
fclose (ofp);
t->nextConIdx++;
t->handle = nfp;
return 1;
}
#endif
// memory allocation primitives
void* StdAwk::allocMem (size_t n) throw ()
{