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

  1.  
  2. /*
  3.  *
  4.  * Description
  5.  *    Read a generic 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. OFFReadGeneric(pProp, fname)
  35.     OFFProperty    *pProp;
  36.     char    *fname;
  37.  
  38.     {
  39.     FILE    *ascfd;
  40.     int        binfd;
  41.     long    code;
  42.     char    *ptr;
  43.     int        i, j;
  44.     long    npts;
  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, "OFFReadGeneric: 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_GENERIC_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, "OFFReadGeneric: cannot open data file %s\n",
  85.             fname);
  86.         return(-1);
  87.         }
  88.     type = OFF_ASCII;
  89.     }
  90.  
  91. /*  Read in the number of data items for the list  */
  92.     if (type == OFF_ASCII)
  93.     fscanf(ascfd,"%d\n", &npts);
  94.     else
  95.     read(binfd, &npts, sizeof(long));
  96.     pProp->PropCount = npts;
  97.  
  98. /*  Compute data size  */
  99.     for (i = 0; i < strlen(pProp->DataFormat); i++)
  100.     {
  101.     switch (pProp->DataFormat[i])
  102.         {
  103.         case 'i': size[i] = sizeof(long);
  104.               padding[i] = ((datasize % 4) == 0) ?
  105.                 0 : 4 - datasize % 4;
  106.               strcpy(format[i], "%d");
  107.               break;
  108.         case 'f': size[i] = sizeof(float);
  109.               padding[i] = ((datasize % 4) == 0) ?
  110.                 0 : 4 - datasize % 4;
  111.               strcpy(format[i], "%f");
  112.               break;
  113.         case 'd': size[i] = sizeof(double);
  114.               padding[i] = ((datasize % 4) == 0) ?
  115.                 0 : 4 - datasize % 4;
  116.               strcpy(format[i], "%F");
  117.               break;
  118.         case 'h': size[i] = sizeof(short);
  119.               padding[i] = ((datasize % 2) == 0) ? 0 : 1;
  120.               strcpy(format[i], "%hd");
  121.               break;
  122.         case 'b': size[i] = sizeof(char);
  123.               padding[i] = 0;
  124.               strcpy(format[i], "%d");
  125.               break;
  126.         case 's': size[i] = sizeof(char *);
  127.               padding[i] = ((datasize % 4) == 0) ?
  128.                 0 : 4 - datasize % 4;
  129.               strcpy(format[i], "%s");
  130.               nostrings = 0;
  131.               break;
  132.         default:  fprintf(stderr, "OFFReadGeneric: data format not ");
  133.               fprintf(stderr, "valid for generic data type\n");
  134.               return (-1);
  135.         }
  136.     datasize += padding[i] + size[i];
  137.     }
  138.  
  139.     endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
  140.     datasize += endpad;
  141.  
  142. /*  Allocate memory for the points and vertices  */
  143.     pProp->PropData = (char *) malloc(sizeof(long) + datasize * npts);
  144.  
  145.     ptr = pProp->PropData;
  146.     lptr = (long *) ptr;
  147.     *lptr = npts;
  148.     ptr += sizeof(long);
  149.  
  150.     if (type == OFF_ASCII)    /* Read info from the ascii file */
  151.     {
  152.     /*  Read in all the data  */
  153.     for(i = 0; i < npts; i++)
  154.         {
  155.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  156.         {
  157.         ptr += padding[j];
  158.         if (pProp->DataFormat[j] == 's')
  159.             {
  160.             fscanf(ascfd, format[j], bigstr);
  161.             lptr = (long *) ptr;
  162.             *lptr = (long) malloc(strlen(bigstr) + 1);
  163.             strcpy(*lptr, bigstr);
  164.             }
  165.         else if (pProp->DataFormat[j] == 'b')
  166.             { fscanf(ascfd, format[j], &ch); *ptr = ch; }
  167.         else
  168.             fscanf(ascfd, format[j], ptr);
  169.  
  170.         ptr += size[j];
  171.         }
  172.  
  173.         ptr += endpad;
  174.         }
  175.     }
  176.  
  177.     else    /* Read info from the binary file */
  178.  
  179.     {
  180.     if (nostrings)
  181.         read(binfd, ptr, datasize * npts);     /* Read object vertex array */
  182.     else
  183.         {
  184.         for(i = 0; i < npts; i++)
  185.         {
  186.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  187.             {
  188.             ptr += padding[j];
  189.             if (padding[j] != 0) read(binfd, ptr, padding[j]);
  190.             if (strcmp(format[j], "%s") != 0)
  191.             read(binfd, ptr, size[j]);
  192.             else
  193.             {
  194.             read(binfd, &strlength, sizeof(char *));
  195.             lptr = (long *) ptr;
  196.             *lptr = (long) malloc(strlength);
  197.             read(binfd, bigstr, strlength);
  198.             strcpy(*lptr, bigstr);
  199.             if ((strlength % 4) != 0)
  200.                 read(binfd, &junk, 4 - strlength % 4);
  201.             }
  202.             ptr += size[j];
  203.             }
  204.         if (endpad != 0) read(binfd, ptr, endpad);
  205.         ptr += endpad;
  206.         }
  207.         }
  208.     }
  209.  
  210. /*  Close the data file  */
  211.     if (type == OFF_ASCII)
  212.     fclose(ascfd);
  213.     else
  214.     close(binfd);
  215.  
  216.     return(0);
  217.     }
  218.  
  219.