From fbb4a817018d6596e7cfed3addec3227dae398c9 Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Fri, 25 Oct 2019 22:52:00 +0300 Subject: [PATCH] Added cstrwrap wrappers functions. it's dirty but quick implementation; --- lib/Makefile.am | 2 +- lib/cswrap.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 lib/cswrap.c diff --git a/lib/Makefile.am b/lib/Makefile.am index d15eb8b..203e320 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -14,7 +14,7 @@ lib_LTLIBRARIES = libsexpr.la libsexpr_la_SOURCES = \ - cstring.c event_temp.c faststack.c io.c \ + cstring.c event_temp.c faststack.c io.c cswrap.c \ parser.c sexp.c sexp_memory.c sexp_ops.c sexp_vis.c libsexpr_la_LDFLAGS = diff --git a/lib/cswrap.c b/lib/cswrap.c new file mode 100644 index 0000000..aab6375 --- /dev/null +++ b/lib/cswrap.c @@ -0,0 +1,131 @@ +/** +@cond IGNORE + +A wrapper functions to deal with a cstrings done as an io wrapper +Copyright (C) 2018 Alexander Vdolainen + + +It is free software: you can redistribute it and/or modify it +under the terms of the GNU Lesser General Public License as published +by the Free Software Foundation, either version 2.1 of the License, or +(at your option) any later version. + +This is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see ."; + +@endcond +**/ + +#include +#include +#include +#include +#include + +sexp_cstrwrap_t *init_cstrwrap(const char *src) +{ + sexp_cstrwrap_t *ow = NULL; + + if(!src) { + sexp_errno = SEXP_ERR_NULLSTRING; + return NULL; + } + + ow = (sexp_cstrwrap_t *)sexp_calloc(1, sizeof(sexp_cstrwrap_t)); + + if(!ow) { + sexp_errno = SEXP_ERR_MEMORY; + return NULL; + } + + memset(ow, 0, sizeof(sexp_cstrwrap_t)); + ow->seval = src; + ow->sln = strlen(src); + + return ow; +} + +void destroy_cstrwrap(sexp_cstrwrap_t *csw) +{ + if(!csw) return; + + if(csw->cc) destroy_continuation(csw->cc); + sexp_free(csw->cc, sizeof(sexp_cstrwrap_t)); + + return; +} + +sexp_t *get_one_sexp(sexp_cstrwrap_t *wr) +{ + sexp_t *sx = NULL; + int sbuf_size = 0; + + if(!wr) return NULL; + + /* check for existing leftovers in continuation */ + if(wr->cc && wr->cc->lastPos) { + wr->cc = cparse_sexp(wr->buf, wr->rl, wr->cc); + + if(!wr->cc) return NULL; /* check the errno: it's set here */ + if(wr->cc->last_sexp) { + sx = wr->cc->last_sexp; + wr->cc->last_sexp = NULL; + return sx; + } + wr->rl = 0; + } + + if(wr->rl == 0) { + /* check for EOB */ + if(wr->sln == wr->cnt) { + sexp_errno = SEXP_ERR_OK; + return NULL; /* we're done here */ + } + + if(wr->sln - wr->cnt >= CSTRBUFSIZ) sbuf_size = CSTRBUFSIZ; + else sbuf_size = wr->sln - wr->cnt; + + /* FIXME: it's ugly to copy TODO: optimize it there */ + memcpy(wr->buf, wr->seval + wr->cnt, sbuf_size); + wr->cnt += sbuf_size; + wr->rl = sbuf_size; + } + + wr->cc = cparse_sexp(wr->buf, wr->rl, wr->cc); + + /* if expression more then buf size, read more */ + while(wr->cc->last_sexp == NULL) { + if(wr->cc->error != SEXP_ERR_OK) { + sexp_errno = wr->cc->error; + return NULL; + } + + /* TODO: FIXME: remove copy-n-paste */ + /* check for EOB */ + if(wr->sln == wr->cnt) { + sexp_errno = SEXP_ERR_IO_EMPTY; + return NULL; /* we're done here, s expression didn't finished */ + } + + if(wr->sln - wr->cnt >= CSTRBUFSIZ) sbuf_size = CSTRBUFSIZ; + else sbuf_size = wr->sln - wr->cnt; + + /* FIXME: it's ugly to copy TODO: optimize it there */ + memcpy(wr->buf, wr->seval + wr->cnt, sbuf_size); + wr->cnt += sbuf_size; + wr->rl = sbuf_size; + + wr->cc = cparse_sexp(wr->buf, wr->rl, wr->cc); + wr->rl = 0; + } + + sx = wr->cc->last_sexp; + wr->cc->last_sexp = NULL; + + return sx; +}