Version 0 of Islist Extension

Updated 2006-01-22 23:54:02

This is a little extension that will check wether a var is (internally) a list or not. Someone wanted something like this so I tryed it. The good thing is, it works :) but since im not really into c i didnt know how to dynamically (?) link it. so i guess you will need to edit the tcl.h include if you folder differs.

I just though i'd post it here, in case someone was looking for something similar, or just wanted another simple (!) extension example.

 /*
 * islist.c --
 *
 *    A dirty Check for list.
 *
 *    (c) Gotisch
 */
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include "/usr/include/tcl8.4/tcl.h" 

 static int
 Islist_Cmd(
  ClientData cdata,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[])
  {
   if (objc < 2) {
      Tcl_SetObjResult(interp, Tcl_NewStringObj("No Argument given...", -1));
      return TCL_ERROR;
   }
   if (objv[1]->typePtr == NULL) {
      Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
   } else if (strcmp(objv[1]->typePtr->name,"list")==0) {
      Tcl_SetObjResult(interp, Tcl_NewIntObj(1));
   } else {
      //Tcl_SetObjResult(interp, Tcl_NewStringObj(objv[1]->typePtr->name, -1));
      Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
   }
   return TCL_OK;
  }
  /*
  * Islist_Init --
  *
  *    Called when Tcl [load]'s your extension.
  */
 int
 Islist_Init(Tcl_Interp *interp)
 {
   if (Tcl_InitStubs(interp, TCL_VERSION, 0) == 0L) {
      return TCL_ERROR;
   }
   Tcl_CreateObjCommand(interp, "Islist", Islist_Cmd, NULL, NULL);
   Tcl_PkgProvide(interp, "Islist", "1.0");
   return TCL_OK;
 }

to compile like this

  gcc -shared -DUSE_TCL_STUBS islist.c -o islist.so -ltclstub8.4

and then use like this

 % load ./islist.so
 % Islist [list 1 2 3]
 1
 % Islist "this is a string"
 0
 % Islist [split "this is a list because we split it"]
 1

This also shows how TCL is switching the internal representation of variables

 % set a [list 3] 
 3
 % Islist $a 
 1
 % incr a
 4
 % Islist $a
 0

First its a list, then it gets (internally) converted to a integer (i think).