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

  1. /*
  2.  
  3. save this to hello.c and
  4. compile this like so ---> gcc -shared -DUSE_TCL_STUBS hello.c -o libhello.so -ltclstub8.4
  5. and run this script:
  6.  
  7. load ./libhello[info sharedlibextension]
  8. puts "1. [shp shape ./hello.c]"
  9. puts "2. [shape file]"
  10.  
  11. It should report something like "./hello.c" but it reports "serial1"
  12.  
  13. What's wrong here?? I can't find the bug :-(
  14.  
  15.  
  16. */
  17.  
  18.  
  19. #include <tcl.h>
  20.  
  21. typedef struct ShapeFile {
  22. char *fileName; /* the name of the shape file */
  23. Tcl_Channel chan; /* the channel to the openend shape file */
  24. } ShapeFile;
  25.  
  26. static int
  27. ShpObjCmd(clientData, interp, objc, objv)
  28. ClientData clientData;
  29. Tcl_Interp *interp;
  30. int objc;
  31. Tcl_Obj * CONST objv[];
  32. {
  33. ShapeFile *shp = (ShapeFile*)clientData;
  34. Tcl_SetObjResult(interp, Tcl_NewStringObj(shp->fileName,-1));
  35. return TCL_OK;
  36. }
  37.  
  38. static int
  39. ShpShpCmd(cdata, interp, objc, objv)
  40. ClientData cdata;
  41. Tcl_Interp *interp;
  42. int objc;
  43. Tcl_Obj *CONST objv[];
  44. {
  45. const char *cmdName;
  46. ShapeFile *shp;
  47. shp = (ShapeFile*)Tcl_Alloc(sizeof(*shp));
  48. shp->chan = NULL;
  49. shp->fileName = NULL;
  50. cmdName = Tcl_GetStringFromObj(objv[1], 0);
  51. shp->fileName = Tcl_GetString(objv[2]);
  52. Tcl_CreateObjCommand(interp, cmdName, ShpObjCmd, shp, NULL);
  53. return TCL_OK;
  54. }
  55.  
  56. int
  57. Hello_Init(Tcl_Interp *interp)
  58. {
  59. /* we want to use stubs */
  60. if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
  61. return TCL_ERROR;
  62. }
  63. Tcl_CreateObjCommand(interp, "shp", ShpShpCmd, NULL, NULL);
  64. Tcl_PkgProvide(interp, "Hello", "1.0");
  65. return TCL_OK;
  66. }

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; }