home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / glview.sit / glview.src / glview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  5.9 KB  |  288 lines

  1. /*-
  2.  * glview.c - main routines for grasp viewer.
  3.  *
  4.  * Macintosh version of GL Viewer, compiler: MPW-C 3
  5.  *
  6.  * from XGRASP, Copyright (c) 1991 by Patrick J. Naughton
  7.  *
  8.  */
  9.  
  10. #pragma segment GLView
  11.  
  12. #define DEBUG 1
  13. #define Version "1.0, 15-Oct-91"
  14.  
  15. #include "grasp.h"
  16.  
  17.  
  18. char       *pname;
  19. int         imageloop = 0;
  20. int         showdirectory = 0;
  21. int         showtext = 0;
  22. int         printthecodes = 0;
  23. int         verbose = 0;
  24. int         imverbose = 0;
  25. Display    *dsp;
  26. Window      win;
  27. Visual     *vis;
  28. XVisualInfo vinfo;
  29. int         screen;
  30. int         planes;
  31. GC          gc;
  32. GC          gc1;
  33. long        white;
  34. long        black;
  35. //Atom        protocol_atom;
  36. //Atom        kill_atom;
  37.  
  38. Bool        aboutdone = False;
  39.  
  40.  
  41. void about()
  42. {
  43.     fprintf(stderr, "\n%s, version %s\n\n",pname,Version);
  44.     fprintf(stderr,
  45. "This program tries to recreate the functionality provided by the PC\n"
  46. "program GRASPRT.EXE.  It plays animation files which usually have the\n"
  47. "extension .GL.  This file format is partially described by the\n"
  48. "documents found in the file 'docs'.  It has many missing features, but\n"
  49. "it is complete enough to view a large percentage of the GL files.\n\n");
  50.     fprintf(stderr,
  51. "This Mac version was hacked from the X-Window source code, XGRASP (v1.7),\n"
  52. "available at anonymous ftp sites or e-mail to xgrasp@ankh.ftl.fl.us,\n"
  53. "and copyright (c) 1991 by Patrick J. Naughton.\n\n");
  54.  
  55.     fprintf(stderr, "Command line interface: file.gl -debugopts\n");
  56.     fprintf(stderr, "usage: file.gl -all -verbose -dir -textout -images -imverbose -printcodes\n");
  57.     aboutdone = True;
  58. }
  59.  
  60.  
  61. char       *
  62. strdup(s)
  63.     char       *s;
  64. {
  65.     char       *new = malloc(strlen(s) + 1);
  66.     return (new ? strcpy(new, s) : (char *) 0);
  67. }
  68.  
  69. void
  70. error(s1, s2)
  71.     char       *s1, *s2;
  72. {
  73.     if (!aboutdone) about();
  74.  
  75.     fprintf(stderr, s1, pname, s2);
  76.     exit(1);
  77. }
  78.  
  79. void
  80. outi(s, i)
  81.     char       *s;
  82.     int         i;
  83. {
  84.     fprintf(stderr, "%s: %d\n", s, i);
  85. }
  86.  
  87. void
  88. outs(s1, s2)
  89.     char       *s1, *s2;
  90. {
  91.     fprintf(stderr, "%s: %s\n", s1, s2);
  92. }
  93.  
  94.  
  95. u_int
  96. GetByte(fp)
  97.     FILE       *fp;
  98. {
  99.     return (u_int) getc(fp);
  100. }
  101.  
  102. u_int
  103. GetWord(fp)
  104.     FILE       *fp;
  105. {
  106.     u_char      b1 = (u_char) getc(fp);
  107.     u_char      b2 = (u_char) getc(fp);
  108.  
  109.     return (u_int) (b1 + b2 * 256);
  110. }
  111.  
  112. u_int
  113. GetLong(fp)
  114.     FILE       *fp;
  115. {
  116.     u_char      b1 = (u_char) getc(fp);
  117.     u_char      b2 = (u_char) getc(fp);
  118.     u_char      b3 = (u_char) getc(fp);
  119.     u_char      b4 = (u_char) getc(fp);
  120.     return (u_int) (b1 + b2 * 256 + b3 * 256 * 256 + b4 * 256 * 256 * 256);
  121. }
  122.  
  123.  
  124. FilenameStruct *
  125. readdirectory(fp, count)
  126.     FILE       *fp;
  127.     int        *count;
  128. {
  129.     FilenameStruct *fn;
  130.     int         i;
  131.     int         len = GetWord(fp);
  132.     long        plen;
  133.     
  134.     *count = (len / 17) - 1;
  135.     fn = (FilenameStruct *) malloc(*count * sizeof(FilenameStruct));
  136.     for (i = 0; i < *count; i++) {
  137.         fn[i].offset = GetLong(fp);
  138.         fread(fn[i].fname, 13, 1, fp);
  139.         fn[i].fname[13] = 0;
  140.         lowerstr(fn[i].fname);
  141.         }
  142.     if (showdirectory) {
  143.       fprintf(stderr, "%13s   %6s  %6s\n", "Name", "offset", "hexoff" );
  144.       for (i = 0; i < *count; i++) {
  145.         /* if (i+1 == *count) plen= -1 else plen= fn[i+1].offset - fn[i].offset; */
  146.         fprintf(stderr, "%13s   %6d   %6X\n", fn[i].fname, fn[i].offset, fn[i].offset);
  147.         }
  148.       }
  149.     return (fn);
  150. }
  151.  
  152. usage()
  153. {
  154.     error("Error: Bad usage.\n", NULL);
  155. }
  156.  
  157.  
  158. #ifdef DEBUG 
  159. /* command line reader for Mac/MPW  -- dgg */
  160. int readCmdOptions(FILE *cl, char ***argv)
  161. {
  162. #define MAXS    255
  163. #define    addarg(sptr)  if (strlen(sptr)>0) {    \
  164.     targv = (char **) realloc( targv, (argc+1) * sizeof(char *)); \
  165.     targv[argc] = (char *) malloc(1+strlen(sptr) * sizeof(char));    \
  166.     strcpy( targv[argc], sptr);  \
  167.     argc++; }
  168.     
  169.     char    *pword, st[MAXS];
  170.     const char    *progname = "glview";
  171.     
  172.     int     argc = 0;
  173.     char    **targv;
  174.     
  175.     targv = NULL;
  176.     addarg( progname);
  177.     fgets( st, MAXS, cl);
  178.     if (!feof(cl) && st!=NULL && *st!=0) {
  179.         pword = strtok( st, "\ \n");
  180.         while (pword!=NULL) {
  181.             addarg( pword);
  182.             pword = strtok( NULL, "\ \n");
  183.             }
  184.         }
  185.             
  186.     *argv = targv;
  187.     return argc;
  188. }
  189.  
  190. int ccommand(char ***argv)
  191. {
  192.     int argc;
  193.     char    **targv;
  194.     
  195.     argc = readCmdOptions(stdin, &targv);
  196.     *argv = targv;
  197.     return argc;
  198. }
  199.  
  200. #endif DEBUG
  201.  
  202.  
  203.  
  204. real_main(argc, argv)
  205.     int         argc;
  206.     char       *argv[];
  207. {
  208.     char       *displayName = "GL View";
  209.     char       *filename = 0;
  210.     FILE       *fp;
  211.     FilenameStruct *dir;
  212.     int         count;
  213.     int         i;
  214.     OSType        SFTypes[4];
  215.     
  216.     //mac
  217.     SFTypes[0]='TEXT';
  218.     SFTypes[1]='BINA';
  219.     SFTypes[2]='GlVw';
  220.     SFTypes[3]='????';
  221.     filename = StdGetFile( "\pChoose GL File:", SFTypes, 4);
  222.  
  223.  if (!filename) {
  224. #ifdef DEBUG 
  225.     pname = argv[0];
  226.     
  227.     about();
  228.     fprintf(stderr, "command> ");
  229.     argc= ccommand(&argv);
  230.  
  231.     pname = argv[0];
  232.     
  233.     for (i = 1; i < argc; i++) {
  234.     char       *s = argv[i];
  235.     int         n = strlen(s);
  236.  
  237.     if (!strncmp("-dir", s, n))
  238.         showdirectory = 1;
  239.     else if (!strncmp("-textout", s, n))
  240.         showtext = 1;
  241.     else if (!strncmp("-images", s, n))
  242.         imageloop = 1;
  243.     else if (!strncmp("-verbose", s, n))
  244.         verbose = 1;
  245.     else if (!strncmp("-imverbose", s, n))
  246.         imverbose = 1;
  247.     else if (!strncmp("-printcodes", s, n))
  248.         printthecodes = 1;
  249.     else if (!strncmp("-all", s, n)) {
  250.         showdirectory = 1;
  251.         showtext = 1;
  252.         imageloop = 1;
  253.         verbose = 1;
  254.         imverbose = 1;
  255.         printthecodes = 1;
  256.         }
  257.     else if (filename || s[0] == '-')
  258.         usage();
  259.     else
  260.         filename = s;
  261.     }
  262.     
  263.     if (!filename) error("%s: No gl file selected.\n");
  264.     
  265. #else
  266.     error("%s: No gl file selected.\n");
  267. #endif
  268. }
  269.  
  270.     displayName = filename;
  271.     if (!(dsp = XOpenDisplay(displayName)))
  272.          error("%s: unable to open display.\n");
  273.     
  274.     fp = fopen(filename, "r");
  275.     if (!fp) error("%s: %s not found.\n", filename);
  276.     
  277.     if (verbose) fprintf(stderr, "\n readdirectory...\n");
  278.     dir = readdirectory(fp, &count);
  279.     
  280.     if (verbose) fprintf(stderr, "\n readfiles...\n");
  281.     readfiles(fp, dir, count);
  282.     fclose(fp);
  283.     
  284.     if (verbose) fprintf(stderr, "\n execfile...\n");
  285.     execfile(exec[0], 0);
  286.     exit(0);
  287. }
  288.