Posted to tcl by arjen at Thu Aug 06 12:35:28 GMT 2009view pretty

/* jdbc_client --
       Illustration of the use of C for interacting with the
       FEWS JDBC server (well, actually we will be working with the
       Fews PI service)

*/

#include <stdio.h>
#include <tcl.h>

/* Define some helper variables and functions */

Tcl_Interp *interp;

void TearDown () {Tcl_Finalize();}

int EvalFile (char *fileName) {
    return Tcl_EvalFile(interp, fileName);
}

int Init (char *argv0) {
    char *pchar;
    char  buffer[1000];

    /* Initialise the library itself */

    Tcl_FindExecutable(argv0);
    interp = Tcl_CreateInterp();
    if (Tcl_Init(interp) != TCL_OK) {
        return TCL_ERROR;
    }

    /* Initialise the FEWS PI services */

    strcpy( buffer, argv0 );
    pchar = strrchr( buffer, '/' );
    if ( pchar == NULL ) {
        pchar = strrchr( buffer, '\\' );
    }
    if ( pchar == NULL ) {
        pchar = buffer;
    }
    strcpy( buffer, "fewspi.tcl" );

    return EvalFile( buffer );
}

/* Fews PI services: wrapper functions */

char *getLocations( char *clientId ) {
    char buffer[1000];

    strcpy( buffer, "getLocations " );
    strcat( buffer, clientId );

    if ( Tcl_Eval( interp, buffer ) != TCL_OK )
{
        return NULL;
    } else {
        return Tcl_GetStringResult( interp ) ;
    }
}

char *createTask( char *clientId ) {
    Tcl_Obj *obj[2];

    obj[0] = Tcl_NewStringObj( "createTask", -1 );
    obj[1] = Tcl_NewStringObj( clientId, -1 );

    Tcl_IncrRefCount( obj[0] ) ;
    Tcl_IncrRefCount( obj[1] ) ;

    if ( Tcl_EvalObjv( interp, 2, obj, 0 ) != TCL_OK ) {
        return NULL;
    } else {

        Tcl_DecrRefCount( obj[0] ) ;
        Tcl_DecrRefCount( obj[1] ) ;
        return Tcl_GetStringResult( interp ) ;
    }
}

int main( int argc, char *argv[] ) {

    /* Get the library started */

    if ( Init( argv[0] ) != TCL_OK ) {
        fprintf( stderr, "Sorry, initialisation failed: %s\n",
            Tcl_GetStringResult( interp ) );
    }

    /* Get the locations */
/*    fprintf( stdout, "Locations: %s\n", getLocations( "aa" ) ); */
    fprintf( stdout, "New task: %s\n", createTask( "aa" ) );
}