Here goes: I have a simple C routine that I want to be able to access
as a tcl function. The C source code follows:
#include <stdio.h>
#include <time.h>
#include "dateconv.h"
#include "tcl.h"
int Tcl_Days1900(void *clientData, Tcl_Interp *interp, int argc, char
**argv)
{
struct tm *now;
time_t aclock;
char todaydate[11];
Date dt_today, dt_1900;
char date1900[11];
time( &aclock ); /* Get time in seconds */
now = localtime( &aclock ); /* Convert time to struct tm form */
sprintf(todaydate,"%04d-%02d-%02d",1900+now->tm_year,now->tm_mon+1,now->tm_mday);
strcpy(date1900,"1899-12-31");
sprintf(interp->result,"%ld", iso_date_to_date(todaydate, &dt_today)
- iso_date_to_date(date1900, &dt_1900));
return TCL_OK;
}
I have compiled it into a .o file called "tcldays1900" - In order for
this function to operate, another .o file must be included called
"dateconv.o"
Can someone explain to me in plain english how to go about adding this
function to tcl? I want any tcl program that is being interpreted to be
able to call it as "days1900" - I _do_ have extended tcl, since the
headers of my tcl script files have a line like this: "load libtclx.so"
We are using tcl-scripting for processing inbound files into an edi
switch. I need this function to properly generate a filename for the
outbound file. Any help would be greatly appreciated.
Rich Stephens
ri...@healthfare.com
Hi Rich,
you can
- compile the objects together in one shared library.
You need to add XXX_Init() function to initialize
your extensions i.e. register your new commands.
( see http://www.cs.uoregon.edu/research/tcl/faqs/tk/#dll on how to
build
your own shared extension )
- try to use tcl-package-system.
( I have no experience with this, though )
Lars
RS> OK... I have tried reading all the faqs and pages, and I have yet to be
RS> able to make heads or tails of it.
RS>
RS> Here goes: I have a simple C routine that I want to be able to access
RS> as a tcl function. The C source code follows:
RS>
RS> #include <stdio.h>
RS> #include <time.h>
RS> #include "dateconv.h"
RS> #include "tcl.h"
RS>
RS> int Tcl_Days1900(void *clientData, Tcl_Interp *interp, int argc, char
RS> **argv)
RS> {
RS> struct tm *now;
RS> time_t aclock;
RS> char todaydate[11];
RS> Date dt_today, dt_1900;
RS> char date1900[11];
RS>
RS> time( &aclock ); /* Get time in seconds */
RS> now = localtime( &aclock ); /* Convert time to struct tm form */
RS>
RS> sprintf(todaydate,"%04d-%02d-%02d",1900+now->tm_year,now->tm_mon+1,now->tm_mday);
RS> strcpy(date1900,"1899-12-31");
RS> sprintf(interp->result,"%ld", iso_date_to_date(todaydate, &dt_today)
RS> - iso_date_to_date(date1900, &dt_1900));
RS>
RS> return TCL_OK;
RS> }
RS>
RS> I have compiled it into a .o file called "tcldays1900" - In order for
RS> this function to operate, another .o file must be included called
RS> "dateconv.o"
RS>
RS> Can someone explain to me in plain english how to go about adding this
RS> function to tcl? I want any tcl program that is being interpreted to be
RS> able to call it as "days1900" - I _do_ have extended tcl, since the
RS> headers of my tcl script files have a line like this: "load libtclx.so"
RS>
RS> We are using tcl-scripting for processing inbound files into an edi
RS> switch. I need this function to properly generate a filename for the
RS> outbound file. Any help would be greatly appreciated.
RS>
RS> Rich Stephens
RS> ri...@healthfare.com
RS>
You need a call to Tcl_CreateCommand() like this:
Tcl_CreateCommand(interp,"days1900",Tcl_Days1900,NULL,NULL);
This call must get called at startup time. There are a couple of ways to do
this. The dirt simple way (old way) is to add this line to tclAppInit.c
(including a "extern int Tcl_Days1900(void *clientData, Tcl_Interp *interp,
int argc, char **argv);" line), recompile tclXAppInit.c and re-link tclsh with
this new tclXAppInit.o and your .o file. You now have a custom tcl with a
new command in it. (You can do the same thing with tkXAppInit.c to make a new
wishx.) The other way (Tcl 7.5+) is to create a shared object library with a
function named Days_Init(Tcl_Interp *interp) which has the Tcl_CreateCommand()
call in it, along with your .o file. This file can be loaded (using the
Tcl 7.5 "load" command) into either tcl or wishx.
--
\/
Robert Heller ||InterNet: Hel...@CS.UMass.EDU
http://vis-www.cs.umass.edu/~heller ||FidoNet: 1:321/153
http://netmar.com/mall/shops/heller /\