qse/ase/awk/awk.c

81 lines
1.6 KiB
C
Raw Normal View History

2005-11-06 12:01:29 +00:00
/*
2005-11-14 15:23:54 +00:00
* $Id: awk.c,v 1.3 2005-11-14 15:23:53 bacon Exp $
2005-11-06 12:01:29 +00:00
*/
#include <xp/awk/awk.h>
#include <xp/bas/memory.h>
2005-11-07 16:02:44 +00:00
#include <xp/bas/assert.h>
2005-11-06 12:01:29 +00:00
xp_awk_t* xp_awk_open (xp_awk_t* awk)
{
if (awk == XP_NULL) {
awk = (xp_awk_t*) xp_malloc (xp_sizeof(awk));
if (awk == XP_NULL) return XP_NULL;
awk->__malloced = xp_true;
}
else awk->__malloced = xp_false;
2005-11-14 15:23:54 +00:00
if (xp_str_open(&awk->token.name, 128) == XP_NULL) {
2005-11-07 16:02:44 +00:00
if (awk->__malloced) xp_free (awk);
return XP_NULL;
}
2005-11-06 12:01:29 +00:00
awk->errnum = XP_AWK_ENOERR;
2005-11-07 16:02:44 +00:00
awk->source_func = XP_NULL;
awk->input_func = XP_NULL;
awk->output_func = XP_NULL;
awk->source_arg = XP_NULL;
awk->input_arg = XP_NULL;
awk->output_arg = XP_NULL;
awk->lex.curc = XP_CHAR_EOF;
2005-11-14 15:23:54 +00:00
awk->lex.ungotc_count = 0;
2005-11-07 16:02:44 +00:00
2005-11-06 12:01:29 +00:00
return awk;
}
int xp_awk_close (xp_awk_t* awk)
{
2005-11-07 16:02:44 +00:00
if (xp_awk_detach_source(awk) == -1) return -1;
2005-11-14 15:23:54 +00:00
xp_str_close (&awk->token.name);
2005-11-06 12:01:29 +00:00
if (awk->__malloced) xp_free (awk);
return 0;
}
2005-11-07 16:02:44 +00:00
int xp_awk_attach_source (xp_awk_t* awk, xp_awk_io_t source, void* arg)
{
if (xp_awk_detach_source(awk) == -1) return -1;
xp_assert (awk->source_func == XP_NULL);
if (source(XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1) {
awk->errnum = XP_AWK_ESRCOP;
return -1;
}
awk->source_func = source;
awk->source_arg = arg;
2005-11-14 15:23:54 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
2005-11-07 16:02:44 +00:00
return 0;
}
int xp_awk_detach_source (xp_awk_t* awk)
{
if (awk->source_func != XP_NULL) {
if (awk->source_func(XP_AWK_IO_CLOSE, awk->source_arg, XP_NULL, 0) == -1) {
awk->errnum = XP_AWK_ESRCCL;
return -1;
}
awk->source_func = XP_NULL;
awk->source_arg = XP_NULL;
2005-11-14 15:23:54 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
2005-11-07 16:02:44 +00:00
}
return 0;
}