xgplib.c

Go to the documentation of this file.
00001 /*
00002 %M This module contains the functions to interface the numerical program 
00003    with the graphic program.
00004 %d January 12sd, 1999.
00005 %a Joao Luiz Elias Campos.
00006 %r $Id: xgplib.c,v 1.1 2004/06/22 05:29:59 joaoluiz Exp $
00007 %w (C) COPYRIGHT 1999, Joao Luiz Elias Campos.
00008    All Rights Reserved
00009    Duplication of this program or any part thereof without the express
00010    written consent of the author is prohibited.
00011 *
00012 *  Modified:  23-Fev-06  Juan Pablo Ibaņez
00013 *    Modificada a funcão _XGPStartGraphic, onde trocou-se o comando exit 
00014 *    pelo comando return.
00015 */
00016 
00017 /*
00018 ** ------------------------------------------------------------------------
00019 ** Global variables and symbols:
00020 */
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #ifdef WIN32
00025 # include <io.h>
00026 # include <fcntl.h>
00027 # include <process.h>
00028 #else
00029 # include <unistd.h>
00030 #endif
00031 
00032 
00035 int    GpType       = 0;
00036 int    NumMaxPoints = 0;
00037 int    NumCurves    = 0;
00038 char **Labels       = 0L;
00042 /*
00043 ** ------------------------------------------------------------------------
00044 ** Local variables and symbols:
00045 */
00046 
00049 static int fd[2];
00050 
00053 #ifdef WIN32
00054 # define PIPE(_)   _pipe(_,1024,O_BINARY)
00055 # define EXECLP(_) spawnl(P_NOWAIT,_,#_,"relax",fdout,0L)
00056 #else
00057 # define PIPE(_)   pipe(_)
00058 # define EXECLP(_) execlp(_,#_,"relax",fdout,0L)
00059 #endif
00060 
00063 /*
00064 ** ------------------------------------------------------------------------
00065 ** Local functions:
00066 */
00067 static int  _XGPStartGraphic();
00068 static void _XGPSendCommand ( char * );
00069 static void _XGPSendInteger ( int );
00070 static void _XGPSendLabel   ( char * );
00071 
00074 static int _XGPStartGraphic()
00075 {
00076  int   pid;
00077  char  fdout[20], xgraph[256];
00078  char *tectos_dir = 0L;
00079  char *tec_uname = 0L;
00080 
00081  if( PIPE(fd) == 1 ) {
00082   printf( "\n\nError on trying to open a pipe.\n\n" );
00083   return 0;
00084  }
00085 
00086  sprintf( fdout, "%d", fd[0] );
00087  if( (tectos_dir = getenv( "TECTOSDIR" )) == 0L ) {
00088   printf( "\n\tThe environment variable TECTOSDIR does not exist.\n\n" );
00089   return 0;
00090  }
00091 #ifdef WIN32
00092  if( (tec_uname = getenv( "TEC_SYSNAME" )) == 0L ) {
00093   printf( "\n\tThe environment variable TEC_SYSNAME does not exist.\n\n" );
00094   return 0;
00095  }
00096  sprintf( xgraph, "%s\\bin\\%s\\xgraph.exe", tectos_dir, tec_uname );
00097  if( (pid = (int)EXECLP(xgraph)) == -1 ) {
00098   printf( "\n\nError on execute xgraph program.\n\n" );
00099   return 0;
00100  }
00101 #else
00102  if( (tec_uname = getenv( "TEC_UNAME" )) == 0L ) {
00103   printf( "\n\tThe environment variable TEC_UNAME does not exist.\n\n" );
00104   return 0;
00105  }
00106  sprintf( xgraph, "%s/bin/%s/xgraph", tectos_dir, tec_uname );
00107  if( (pid = fork()) == -1 ) {
00108   printf( "\n\nError on trying to fork a child process.\n\n" );
00109   return 0;
00110  }
00111  else if( pid == 0 ) {
00112   if( EXECLP(xgraph) ==  -1 ) {
00113    printf( "\n\nError on execute xgraph program.\n\n" );
00114    return 0;
00115   }
00116   return 0;
00117  }
00118 #endif
00119  return 1;
00120 
00121 } /* End of _XGPStartGraphic */
00122 
00125 static void _XGPSendCommand( char *command )
00126 {
00127  write( fd[1], command, sizeof(command) );
00128 
00129 } /* End of XGPSendCommand */
00130 
00131 
00134 static void _XGPSendInteger( int number )
00135 {
00136  write( fd[1], &number, sizeof(int) );
00137 
00138 } /* End of XGPSendInteger */
00139 
00140 
00143 static void _XGPSendLabel( char *string )
00144 {
00145  int len = (int)strlen( string );
00146 
00147  write( fd[1], &len, sizeof(int) );
00148  write( fd[1], string, len );
00149 
00150 } /* End of XGPSendString */
00151 
00152 
00153 /*
00154 ** ------------------------------------------------------------------------
00155 ** Public functions:
00156 */
00157 void XGPBegin    ( void );
00158 void XGPEnd      ( void );
00159 void XGPSendPoint( int, double, double );
00160 
00161 
00162 /*
00163 %F
00164 */
00165 void XGPBegin( void );
00166 void XGPBegin( void )
00167 {
00168  int i;
00169 
00170  if( (NumCurves == 0) || !_XGPStartGraphic() )
00171   return;
00172  
00173 /* Create graphic
00174  */
00175  _XGPSendCommand( "%l" );
00176  _XGPSendInteger( NumCurves );
00177  for( i = 0; i < NumCurves; i++ )
00178   _XGPSendLabel( Labels[i] );
00179  _XGPSendCommand( "%c" );
00180  _XGPSendInteger( GpType );
00181  _XGPSendInteger( NumMaxPoints );
00182  _XGPSendInteger( NumCurves );
00183 
00184 } /* End of XGPBegin */
00185 
00186 
00187 /*
00188 %F
00189 */
00190 void XGPEnd( void );
00191 void XGPEnd( void )
00192 {
00193 
00194  if( NumCurves == 0 )
00195   return;
00196  
00197  write( fd[1], "%e", 4 );
00198 
00199 } /* End of XGPEnd */
00200 
00201 
00202 /*
00203 %F
00204 */
00205 void XGPSendPoint( int, double, double );
00206 void XGPSendPoint( int curve, double x, double y )
00207 {
00208 
00209  write( fd[1], "%i", 4 );
00210  write( fd[1], &curve, sizeof(int) );
00211  write( fd[1], &x, sizeof(double) );
00212  write( fd[1], &y, sizeof(double) );
00213 
00214 } /* End of XGPSendPoint */
00215 
00216 
00217 /* =======================================================  End of File  */
00218 

Generated on Tue Oct 23 11:23:30 2007 for Relax by  doxygen 1.5.3