X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=hbytes%2Fchop.c;h=bbc423322b5675b55a250573be2e54424be210a2;hp=5d89cd22c0f44969a3a043f9a8014f3e18be8784;hb=ca8b96bf81245f21fe3906c71dc2994bfc5e516f;hpb=503f816f2157f764c7c93c873047aad4ca63e10d diff --git a/hbytes/chop.c b/hbytes/chop.c index 5d89cd2..bbc4233 100644 --- a/hbytes/chop.c +++ b/hbytes/chop.c @@ -1,59 +1,106 @@ /* + * hbytes - hex-stringrep efficient byteblocks for Tcl + * Copyright 2006-2012 Ian Jackson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301, USA. */ -#include -#include "hbytes.h" +#include "chiark_tcl_hbytes.h" -static int strs(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) { - int rc, l, i; +static int strs1(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) { + int rc, l, i, pl; l= 0; for (i=1; istart= v.hb->end= Tcl_Realloc(v.hb->start, ol + al); - - if (begin) v.hb->end += ol; - else memmove(v.hb->start + al, v.hb->start, ol); +static void strs2(Byte *dest, int strc, Tcl_Obj *const *strv) { + int tl, i; for (i=1; iend, HBYTES(strv[i]).start, tl); - v.hb->end += tl; + tl= cht_hb_len(OBJ_HBYTES(strv[i])); + memcpy(dest, cht_hb_data(OBJ_HBYTES(strv[i])), tl); + dest += tl; } - return TCL_OK; -} +} -int do_hbytes_append(ClientData cd, Tcl_Interp *ip, - HBytes_Var v, int strc, Tcl_Obj *const *strv) { - return app_pre(cd,ip,1,v,strc,strv); +int cht_do_hbytes_prepend(ClientData cd, Tcl_Interp *ip, + HBytes_Var v, int strc, Tcl_Obj *const *strv) { + int rc, el; + Byte *dest; + + rc= strs1(ip,strc,strv,&el); if (rc) return rc; + dest= cht_hb_prepend(v.hb, el); + strs2(dest, strc,strv); + return TCL_OK; } -int do_hbytes_prepend(ClientData cd, Tcl_Interp *ip, - HBytes_Var v, int strc, Tcl_Obj *const *strv) { - return app_pre(cd,ip,0,v,strc,strv); +int cht_do_hbytes_append(ClientData cd, Tcl_Interp *ip, + HBytes_Var v, int strc, Tcl_Obj *const *strv) { + int rc, el; + Byte *dest; + + rc= strs1(ip,strc,strv,&el); if (rc) return rc; + dest= cht_hb_append(v.hb, el); + strs2(dest, strc,strv); + return TCL_OK; } -int do_hbytes_concat(ClientData cd, Tcl_Interp *ip, +int cht_do_hbytes_concat(ClientData cd, Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, HBytes_Value *result) { - HBytes_Var fake; + int rc, l; + Byte *dest; - result->start= result->end= 0; - fake.hb= result; - return app_pre(cd,ip,1,fake,strc,strv); + rc= strs1(ip,strc,strv,&l); if (rc) return rc; + dest= cht_hb_arrayspace(result,l); + strs2(dest, strc,strv); + return TCL_OK; +} + +static int underrun(Tcl_Interp *ip) { + return cht_staticerr(ip,"data underrun","HBYTES LENGTH UNDERRUN"); } +int cht_do_hbytes_unprepend(ClientData cd, Tcl_Interp *ip, + HBytes_Var v, int preflength, HBytes_Value *result) { + const Byte *rdata= cht_hb_unprepend(v.hb, preflength); + if (!rdata) return underrun(ip); + cht_hb_array(result, rdata, preflength); + return TCL_OK; +} + +int cht_do_hbytes_unappend(ClientData cd, Tcl_Interp *ip, + HBytes_Var v, int suflength, HBytes_Value *result) { + const Byte *rdata= cht_hb_unappend(v.hb, suflength); + if (!rdata) return underrun(ip); + cht_hb_array(result, rdata, suflength); + return TCL_OK; +} + +int cht_do_hbytes_chopto(ClientData cd, Tcl_Interp *ip, + HBytes_Var v, int newlength, HBytes_Value *result) { + int suflength= cht_hb_len(v.hb) - newlength; + return cht_do_hbytes_unappend(0,ip,v, suflength, result); +}