home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / off / code / readindp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-20  |  5.7 KB  |  249 lines

  1.  
  2. /*
  3.  *
  4.  * Description
  5.  *    Read an indexed_poly data file.
  6.  *
  7.  * Output
  8.  *
  9.  * Input
  10.  *    pProp        Pointer to property structure in which to store data
  11.  *    fname        Full path/file name of file to be read.
  12.  *
  13.  * Diagnostics
  14.  *    Returns 0 if successful, -1 if unsuccessful for any reason.
  15.  *
  16.  * Author
  17.  *    Randi J. Rost
  18.  *    Digital Equipment Corp.
  19.  *    Workstation Systems Engineering
  20.  *    Palo Alto, CA
  21.  *
  22.  * History
  23.  *    17-Nov-86    Created
  24.  *
  25.  */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <sys/file.h>
  30. #include "off.h"
  31.  
  32. #define MAX_DATA_ITEMS        30
  33.  
  34. OFFReadIndexedPoly(pProp, fname)
  35.     OFFProperty    *pProp;
  36.     char    *fname;
  37.  
  38.     {
  39.     FILE    *ascfd;
  40.     int        binfd;
  41.     int        code;
  42.     char    *ptr, *ptr2;
  43.     int        i, j;
  44.     long    npts, npolys, nconnects;
  45.     long    *lptr;
  46.     char    format[MAX_DATA_ITEMS][10];
  47.     int        padding[MAX_DATA_ITEMS];
  48.     int        size[MAX_DATA_ITEMS];
  49.     int        datasize = 0;
  50.     int        type = OFF_BINARY;
  51.     char    ch;
  52.     char    bigstr[OFF_BIGSTR];
  53.     int        nostrings = 1;
  54.     int        strlength;
  55.     int        endpad;
  56.     long    junk;
  57.  
  58.  
  59. /*  Try opening the file as if it were binary first  */
  60.     binfd = open(fname, O_RDONLY, 0);
  61.  
  62. /*  If error opening file, punt  */
  63.     if (binfd < 0)
  64.     {
  65.     fprintf(stderr, "OFFReadIndexedPoly: cannot open data file %s\n",
  66.         fname);
  67.     return(-1);
  68.     }
  69.  
  70. /*  Read first word of file to determine file type  */
  71.     read(binfd, &code, sizeof(long));
  72.  
  73.     if (code != OFF_INDEXED_POLY_MAGIC)
  74.     {
  75.     /*  Close the file  */
  76.     close(binfd);
  77.  
  78.     /*  Try to open it as an ascii data file  */
  79.     ascfd = fopen(fname, "r");
  80.  
  81.     /*  If error opening file, punt  */
  82.     if (ascfd == NULL)
  83.         {
  84.         fprintf(stderr, "OFFReadIndexedPoly: cannot open data file %s\n",
  85.             fname);
  86.         return(-1);
  87.         }
  88.     type = OFF_ASCII;
  89.     }
  90.  
  91. /*  Read in the number of points and polygons for the object  */
  92.     if (type == OFF_ASCII)
  93.     fscanf(ascfd,"%d %d %d\n", &npts, &npolys, &nconnects);
  94.     else
  95.     {
  96.     read(binfd, &npts, sizeof(long));
  97.     read(binfd, &npolys, sizeof(long));
  98.     read(binfd, &nconnects, sizeof(long));
  99.     }
  100.     pProp->PropCount = npts;
  101.  
  102. /*  Compute data size  */
  103.     for (i = 0; i < strlen(pProp->DataFormat); i++)
  104.     {
  105.     switch (pProp->DataFormat[i])
  106.         {
  107.         case 'i': size[i] = sizeof(long);
  108.               padding[i] = ((datasize % 4) == 0) ?
  109.                 0 : 4 - datasize % 4;
  110.               strcpy(format[i], "%d");
  111.               break;
  112.         case 'f': size[i] = sizeof(float);
  113.               padding[i] = ((datasize % 4) == 0) ?
  114.                 0 : 4 - datasize % 4;
  115.               strcpy(format[i], "%f");
  116.               break;
  117.         case 'd': size[i] = sizeof(double);
  118.               padding[i] = ((datasize % 4) == 0) ?
  119.                 0 : 4 - datasize % 4;
  120.               strcpy(format[i], "%F");
  121.               break;
  122.         case 'h': size[i] = sizeof(short);
  123.               padding[i] = ((datasize % 2) == 0) ? 0 : 1;
  124.               strcpy(format[i], "%hd");
  125.               break;
  126.         case 'b': size[i] = sizeof(char);
  127.               padding[i] = 0;
  128.               strcpy(format[i], "%d");
  129.               break;
  130.         case 's': size[i] = sizeof(char *);
  131.               padding[i] = ((datasize % 4) == 0) ?
  132.                 0 : 4 - datasize % 4;
  133.               strcpy(format[i], "%s");
  134.               nostrings = 0;
  135.               break;
  136.         default:  fprintf(stderr, "OFFReadIndexedPoly: data format not ");
  137.               fprintf(stderr, "valid for indexed poly type\n");
  138.               return (-1);
  139.         }
  140.     datasize += padding[i] + size[i];
  141.     }
  142.  
  143.     endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
  144.     datasize += endpad;
  145.  
  146. /*  Allocate memory for the points and vertices  */
  147.     pProp->PropData = (char *) malloc(sizeof(long) * 3
  148.         + datasize * npts
  149.         + sizeof(short) * npolys 
  150.         + sizeof(short) * nconnects);
  151.  
  152.     ptr = pProp->PropData;
  153.     lptr = (long *) ptr;
  154.     *lptr++ = npts;
  155.     *lptr++ = npolys;
  156.     *lptr++ = nconnects;
  157.     ptr = (char *) lptr;
  158.  
  159.     if (type == OFF_ASCII)    /* Read info from the ascii file */
  160.     {
  161.     /*  Read in all the points  */
  162.     for(i = 0; i < npts; i++)
  163.         {
  164.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  165.         {
  166.         ptr += padding[j];
  167.         if (pProp->DataFormat[j] == 's')
  168.             {
  169.             fscanf(ascfd, format[j], bigstr);
  170.             lptr = (long *) ptr;
  171.             *lptr = (long) malloc(strlen(bigstr) + 1);
  172.             strcpy(*lptr, bigstr);
  173.             }
  174.         else if (pProp->DataFormat[j] == 'b')
  175.             { fscanf(ascfd, format[j], &ch); *ptr = ch; }
  176.         else
  177.             fscanf(ascfd, format[j], ptr);
  178.  
  179.         ptr += size[j];
  180.         }
  181.  
  182.         ptr += endpad;
  183.         }
  184.  
  185.     /*  Read in all the polygon counts and connectivity info */
  186.     ptr2 = ptr + sizeof(short) * npolys;
  187.     for(i = 0; i < npolys; i++)
  188.         {
  189.         fscanf(ascfd,"%hd", (short *) ptr);
  190.         for (j = 0; j < *((short *) ptr); j++)
  191.         { fscanf(ascfd,"%hd", (short *) ptr2); ptr2+= sizeof(short); }
  192.         ptr += sizeof(short);
  193.         }
  194.  
  195.     }
  196.  
  197.     else    /* Read info from the binary file */
  198.  
  199.     {
  200.     if (nostrings)
  201.         {
  202.         read(binfd, ptr, datasize * npts);     /* Read object vertex array */
  203.         ptr += datasize * npts;
  204.         }
  205.     else
  206.         {
  207.         for(i = 0; i < npts; i++)
  208.         {
  209.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  210.             {
  211.             ptr += padding[j];
  212.             if (padding[j] != 0) read(binfd, ptr, padding[j]);
  213.             if (strcmp(format[j], "%s") != 0)
  214.             read(binfd, ptr, size[j]);
  215.             else
  216.             {
  217.             read(binfd, &strlength, sizeof(char *));
  218.             lptr = (long *) ptr;
  219.             *lptr = (long) malloc(strlength);
  220.             read(binfd, bigstr, strlength);
  221.             strcpy(*lptr, bigstr);
  222.             if ((strlength % 4) != 0)
  223.                 read(binfd, &junk, 4 - strlength % 4);
  224.             }
  225.             ptr += size[j];
  226.             }
  227.         }
  228.         }
  229.  
  230.     if (endpad != 0)
  231.         {
  232.         read(binfd, ptr, endpad);
  233.         ptr += endpad;
  234.         }
  235.  
  236.     /*  Read poly count array */
  237.     read(binfd, ptr, sizeof(short) * (npolys + nconnects));
  238.     }
  239.  
  240. /*  Close the data file  */
  241.     if (type == OFF_ASCII)
  242.     fclose(ascfd);
  243.     else
  244.     close(binfd);
  245.  
  246.     return(0);
  247.     }
  248.  
  249.