chiark / gitweb /
Invalidate string rep. Before make expandable buffer.
[chiark-tcl.git] / hbytes / chop.c
1 /*
2  */
3
4 #include <string.h>
5
6 #include "hbytes.h"
7
8 static int strs(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
9   int rc, l, i;
10
11   l= 0;
12   for (i=1; i<strc; i++) {
13     rc= Tcl_ConvertToType(ip,strv[i],&hbytes_type);
14     if (rc) return rc;
15     l += HBYTES_LEN(HBYTES(strv[i]));
16 fprintf(stderr,"strs #%d %d %02x\n",i,l,
17         HBYTES(strv[i]).start ? HBYTES(strv[i]).start[0] : 0xff);
18   }
19   *l_r= l;
20   return TCL_OK;
21 }
22
23 int app_pre(ClientData cd, Tcl_Interp *ip, int begin,
24             HBytes_Var v, int strc, Tcl_Obj *const *strv) {
25   int ol, rc, al, i, tl;
26
27   rc= strs(ip,strc,strv,&al);  if (rc) return rc;
28
29   ol= HBYTES_LEN(*v.hb);
30   v.hb->start= v.hb->end= Tcl_Realloc(v.hb->start, ol + al);
31
32   if (begin) v.hb->end += ol;
33   else memmove(v.hb->start + al, v.hb->start, ol);
34   
35   for (i=1; i<strc; i++) {
36     tl= HBYTES_LEN(HBYTES(strv[i]));
37     memcpy(v.hb->end, HBYTES(strv[i]).start, tl);
38     v.hb->end += tl;
39   }
40   return TCL_OK;
41 }  
42
43 int do_hbytes_append(ClientData cd, Tcl_Interp *ip,
44                      HBytes_Var v, int strc, Tcl_Obj *const *strv) {
45   return app_pre(cd,ip,0,v,strc,strv);
46 }
47   
48 int do_hbytes_prepend(ClientData cd, Tcl_Interp *ip,
49                       HBytes_Var v, int strc, Tcl_Obj *const *strv) {
50   return app_pre(cd,ip,1,v,strc,strv);
51 }
52
53 int do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
54                      int strc, Tcl_Obj *const *strv, HBytes_Value *result) {
55   HBytes_Var fake;
56   
57   result->start= result->end= 0;
58   fake.hb= result;
59   return app_pre(cd,ip,1,fake,strc,strv);
60 }
61