porting tio from xpkit

This commit is contained in:
2008-10-14 05:32:58 +00:00
parent 5cb4f03cf9
commit dc0203008e
16 changed files with 917 additions and 46 deletions

View File

@ -142,3 +142,16 @@ static ase_ccls_t ccls =
};
ase_ccls_t* ase_ccls = &ccls;
#if 0
int ase_wctomb (ase_wchar_t wc, ase_mchar_t* mb, int mblen)
{
if (mblen < MB_CUR_MAX) return -1;
return wctomb (mb, wc);
}
ase_wchar_t ase_mbtowc (ase_mchar_t* mb, int mblen)
{
}
#endif

View File

@ -96,7 +96,13 @@ lda_t* ase_lda_open (mmgr_t* mmgr, size_t ext, size_t capa)
lda = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(lda_t) + ext);
if (lda == ASE_NULL) return ASE_NULL;
return ase_lda_init (lda, mmgr, capa);
if (ase_lda_init (lda, mmgr, capa) == ASE_NULL)
{
ASE_MMGR_FREE (mmgr, lda);
return ASE_NULL;
}
return lda;
}
void ase_lda_close (lda_t* lda)

View File

@ -6,6 +6,7 @@ lib_LTLIBRARIES = libasecmn.la
libasecmn_la_SOURCES = mem.h chr.h \
mem.c chr.c rex.c str_bas.c str_cnv.c str_dyn.c \
lda.c map.c sll.c dll.c opt.c \
tio.c tio_get.c tio_put.c \
misc.c
libasecmn_la_LDFLAGS = -version-info 1:0:0

View File

@ -58,7 +58,8 @@ libLTLIBRARIES_INSTALL = $(INSTALL)
LTLIBRARIES = $(lib_LTLIBRARIES)
libasecmn_la_LIBADD =
am_libasecmn_la_OBJECTS = mem.lo chr.lo rex.lo str_bas.lo str_cnv.lo \
str_dyn.lo lda.lo map.lo sll.lo dll.lo opt.lo misc.lo
str_dyn.lo lda.lo map.lo sll.lo dll.lo opt.lo tio.lo \
tio_get.lo tio_put.lo misc.lo
libasecmn_la_OBJECTS = $(am_libasecmn_la_OBJECTS)
DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/autoconf/depcomp
@ -198,6 +199,7 @@ lib_LTLIBRARIES = libasecmn.la
libasecmn_la_SOURCES = mem.h chr.h \
mem.c chr.c rex.c str_bas.c str_cnv.c str_dyn.c \
lda.c map.c sll.c dll.c opt.c \
tio.c tio_get.c tio_put.c \
misc.c
libasecmn_la_LDFLAGS = -version-info 1:0:0
@ -282,6 +284,9 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_bas.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_cnv.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_dyn.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_get.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_put.Plo@am__quote@
.c.o:
@am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \

View File

@ -91,7 +91,13 @@ sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext)
sll = ASE_MMGR_ALLOC (mmgr, SIZEOF(sll_t) + ext);
if (sll == ASE_NULL) return ASE_NULL;
return ase_sll_init (sll, mmgr);
if (ase_sll_init (sll, mmgr) == ASE_NULL)
{
ASE_MMGR_FREE (mmgr, sll);
return ASE_NULL;
}
return sll;
}
void ase_sll_close (sll_t* sll)

222
ase/lib/cmn/tio.c Normal file
View File

