home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 02 Berger / sinterp / sinterp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  904 b   |  40 lines

  1. #include "VM.H"
  2.  
  3.  
  4. int main( int argc, char **argv )
  5. {
  6.   // The usage to this program is very simple:  it requires a single file name
  7.   // which is the bytecode stream to execute.
  8.   if ( argc != 2 ) {
  9.     printf( "usage: %s file", argv[0] );
  10.     return 1;
  11.   }
  12.  
  13.   // Jump through hoops opening the file.  We need to know how big to allocate
  14.   // the buffer, so make sure to seek to the end of the file to get its file
  15.   // size.
  16.   FILE *f = fopen( argv[1], "rb" );
  17.   if ( f == NULL ) {
  18.     perror( argv[1] );
  19.     return 1;
  20.   }
  21.  
  22.   fseek( f, 0, SEEK_END );
  23.   size_t len = ftell( f );
  24.   rewind( f );
  25.  
  26.   char *stream = new char [len];
  27.  
  28.   fread( stream, len, 1, f );
  29.  
  30.   // Now that we finially have the bytecode stream from the file, create an
  31.   // instance of the VM and execute it.
  32.   VM vm( stream, len );
  33.   vm.Exec();
  34.  
  35.   delete [] stream;
  36.   fclose( f );
  37.  
  38.   return 0;
  39. }
  40.