home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / EMAIN.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  4KB  |  135 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. //************************************************************//
  7. //                                                            //
  8. //     F I L E :    E M A I N . C                             //
  9. //                                                            //
  10. //         Sample driver code for geometric shapes example    //
  11. //                                                            //
  12. //************************************************************//
  13. #include <a.out.h>
  14. #include <fcntl.h>
  15. #include "eshape.h"
  16. #ifndef _COORDINATE_H
  17. #include "ecoord.h"
  18. #endif
  19. extern void doClassUpdate();
  20. extern int compile(const String &fileName);
  21. extern int mkfile(const String &fileName,
  22.                  const String &contents);
  23.  
  24. int main2() {
  25.     Shape::init();
  26.     Coordinate p1, p2, p3;
  27.     Shape object = (*shape)->make(p1, p2, p3);
  28.     printf("object is "); object->draw(); printf("\n");
  29.  
  30.     // demonstrate virtual function update
  31.     object->move(p1);
  32.     compile("ev2tri.c");
  33.     String include = "includes.h";
  34.     mkfile(include,
  35.     "#include \"ek.h\"\n#include \"ev2tri.h\"\n");
  36.     object->update("v2Triangle.o", "Triangle::move");
  37.     object->move(p1);
  38.     doClassUpdate();
  39.     object->move(p1);
  40.     {
  41.         Shape object3 = (*shape)->make(p1, p2, p3);
  42.         printf("object3 is "); object3->draw(); printf("\n");
  43.     }
  44. printf("main: making object2\n");
  45.     Shape object2 = (*shape)->make(p1, p2, p3);
  46.     shape->gc();        // do a gc now and then
  47.     printf("object2 is "); object2->draw(); printf("\n");
  48. printf("main: made object2, calling object2->move\n");
  49.     object2->move(p1);
  50.     shape->gc();        // do a gc now and then
  51. printf("exiting\n");
  52.     return 0;
  53. }
  54.  
  55. int main() {
  56.     int retval = main2();
  57.     shape->gc();
  58.     return retval;
  59. }
  60.  
  61. void
  62. doClassUpdate() {
  63.     extern Shape *triangle;
  64.     const String include = "includes.h";
  65.     mkfile(include,
  66.     "#include \"ek.h\"\n#include \"ev2tri.h\"\n");
  67.  
  68.     compile("ev3tria.c");
  69.     (*triangle).update("ev3tria.o", "Triangle::make",
  70.         "Shape (Triangle::*TYPE)()");
  71.  
  72.     compile("ev3trib.c");
  73.     (*triangle).update("ev3trib.o", "Triangle::make",
  74.         "Shape (Triangle::*TYPE)\
  75.             (Coordinate,Coordinate,Coordinate)");
  76.  
  77.     compile("ev3trim.c");
  78.     (*triangle).update("ev3trim.o", "Triangle::move");
  79.  
  80.     compile("v3tric.c");
  81.     (*triangle).update("v3tric.o",
  82.                     "Triangle::cutover");
  83.  
  84.     mkfile(include, "#include \"ek.h\"\n\
  85.        #include \"ev3tri.h\"\n");
  86.  
  87.     mkfile("ev3doit.c",  "#include \"ek.h\"\n\
  88.        #include \"ev3tri.h\"\n\
  89.         Top * Shape::doit() {\n\
  90.             printf(\"v3 Shape::doit (new) called\\n\");\n\
  91.             Thingp Ttriangle = triangle;\n\
  92.             shape->dataUpdate(Ttriangle,\n\
  93.                 new Triangle(Exemplar(0)));\n\
  94.             triangle = (ShapeRep*) Ttriangle;\n\
  95.             printf(\"Shape::doit: did data update\\n\");\
  96.             return 0;\n\
  97.         }\n\n\
  98.         Triangle::Triangle(Exemplar e): ShapeRep(e) { }\n");
  99.     compile("ev3doit.c");
  100.     printf("doClassUpdate:\
  101.         calling shape->update(\"ev3doit.o\",\
  102.         \"Shape::doit\")\n");
  103.     shape->update("ev3doit.o", "Shape::doit");
  104.     shape->doit();
  105.     shape->gc();        // do a gc now and then
  106.     unlink("ev3doit.c");
  107.     unlink("ev3doit.o");
  108. }
  109.  
  110. #include <sys/stat.h>
  111.  
  112. int compile(const String& fileName) {
  113.     struct stat dotC, dotO;
  114.     String fileNameDotO =
  115.         fileName(0,fileName.length()-2) + ".o";
  116.     stat(fileName, &dotC);
  117.     stat(fileNameDotO, &dotO);
  118.     if (dotC.st_mtime < dotO.st_mtime) {
  119.         printf("\"%s\" is up to date\n", (const char*)fileName);
  120.         return 0;
  121.     } else {
  122.         String command = String("CC +e0 -c -g ") + fileName;
  123.         printf("compile: <%s>\n", (const char*)command);
  124.         return system(command);
  125.     }
  126. }
  127.  
  128. extern int mkfile(const String &fileName,
  129.                     const String &contents) {
  130.     FILE *inc = fopen(fileName, "w");
  131.     printf("mkfile: creating <%s>\n", (const char *)fileName);
  132.     fprintf(inc, (const char*)contents);
  133.     return fclose(inc);
  134. }
  135.