@ -0,0 +1,222 @@
/*
* $Id: tio.c,v 1.13 2006/01/01 13:50:24 bacon Exp $
*/
#include <ase/cmn/tio.h>
#include "mem.h"
ase_tio_t* ase_tio_open (ase_mmgr_t* mmgr, ase_size_t ext)
{
ase_tio_t* tio;
if (mmgr == ASE_NULL)
{
mmgr = ASE_MMGR_GETDFL();
ASE_ASSERTX (mmgr != ASE_NULL,
"Set the memory manager with ASE_MMGR_SETDFL()");
if (mmgr == ASE_NULL) return ASE_NULL;
}
tio = ASE_MMGR_ALLOC (mmgr, ASE_SIZEOF(ase_tio_t) + ext);
if (tio == ASE_NULL) return ASE_NULL;
if (ase_tio_init (tio, mmgr) == ASE_NULL)
{
ASE_MMGR_FREE (mmgr, tio);
return ASE_NULL;
}
return tio;
}
int ase_tio_close (ase_tio_t* tio)
{
int n = ase_tio_fini (tio);
ASE_MMGR_FREE (tio->mmgr, tio);
return n;
}
ase_tio_t* ase_tio_init (ase_tio_t* tio, ase_mmgr_t* mmgr)
{
ASE_MEMSET (tio, 0, ASE_SIZEOF(*tio));
tio->mmgr = mmgr;
/*
tio->input_func = ASE_NULL;
tio->input_arg = ASE_NULL;
tio->output_func = ASE_NULL;
tio->output_arg = ASE_NULL;
tio->input_status = 0;
tio->inbuf_curp = 0;
tio->inbuf_len = 0;
tio->outbuf_len = 0;
*/
tio->errnum = ASE_TIO_ENOERR;
return tio;
}
int ase_tio_fini (ase_tio_t* tio)
{
ase_tio_flush (tio); /* don't care about the result */
if (ase_tio_detin(tio) == -1) return -1;
if (ase_tio_detout(tio) == -1) return -1;
return 0;
}
int ase_tio_geterrnum (ase_tio_t* tio)
{
return tio->errnum;
}
const ase_char_t* ase_tio_geterrstr (ase_tio_t* tio)
{
static const ase_char_t* __errstr[] =
{
ASE_T("no error"),
ASE_T("out of memory"),
ASE_T("no more space"),
ASE_T("illegal utf-8 sequence"),
ASE_T("no input function attached"),
ASE_T("input function returned an error"),
ASE_T("input function failed to open"),
ASE_T("input function failed to closed"),
ASE_T("no output function attached"),
ASE_T("output function returned an error"),
ASE_T("output function failed to open"),
ASE_T("output function failed to closed"),
ASE_T("unknown error")
};
return __errstr[
(tio->errnum < 0 || tio->errnum >= ase_countof(__errstr))?
ase_countof(__errstr) - 1: tio->errnum];
}
int ase_tio_attin (ase_tio_t* tio, ase_tio_io_t input, void* arg)
{
if (ase_tio_detin(tio) == -1) return -1;
ASE_ASSERT (tio->input_func == ASE_NULL);
if (input(ASE_TIO_IO_OPEN, arg, ASE_NULL, 0) == -1)
{
tio->errnum = ASE_TIO_EINPOP;
return -1;
}
tio->input_func = input;
tio->input_arg = arg;
tio->input_status = 0;
tio->inbuf_curp = 0;
tio->inbuf_len = 0;
return 0;
}
int ase_tio_detin (ase_tio_t* tio)
{
if (tio->input_func != ASE_NULL)
{
if (tio->input_func (
ASE_TIO_IO_CLOSE, tio->input_arg, ASE_NULL, 0) == -1)
{
tio->errnum = ASE_TIO_EINPCL;
return -1;
}
tio->input_func = ASE_NULL;
tio->input_arg = ASE_NULL;
}
return 0;
}
int ase_tio_attout (ase_tio_t* tio, ase_tio_io_t output, void* arg)
{
if (ase_tio_detout(tio) == -1) return -1;
ASE_ASSERT (tio->output_func == ASE_NULL);
if (output(ASE_TIO_IO_OPEN, arg, ASE_NULL, 0) == -1)
{
tio->errnum = ASE_TIO_EOUTOP;
return -1;
}
tio->output_func = output;
tio->output_arg = arg;
tio->outbuf_len = 0;
return 0;
}
int ase_tio_detout (ase_tio_t* tio)
{
if (tio->output_func != ASE_NULL)
{
ase_tio_flush (tio); /* don't care about the result */
if (tio->output_func (
ASE_TIO_IO_CLOSE, tio->output_arg, ASE_NULL, 0) == -1)
{
tio->errnum = ASE_TIO_EOUTCL;
return -1;
}
tio->output_func = ASE_NULL;
tio->output_arg = ASE_NULL;
}
return 0;
}
ase_ssize_t ase_tio_flush (ase_tio_t* tio)
{
ase_size_t left, count;
if (tio->output_func == ASE_NULL)
{
tio->errnum = ASE_TIO_ENOUTF;
return (ase_ssize_t)-1;
}
left = tio->outbuf_len;
while (left > 0)
{
ase_ssize_t n;
n = tio->output_func (
ASE_TIO_IO_DATA, tio->output_arg, tio->outbuf, left);
if (n <= -1)
{
tio->outbuf_len = left;
tio->errnum = ASE_TIO_EOUTPT;
return -1;
}
if (n == 0) break;
left -= n;
ase_memcpy (tio->outbuf, &tio->inbuf[n], left);
}
count = tio->outbuf_len - left;
tio->outbuf_len = left;
return (ase_ssize_t)count;
}
void ase_tio_purge (ase_tio_t* tio)
{
tio->input_status = 0;
tio->inbuf_curp = 0;
tio->inbuf_len = 0;
tio->outbuf_len = 0;
tio->errnum = ASE_TIO_ENOERR;
}

