Posted to tcl by torsten at Thu Sep 21 11:03:56 GMT 2006view pretty

/*

save this to hello.c and
compile this like so ---> gcc -shared -DUSE_TCL_STUBS hello.c -o libhello.so -ltclstub8.4
and run this script:

load ./libhello[info sharedlibextension]
puts "1. [shp shape ./hello.c]"
puts "2. [shape file]"

It should report something like "./hello.c" but it reports "serial1"

What's wrong here?? I can't find the bug :-(


*/


#include <tcl.h>

typedef struct ShapeFile {
	char *fileName;     /* the name of the shape file */
	Tcl_Channel chan;   /* the channel to the openend shape file */
} ShapeFile;

static int
ShpObjCmd(clientData, interp, objc, objv)
	ClientData clientData;
	Tcl_Interp *interp;
	int objc;
	Tcl_Obj * CONST objv[];
{
	ShapeFile *shp = (ShapeFile*)clientData;
	Tcl_SetObjResult(interp, Tcl_NewStringObj(shp->fileName,-1));
	return TCL_OK;
}

static int
ShpShpCmd(cdata, interp, objc, objv)
	ClientData cdata;
	Tcl_Interp *interp;
	int objc;
	Tcl_Obj *CONST objv[];
{
	const char *cmdName;
	ShapeFile *shp;
	shp = (ShapeFile*)Tcl_Alloc(sizeof(*shp));
	shp->chan = NULL;
	shp->fileName = NULL;
	cmdName = Tcl_GetStringFromObj(objv[1], 0);
	shp->fileName = Tcl_GetString(objv[2]);
	Tcl_CreateObjCommand(interp, cmdName, ShpObjCmd, shp, NULL);
	return TCL_OK;
}

int
Hello_Init(Tcl_Interp *interp)
{
	/* we want to use stubs */
	if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
		return TCL_ERROR;
	}
	Tcl_CreateObjCommand(interp, "shp", ShpShpCmd, NULL, NULL);
	Tcl_PkgProvide(interp, "Hello", "1.0");
	return TCL_OK;
}

Comments

Posted by mjanssen at Thu Sep 21 11:47:54 GMT 2006 [text] [code]

#include <tcl.h> typedef struct ShapeFile { Tcl_Obj * filename; /* the name of the shape filem */ Tcl_Channel chan; /* the channel to the openend shape file */ } ShapeFile; static int ShpObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]) { ShapeFile *shp = (ShapeFile*)clientData; Tcl_SetObjResult(interp, shp->filename); return TCL_OK; } static int ShpShpCmd ( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { char *cmdName; ShapeFile *shp; shp = (ShapeFile*)Tcl_Alloc(sizeof(*shp)); shp->chan = NULL; shp->filename = NULL; cmdName = Tcl_GetStringFromObj(objv[1], 0); shp->filename = Tcl_DuplicateObj(objv[2]); Tcl_CreateObjCommand(interp, cmdName, ShpObjCmd, shp, NULL); return TCL_OK; } __declspec(dllexport) int Hello_Init(Tcl_Interp *interp) { /* we want to use stubs */ if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) { return TCL_ERROR; } Tcl_CreateObjCommand(interp, "shp", ShpShpCmd, NULL, NULL); Tcl_PkgProvide(interp, "Hello", "1.0"); return TCL_OK; }