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

  1. /* jdbc_client --
  2. Illustration of the use of C for interacting with the
  3. FEWS JDBC server (well, actually we will be working with the
  4. Fews PI service)
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <tcl.h>
  10.  
  11. /* Define some helper variables and functions */
  12.  
  13. Tcl_Interp *interp;
  14.  
  15. void TearDown () {Tcl_Finalize();}
  16.  
  17. int EvalFile (char *fileName) {
  18. return Tcl_EvalFile(interp, fileName);
  19. }
  20.  
  21. int Init (char *argv0) {
  22. char *pchar;
  23. char buffer[1000];
  24.  
  25. /* Initialise the library itself */
  26.  
  27. Tcl_FindExecutable(argv0);
  28. interp = Tcl_CreateInterp();
  29. if (Tcl_Init(interp) != TCL_OK) {
  30. return TCL_ERROR;
  31. }
  32.  
  33. /* Initialise the FEWS PI services */
  34.  
  35. strcpy( buffer, argv0 );
  36. pchar = strrchr( buffer, '/' );
  37. if ( pchar == NULL ) {
  38. pchar = strrchr( buffer, '\\' );
  39. }
  40. if ( pchar == NULL ) {
  41. pchar = buffer;
  42. }
  43. strcpy( buffer, "fewspi.tcl" );
  44.  
  45. return EvalFile( buffer );
  46. }
  47.  
  48. /* Fews PI services: wrapper functions */
  49.  
  50. char *getLocations( char *clientId ) {
  51. char buffer[1000];
  52.  
  53. strcpy( buffer, "getLocations " );
  54. strcat( buffer, clientId );
  55.  
  56. if ( Tcl_Eval( interp, buffer ) != TCL_OK )
  57. {
  58. return NULL;
  59. } else {
  60. return Tcl_GetStringResult( interp ) ;
  61. }
  62. }
  63.  
  64. char *createTask( char *clientId ) {
  65. Tcl_Obj *obj[2];
  66.  
  67. obj[0] = Tcl_NewStringObj( "createTask", -1 );
  68. obj[1] = Tcl_NewStringObj( clientId, -1 );
  69.  
  70. Tcl_IncrRefCount( obj[0] ) ;
  71. Tcl_IncrRefCount( obj[1] ) ;
  72.  
  73. if ( Tcl_EvalObjv( interp, 2, obj, 0 ) != TCL_OK ) {
  74. return NULL;
  75. } else {
  76.  
  77. Tcl_DecrRefCount( obj[0] ) ;
  78. Tcl_DecrRefCount( obj[1] ) ;
  79. return Tcl_GetStringResult( interp ) ;
  80. }
  81. }
  82.  
  83. int main( int argc, char *argv[] ) {
  84.  
  85. /* Get the library started */
  86.  
  87. if ( Init( argv[0] ) != TCL_OK ) {
  88. fprintf( stderr, "Sorry, initialisation failed: %s\n",
  89. Tcl_GetStringResult( interp ) );
  90. }
  91.  
  92. /* Get the locations */
  93. /* fprintf( stdout, "Locations: %s\n", getLocations( "aa" ) ); */
  94. fprintf( stdout, "New task: %s\n", createTask( "aa" ) );
  95. }