156
ase/lib/cmn/tio_get.c Normal file
View File

@ -0,0 +1,156 @@
/*
* $Id: tio_get.c,v 1.8 2005/12/26 07:41:48 bacon Exp $
*/
#include <ase/cmn/tio.h>
#include "mem.h"
#define STATUS_GETC_EILSEQ (1 << 0)
ase_ssize_t ase_tio_getc (ase_tio_t* tio, ase_char_t* c)
{
ase_size_t left = 0;
ase_ssize_t n;
ase_char_t curc;
#ifndef ASE_CHAR_IS_MCHAR
ase_size_t seqlen;
#endif
/* TODO: more efficient to check this...
* maybe better to use ASE_ASSERT
* ASE_ASSERT (tio->input_func != ASE_NULL);
*/
if (tio->input_func == ASE_NULL)
{
tio->errnum = ASE_TIO_ENOINF;
return -1;
}
if (tio->input_status & STATUS_GETC_EILSEQ)
{
tio->input_status &= ~STATUS_GETC_EILSEQ;
tio->errnum = ASE_TIO_EILSEQ;
return -1;
}
if (tio->inbuf_curp >= tio->inbuf_len)
{
getc_conv:
n = tio->input_func (
ASE_TIO_IO_DATA, tio->input_arg,
&tio->inbuf[left], ASE_COUINTOF(tio->inbuf) - left);
if (n == 0) return 0;
if (n <= -1)
{
tio->errnum = ASE_TIO_EINPUT;
return -1;
}
tio->inbuf_curp = 0;
tio->inbuf_len = (ase_size_t)n + left;
}
#ifdef ASE_CHAR_IS_MCHAR
curc = tio->inbuf[tio->inbuf_curp++];
#else
seqlen = ase_mcseqlen (tio->inbuf[tio->inbuf_curp]);
if (seqlen == 0) {
tio->inbuf_curp++; /* skip one byte */
tio->errnum = ASE_TIO_EILSEQ;
return -1;
}
left = tio->inbuf_len - tio->inbuf_curp;
if (left < seqlen) {
ase_memcpy (tio->inbuf, &tio->inbuf[tio->inbuf_curp], left);
tio->inbuf_curp = 0;
tio->inbuf_len = left;
goto getc_conv;
}
n = ase_mctowc (&tio->inbuf[tio->inbuf_curp], seqlen, &curc);
if (n == 0) {
tio->inbuf_curp++; /* skip one byte */
tio->errnum = ASE_TIO_EILSEQ;
return -1;
}
tio->inbuf_curp += n;
#endif
*c = curc;
return 1;
}
ase_ssize_t ase_tio_gets (ase_tio_t* tio, ase_char_t* buf, ase_size_t size)
{
ase_ssize_t n;
if (size <= 0) return 0;
n = ase_tio_getsx (tio, buf, size - 1);
if (n == -1) return -1;
buf[n] = ASE_CHAR('\0');
return n;
}
ase_ssize_t ase_tio_getsx (ase_tio_t* tio, ase_char_t* buf, ase_size_t size)
{
ase_ssize_t n;
ase_char_t* p, * end, c;
if (size <= 0) return 0;
p = buf; end = buf + size;
while (p < end)
{
n = ase_tio_getc (tio, &c);
if (n == -1)
{
if (p > buf && tio->errnum == ASE_TIO_EILSEQ)
{
tio->input_status |= STATUS_GETC_EILSEQ;
break;
}
return -1;
}
if (n == 0) break;
*p++ = c;
if (c == ASE_CHAR('\n')) break;
}
return p - buf;
}
ase_ssize_t ase_tio_getstr (ase_tio_t* tio, ase_str_t* buf)
{
ase_ssize_t n;
ase_char_t c;
ase_str_clear (buf);
for (;;)
{
n = ase_tio_getc (tio, &c);
if (n == -1)
{
if (ASE_STR_LEN(buf) > 0 && tio->errnum == ASE_TIO_EILSEQ)
{
tio->input_status |= STATUS_GETC_EILSEQ;
break;
}
return -1;
}
if (n == 0) break;
if (ase_str_ccat(buf, c) == (ase_size_t)-1)
{
tio->errnum = ASE_TIO_ENOMEM;
return -1;
}
if (c == ASE_CHAR('\n')) break;
}
return ASE_STR_LEN(buf);
}

