home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / EXECUTE.C < prev    next >
C/C++ Source or Header  |  1993-08-22  |  6KB  |  196 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         GWSIM - Simulation        */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*          Simulation shell         */
  14. /*             dialog box            */
  15. /*                                   */
  16. /*           QuickC/WIN 1.0          */
  17. /*                                   */
  18. /*   (include here compilers that    */
  19. /*   compiled GWSIM successfully)    */
  20. /*                                   */
  21. /*************************************/
  22.  
  23.  
  24. #include <windows.h>
  25. #include <string.h>
  26. #include <io.h>
  27. #include "toolhelp.h"
  28. #include "globals.h"
  29. #include "defines.h"
  30. #include "strtbl.h"
  31. #include "gwsim.h"
  32. #include "simgvar.h"
  33. #include "basic.h"
  34. #include "gep2.h"
  35.  
  36. #pragma alloc_text( CODE11, Execute, WatchDosApp, CheckSimEng )
  37.  
  38. #define HFILE_ERROR -1
  39.  
  40. /* calls the simulation engine to carry out the current simulation */
  41.  
  42. HANDLE hSimTask;
  43.  
  44. int Execute( HWND hWnd, HANDLE hInst )
  45. {
  46.  int hFile, nRc;
  47.  char szString[256];
  48.  char *ptr;
  49.  
  50.  
  51.  lstrcpy( (LPSTR) CommandLine, (LPSTR) GepasiIni );
  52.  ptr = strrchr( CommandLine, '\\' );
  53.  *(ptr+1) = '\0';
  54.  GetTempFileName( 0, (LPSTR) "GEP", 0, (LPSTR) TempFile );
  55.  hFile = _lopen( (LPSTR) TempFile, OF_WRITE | OF_SHARE_COMPAT );
  56.  nRc = _lwrite( hFile, FileName, lstrlen( FileName ) );
  57.  nRc += _lwrite( hFile, (LPSTR) "\n", 1 );
  58.  _lclose( hFile );
  59.  if( nRc < ( lstrlen( (LPSTR) FileName ) +1 ) )
  60.  {
  61.   LoadString(hInst, IDS_ERR_NO_TEMP, szString, sizeof(szString));
  62.   MessageBeep( MB_OK );
  63.   MessageBox( hWnd, szString, FileTitle, MB_ICONINFORMATION );
  64.   unlink( TempFile );
  65.   return IDS_ERR_NO_TEMP;
  66.  }
  67.  if( options.debug )
  68.  {
  69.   lstrcat( (LPSTR) CommandLine, (LPSTR) "GEPASI_R.BAT " );
  70.   GetModuleFileName( hInst, (LPSTR) szString, sizeof( szString ) );
  71.   ptr = strrchr( szString, '\\' );
  72.   *(ptr+1) = '\0';
  73.   lstrcat( (LPSTR) CommandLine, (LPSTR) szString );
  74.  }
  75.  lstrcat( (LPSTR) CommandLine, (LPSTR) "GEPASI.EXE " );
  76.  lstrcat( (LPSTR) CommandLine, (LPSTR) TempFile );
  77.  lstrcat( (LPSTR) CommandLine, (LPSTR) " -d" );
  78.  if( options.debug )
  79.  {
  80.   lstrcpy( (LPSTR) LogFile, (LPSTR) FileName );
  81.   if ( ptr = strrchr( LogFile, '.' ) ) strcpy( ptr, ".LOG" );
  82.   else strcat( ptr, ".LOG" );
  83.   lstrcat( (LPSTR) CommandLine, (LPSTR) " " );
  84.   lstrcat( (LPSTR) CommandLine, (LPSTR) LogFile );
  85.   MessageBox( hWnd, (LPSTR) CommandLine, FileTitle, MB_ICONINFORMATION );
  86.  }
  87.  nRc = WinExec( (LPSTR) CommandLine, SW_SHOWMINIMIZED );
  88.  if( nRc<32 ) ErrorHandler( hInst, nRc );
  89.  else
  90.  {
  91.   if( ! WatchDosApp( (HANDLE) nRc, fpCheckSimEng ) )
  92.   {
  93.    MessageBeep( MB_OK );
  94.    MessageBox( hWnd, "Unable to create watch callback function.\nPlease report to developer.", (LPSTR) "Execute()", MB_ICONEXCLAMATION );
  95.    return -1;
  96.   }
  97.   EnableMenuItem( hMenu, IDM_GO, MF_GRAYED );
  98.   EnableWindow( hButton2, FALSE );
  99.   DrawMenuBar( hWnd );
  100.   running = 1;
  101.  }
  102.  
  103.  return nRc;
  104. }
  105.  
  106.  
  107. int WatchDosApp( HANDLE hApp, FARPROC fpCheck )
  108. {
  109.  TASKENTRY TaskStruct;
  110.  int nRc;
  111.  
  112.  /* initialize the TASKENTRY structure    */
  113.  TaskStruct.dwSize = (DWORD) sizeof( TASKENTRY );
  114.  
  115.  if ( TaskFirst( &TaskStruct ) )
  116.   do
  117.    if( TaskStruct.hInst == hApp ) break;
  118.   while ( TaskNext( &TaskStruct ) );
  119.  else return 1;
  120.  hSimTask = TaskStruct.hTask;
  121.  nRc = NotifyRegister( NULL , (LPFNNOTIFYCALLBACK) fpCheck, NF_NORMAL );
  122.  return nRc;
  123. }
  124.  
  125. /*
  126.   Callback function that is called when timer goes off.
  127.   Checks if the Simulation Engine is still running. If
  128.   not plots the file and/or lists the report and/or the
  129.   debug log
  130. */
  131.  
  132. BOOL FAR PASCAL CheckSimEng( WORD wID, DWORD dwData)
  133. {
  134.  /* return if task is not exiting            */
  135.  if( wID == NFY_EXITTASK )
  136.  {
  137.   /* post the appropriate message            */
  138.   PostMessage( hWndMain, WM_USER+2, 0, 0 );
  139.   /* signal that notification was processed    */
  140.   return 1;
  141.  }
  142.  else return 0;
  143. }
  144.  
  145. void SimEnd( void )
  146. {
  147.  int nRc;
  148.  char *ptr;
  149.  TASKENTRY TaskStruct;
  150.  
  151.  /* initialize the TASKENTRY structure    */
  152.  TaskStruct.dwSize = (DWORD) sizeof( TASKENTRY );
  153.  /* do nothing if the application that exited is not the simulation engine    */
  154.  if( ! TaskFindHandle( &TaskStruct, hSimTask ) )
  155.  {
  156.   /* unregister the callback procedure                */
  157.   if( ! NotifyUnRegister( NULL ) )
  158.   {
  159.    MessageBeep( MB_OK );
  160.    MessageBox( hWndMain, (LPSTR) "Couldn't NotifyUnRegister hTask.\nPlease report to developer.", (LPSTR) "SimEnd()", MB_ICONEXCLAMATION );
  161.   }
  162.  
  163.   /* enable the Run selections                        */
  164.   EnableMenuItem( hMenu, IDM_GO, MF_ENABLED );
  165.   EnableWindow( hButton2, TRUE );
  166.   DrawMenuBar( hWndMain );
  167.   InvalidateRect( hWndMain, NULL, TRUE );
  168.   running = 0;
  169.  
  170.   /* ask user if he wants to read the debug log        */
  171.   if( options.debug )
  172.   {
  173.    lstrcpy( (LPSTR) CommandLine, (LPSTR) "notepad " );
  174.    lstrcat( (LPSTR) CommandLine, (LPSTR) LogFile );
  175.    nRc = WinExec( (LPSTR) CommandLine, SW_SHOWNORMAL );
  176.    if( nRc<32 ) ErrorHandler( hInst, nRc );
  177.   }
  178.  
  179.   /* ask user if he wants to read the report file    */
  180.   if( options.txt )
  181.   {
  182.    LoadString(hInst, IDS_REPORTQUERY, szString, sizeof(szString));
  183.    MessageBeep( MB_ICONQUESTION );
  184.    nRc = MessageBox(hWndMain, (LPSTR) szString, (LPSTR) FileTitle, MB_ICONQUESTION | MB_YESNO );
  185.    if( nRc == IDYES )
  186.    {
  187.     lstrcpy( (LPSTR) CommandLine, (LPSTR) "notepad " );
  188.     lstrcat( (LPSTR) CommandLine, (LPSTR) FileName );
  189.     if ( ( ptr = strrchr( CommandLine, '.' ) ) != NULL ) strcpy( ptr, ".TXT" );
  190.     else strcat( CommandLine, ".TXT" );
  191.     nRc = WinExec( (LPSTR) CommandLine, SW_SHOWNORMAL );
  192.     if( nRc<32 ) ErrorHandler( hInst, nRc );
  193.    }
  194.   }
  195.  }
  196. }