chiark / gitweb /
Progressing.
[chiark-tcl.git] / hbytes / chop.c
diff --git a/hbytes/chop.c b/hbytes/chop.c
new file mode 100644 (file)
index 0000000..5d89cd2
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ */
+
+#include <string.h>
+
+#include "hbytes.h"
+
+static int strs(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
+  int rc, l, i;
+
+  l= 0;
+  for (i=1; i<strc; i++) {
+    rc= Tcl_ConvertToType(ip,strv[i],&hbytes_type);
+    if (rc) return rc;
+    l += HBYTES_LEN(HBYTES(strv[i]));
+  }
+  *l_r= l;
+  return TCL_OK;
+}
+
+int app_pre(ClientData cd, Tcl_Interp *ip, int begin,
+           HBytes_Var v, int strc, Tcl_Obj *const *strv) {
+  int ol, rc, al, i, tl;
+
+  rc= strs(ip,strc,strv,&al);  if (rc) return rc;
+
+  ol= HBYTES_LEN(*v.hb);
+  v.hb->start= 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);
+  
+  for (i=1; i<strc; i++) {
+    tl= HBYTES_LEN(HBYTES(strv[i]));
+    memcpy(v.hb->end, HBYTES(strv[i]).start, tl);
+    v.hb->end += 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 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 do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
+                    int strc, Tcl_Obj *const *strv, HBytes_Value *result) {
+  HBytes_Var fake;
+  
+  result->start= result->end= 0;
+  fake.hb= result;
+  return app_pre(cd,ip,1,fake,strc,strv);
+}
+