152
ase/lib/cmn/tio_put.c Normal file
View File

@ -0,0 +1,152 @@
/*
* $Id: tio_put.c,v 1.2 2005/12/26 07:41:48 bacon Exp $
*/
#include <ase/cmn/tio.h>
ase_ssize_t ase_tio_putc (ase_tio_t* tio, ase_char_t c)
{
#ifndef ASE_CHAR_IS_MCHAR
ase_size_t n, i;
ase_mchar_t mc[50];
#endif
if (tio->outbuf_len >= ASE_COUNTOF(tio->outbuf))
{
/* maybe, previous flush operation has failed a few
* times previously. so the buffer is full.
*/
tio->errnum = ASE_TIO_ENOSPC;
return -1;
}
#ifdef ASE_CHAR_IS_MCHAR
tio->outbuf[tio->outbuf_len++] = c;
if (tio->outbuf_len >= ASE_COUNTOF(tio->outbuf))
return ase_tio_flush (tio);
#else
n = ase_wctomc (c, mc, ASE_COUNTOF(mc));
if (n == 0)
{
tio->errnum = ASE_TIO_EILSEQ;
return -1;
}
for (i = 0; i < n; i++)
{
tio->outbuf[tio->outbuf_len++] = mc[i];
if (tio->outbuf_len >= ASE_COUNTOF(tio->outbuf))
{
if (ase_tio_flush(tio) == -1) return -1;
}
}
#endif
if (c == ASE_CHAR('\n') && tio->outbuf_len > 0)
{
if (ase_tio_flush(tio) == -1) return -1;
}
return 1;
}
ase_ssize_t ase_tio_puts (ase_tio_t* tio, const ase_char_t* str)
{
ase_ssize_t n;
const ase_char_t* p;
for (p = str; *p != ASE_CHAR('\0'); p++)
{
n = ase_tio_putc (tio, *p);
if (n == -1) return -1;
if (n == 0) break;
}
return p - str;
}
ase_ssize_t ase_tio_putsx (ase_tio_t* tio, const ase_char_t* str, ase_size_t size)
{
ase_ssize_t n;
const ase_char_t* p, * end;
if (size == 0) return 0;
p = str; end = str + size;
while (p < end)
{
n = ase_tio_putc (tio, *p);
if (n == -1) return -1;
if (n == 0) break;
p++;
}
return p - str;
}
#if 0
ase_ssize_t ase_tio_putsn (ase_tio_t* tio, ...)
{
ase_ssize_t n;
ase_va_list ap;
ase_va_start (ap, tio);
n = ase_tio_putsv (tio, ap);
ase_va_end (ap);
return n;
}
ase_ssize_t ase_tio_putsxn (ase_tio_t* tio, ...)
{
ase_ssize_t n;
ase_va_list ap;
ase_va_start (ap, tio);
n = ase_tio_putsxv (tio, ap);
ase_va_end (ap);
return n;
}
ase_ssize_t ase_tio_putsv (ase_tio_t* tio, ase_va_list ap)
{
const ase_char_t* p;
ase_size_t n, total = 0;
while ((p = ase_va_arg (ap, const ase_char_t*)) != ASE_NULL)
{
if (p[0] == ASE_CHAR('\0')) continue;
n = ase_tio_puts (tio, p);
if (n == -1) return -1;
if (n == 0) break;
total += n;
}
return total;
}
ase_ssize_t ase_tio_putsxv (ase_tio_t* tio, ase_va_list ap)
{
const ase_char_t* p;
ase_size_t len, n, total = 0;
while ((p = ase_va_arg (ap, const ase_char_t*)) != ASE_NULL)
{
len = ase_va_arg (ap, ase_size_t);
if (len == 0) continue;
n = ase_tio_putsx (tio, p, len);
if (n == -1) return -1;
if (n == 0) break;
total += n;
}
return total;
}
#endif