qse/ase/awk/extio.c

138 lines
2.3 KiB
C
Raw Normal View History

2006-06-16 07:37:27 +00:00
/*
2006-06-19 09:10:57 +00:00
* $Id: extio.c,v 1.5 2006-06-19 09:08:50 bacon Exp $
2006-06-16 07:37:27 +00:00
*/
#include <xp/awk/awk_i.h>
#ifndef XP_AWK_STAND_ALONE
#include <xp/bas/assert.h>
#include <xp/bas/string.h>
#include <xp/bas/memory.h>
#endif
2006-06-19 09:10:57 +00:00
int xp_awk_readextio (xp_awk_run_t* run,
xp_awk_extio_t** extio, xp_awk_io_t handler,
const xp_char_t* name, int* errnum)
2006-06-16 07:37:27 +00:00
{
2006-06-19 09:10:57 +00:00
xp_awk_extio_t* p = *extio;
xp_bool_t extio_created = xp_false;
2006-06-16 07:37:27 +00:00
while (p != XP_NULL)
{
2006-06-19 09:10:57 +00:00
if (xp_strcmp(p->name,name) == 0) break;
2006-06-16 07:37:27 +00:00
p = p->next;
}
if (p == XP_NULL)
{
2006-06-19 09:10:57 +00:00
p = (xp_awk_extio_t*) xp_malloc (xp_sizeof(xp_awk_extio_t));
2006-06-16 07:37:27 +00:00
if (p == XP_NULL)
{
2006-06-16 11:24:32 +00:00
*errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-19 09:10:57 +00:00
p->name = xp_strdup (name);
2006-06-16 07:37:27 +00:00
if (p->name == XP_NULL)
{
xp_free (p);
2006-06-16 11:24:32 +00:00
*errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
p->handle = XP_NULL;
2006-06-19 09:10:57 +00:00
p->next = XP_NULL;
extio_created = xp_true;
2006-06-16 07:37:27 +00:00
}
if (p->handle == XP_NULL)
{
2006-06-19 09:10:57 +00:00
if (handler (XP_AWK_INPUT_OPEN, p, XP_NULL, 0) == -1)
2006-06-16 07:37:27 +00:00
{
2006-06-19 09:10:57 +00:00
if (extio_created)
{
xp_free (p->name);
xp_free (p);
}
/* TODO: set ERRNO */
*errnum = XP_AWK_ENOERR;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-19 04:38:51 +00:00
if (p->handle == XP_NULL)
{
2006-06-19 09:10:57 +00:00
if (extio_created)
{
xp_free (p->name);
xp_free (p);
}
2006-06-19 04:38:51 +00:00
2006-06-19 09:10:57 +00:00
/* wrong implementation of user io handler.
* the correct io handler should set p->handle to
* non XP_NULL when it returns 0. */
*errnum = XP_AWK_EIOIMPL;
2006-06-19 04:38:51 +00:00
return -1;
}
2006-06-16 07:37:27 +00:00
}
2006-06-19 09:10:57 +00:00
/* link it to the extio chain */
if (extio_created)
{
p->next = *extio;
*extio = p;
}
2006-06-16 07:37:27 +00:00
{
2006-06-19 04:38:51 +00:00
xp_char_t buf[1024];
2006-06-19 09:10:57 +00:00
if (handler (XP_AWK_INPUT_DATA, p, buf, xp_countof(buf)) == 0)
2006-06-19 04:38:51 +00:00
{
2006-06-19 09:10:57 +00:00
/* no more data. end of data stream */
2006-06-19 04:38:51 +00:00
return 0;
}
xp_printf(XP_TEXT("%s"), buf);
2006-06-16 07:37:27 +00:00
}
return 1;
}
2006-06-19 09:10:57 +00:00
int xp_awk_closeextio (xp_awk_run_t* run,
xp_awk_extio_t** extio, xp_awk_io_t handler,
const xp_char_t* name, int* errnum)
2006-06-16 07:37:27 +00:00
{
2006-06-19 09:10:57 +00:00
xp_awk_extio_t* p = *extio, * px = XP_NULL;
2006-06-16 07:37:27 +00:00
while (p != XP_NULL)
{
2006-06-19 09:10:57 +00:00
if (xp_strcmp(p->name,name) == 0)
2006-06-16 07:37:27 +00:00
{
2006-06-19 09:10:57 +00:00
if (handler (XP_AWK_INPUT_CLOSE, p, XP_NULL, 0) == -1)
2006-06-19 04:38:51 +00:00
{
2006-06-19 09:10:57 +00:00
/* this is not a run-time error.*/
*errnum = XP_AWK_ENOERR;
2006-06-19 04:38:51 +00:00
return -1;
}
2006-06-16 07:37:27 +00:00
2006-06-19 04:38:51 +00:00
p->handle = XP_NULL;
2006-06-19 09:10:57 +00:00
//if (opt_remove_closed_extio) // TODO:...
2006-06-16 07:37:27 +00:00
//{
if (px != XP_NULL) px->next = p->next;
2006-06-19 09:10:57 +00:00
else *extio = p->next;
2006-06-16 07:37:27 +00:00
xp_free (p->name);
xp_free (p);
//}
2006-06-19 04:38:51 +00:00
return 0;
2006-06-16 07:37:27 +00:00
}
px = p;
p = p->next;
}
2006-06-19 09:10:57 +00:00
/* this is not a run-time error */
*errnum = XP_AWK_ENOERR;
2006-06-16 07:37:27 +00:00
return -1;
}