chiark / gitweb /
5d89cd22c0f44969a3a043f9a8014f3e18be8784
[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   }
17   *l_r= l;
18   return TCL_OK;
19 }
20
21 int app_pre(ClientData cd, Tcl_Interp *ip, int begin,
22             HBytes_Var v, int strc, Tcl_Obj *const *strv) {
23   int ol, rc, al, i, tl;
24
25   rc= strs(ip,strc,strv,&al);  if (rc) return rc;
26
27   ol= HBYTES_LEN(*v.hb);
28   v.hb->start= v.hb->end= Tcl_Realloc(v.hb->start, ol + al);
29
30   if (begin) v.hb->end += ol;
31   else memmove(v.hb->start + al, v.hb->start, ol);
32   
33   for (i=1; i<strc; i++) {
34     tl= HBYTES_LEN(HBYTES(strv[i]));
35     memcpy(v.hb->end, HBYTES(strv[i]).start, tl);
36     v.hb->end += tl;
37   }
38   return TCL_OK;
39 }  
40
41 int do_hbytes_append(ClientData cd, Tcl_Interp *ip,
42                      HBytes_Var v, int strc, Tcl_Obj *const *strv) {
43   return app_pre(cd,ip,1,v,strc,strv);
44 }
45   
46 int do_hbytes_prepend(ClientData cd, Tcl_Interp *ip,
47                       HBytes_Var v, int strc, Tcl_Obj *const *strv) {
48   return app_pre(cd,ip,0,v,strc,strv);
49 }
50
51 int do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
52                      int strc, Tcl_Obj *const *strv, HBytes_Value *result) {
53   HBytes_Var fake;
54   
55   result->start= result->end= 0;
56   fake.hb= result;
57   return app_pre(cd,ip,1,fake,strc,strv);
58 }
59