home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / main.c < prev    next >
C/C++ Source or Header  |  1992-11-03  |  3KB  |  130 lines

  1. /*
  2.  * main.C
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stream.h>
  20. #include <stdlib.h>
  21. #include <new.h>
  22.  
  23. #include "Options.h"
  24. #include "LSystem.h"
  25. #include "Interpreter.h"
  26. #include "Color.h"
  27. #include "BBoxDevice.h"
  28. #include "ExampleDevice.h"
  29. #include "RayshadeDevice.h"
  30. #include "FlatDevice.h"
  31.  
  32. #ifdef SUPPORT_X11
  33. # include "LineDevice.h"
  34. # include "WireDevice.h"
  35. # include "X11_Window.h"
  36. #endif
  37.  
  38. extern int yyparse(void);
  39. extern LSystem* lsystem;
  40. extern FILE* yyin;      
  41.  
  42. void memory_error_handler();
  43.  
  44. int main(int argc, char** argv)
  45. {
  46. #if !defined(__GNUC__) && !defined (_AIX) && !defined(__DECCXX)
  47.   mallopt(M_MXFAST, 32);
  48. #endif
  49.  
  50.   set_new_handler(memory_error_handler);
  51.  
  52.   /*
  53.    * Parse the command line options to get name of input file.
  54.    */
  55.   parseOptions(argc, argv);
  56.   Color::setupColors();
  57.  
  58.   rcString cmd(CPP_NAME);
  59.  
  60.   if (theOptions.iname != "cin") 
  61.     cmd = cmd + " " + theOptions.iname + theOptions.optionsForCPP;
  62.   else
  63.     cmd = cmd + " " + theOptions.optionsForCPP;
  64.  
  65.   if ((yyin = popen(cmd.chars(), "r")) == NULL)
  66.     Error(ERR_PANIC, theOptions.iname +  ": no such file");
  67.  
  68.   if (!yyparse()) {
  69.     /*
  70.      * Parse a second time, because command line options overrides
  71.      * the settings in lsys-file.
  72.      */
  73.     parseOptions(argc, argv);
  74.  
  75.     if (theOptions.printlsys)
  76.       cerr << *lsystem << "\n";
  77.  
  78.     ModuleList* result = lsystem->execute();
  79.     if (theOptions.printModules) {
  80.       cerr << '\n';
  81.       for (register long i=0; i<result->count(); i++) 
  82.     cerr << *result->item(i) << "\n"; 
  83.     }
  84.  
  85.     /*
  86.      * Select output driver and start turtle interpretation.
  87.      */
  88.     DeviceDriver* d;
  89.     if (theOptions.drivername == "no")
  90.       return 1;
  91.     else if (theOptions.drivername == "example")
  92.       d = new ExampleDevice;
  93.     else if (theOptions.drivername == "bbox")
  94.       d = new BBoxDevice(&theOptions);
  95.     else if (theOptions.drivername == "rayshade")
  96.       d = new RayshadeDevice(&theOptions);
  97.     else if (theOptions.drivername == "flat")
  98.       d = new FlatDevice(&theOptions);
  99. #ifdef SUPPORT_X11
  100.     else if (theOptions.drivername == "x11simple")
  101.       d = new LineDevice(new X11_Window, &theOptions);
  102.     else if (theOptions.drivername == "x11wire")
  103.       d = new WireDevice(new X11_Window, &theOptions);
  104. #endif
  105.     else {
  106.       if (!theOptions.quiet)
  107. #ifdef SUPPORT_X11
  108.     cerr << "\nUnknown driver specified, using simple X11.\n\n";
  109.       d = new LineDevice(new X11_Window, &theOptions);
  110. #else
  111.     cerr << "\nUnknown driver specified, using ExampleDevice.\n\n";
  112.       d = new ExampleDevice;
  113. #endif
  114.     }
  115.  
  116.     d->setName(lsystem->getName());
  117.     Interpreter interp(d, &theOptions);
  118.     interp.interpret(result);
  119.     // delete d;  let the system do the clean up.
  120.   }
  121.  
  122.   return 1;
  123. }
  124.  
  125. void memory_error_handler()
  126. {
  127.   cerr << "Fatal error: out of memory\n";
  128.   exit(1);
  129. }
  130.