home *** CD-ROM | disk | FTP | other *** search
-
- /*
- *
- * Description
- * Read a generic data file.
- *
- * Output
- *
- * Input
- * pProp Pointer to property structure in which to store data
- * fname Full path/file name of file to be read.
- *
- * Diagnostics
- * Returns 0 if successful, -1 if unsuccessful for any reason.
- *
- * Author
- * Randi J. Rost
- * Digital Equipment Corp.
- * Workstation Systems Engineering
- * Palo Alto, CA
- *
- * History
- * 17-Nov-86 Created
- *
- */
-
-
- #include <stdio.h>
- #include <sys/file.h>
- #include "off.h"
-
- #define MAX_DATA_ITEMS 30
-
- OFFReadGeneric(pProp, fname)
- OFFProperty *pProp;
- char *fname;
-
- {
- FILE *ascfd;
- int binfd;
- long code;
- char *ptr;
- int i, j;
- long npts;
- long *lptr;
- char format[MAX_DATA_ITEMS][10];
- int padding[MAX_DATA_ITEMS];
- int size[MAX_DATA_ITEMS];
- int datasize = 0;
- int type = OFF_BINARY;
- char ch;
- char bigstr[OFF_BIGSTR];
- int nostrings = 1;
- int strlength;
- int endpad;
- long junk;
-
-
- /* Try opening the file as if it were binary first */
- binfd = open(fname, O_RDONLY, 0);
-
- /* If error opening file, punt */
- if (binfd < 0)
- {
- fprintf(stderr, "OFFReadGeneric: cannot open data file %s\n",
- fname);
- return(-1);
- }
-
- /* Read first word of file to determine file type */
- read(binfd, &code, sizeof(long));
-
- if (code != OFF_GENERIC_MAGIC)
- {
- /* Close the file */
- close(binfd);
-
- /* Try to open it as an ascii data file */
- ascfd = fopen(fname, "r");
-
- /* If error opening file, punt */
- if (ascfd == NULL)
- {
- fprintf(stderr, "OFFReadGeneric: cannot open data file %s\n",
- fname);
- return(-1);
- }
- type = OFF_ASCII;
- }
-
- /* Read in the number of data items for the list */
- if (type == OFF_ASCII)
- fscanf(ascfd,"%d\n", &npts);
- else
- read(binfd, &npts, sizeof(long));
- pProp->PropCount = npts;
-
- /* Compute data size */
- for (i = 0; i < strlen(pProp->DataFormat); i++)
- {
- switch (pProp->DataFormat[i])
- {
- case 'i': size[i] = sizeof(long);
- padding[i] = ((datasize % 4) == 0) ?
- 0 : 4 - datasize % 4;
- strcpy(format[i], "%d");
- break;
- case 'f': size[i] = sizeof(float);
- padding[i] = ((datasize % 4) == 0) ?
- 0 : 4 - datasize % 4;
- strcpy(format[i], "%f");
- break;
- case 'd': size[i] = sizeof(double);
- padding[i] = ((datasize % 4) == 0) ?
- 0 : 4 - datasize % 4;
- strcpy(format[i], "%F");
- break;
- case 'h': size[i] = sizeof(short);
- padding[i] = ((datasize % 2) == 0) ? 0 : 1;
- strcpy(format[i], "%hd");
- break;
- case 'b': size[i] = sizeof(char);
- padding[i] = 0;
- strcpy(format[i], "%d");
- break;
- case 's': size[i] = sizeof(char *);
- padding[i] = ((datasize % 4) == 0) ?
- 0 : 4 - datasize % 4;
- strcpy(format[i], "%s");
- nostrings = 0;
- break;
- default: fprintf(stderr, "OFFReadGeneric: data format not ");
- fprintf(stderr, "valid for generic data type\n");
- return (-1);
- }
- datasize += padding[i] + size[i];
- }
-
- endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
- datasize += endpad;
-
- /* Allocate memory for the points and vertices */
- pProp->PropData = (char *) malloc(sizeof(long) + datasize * npts);
-
- ptr = pProp->PropData;
- lptr = (long *) ptr;
- *lptr = npts;
- ptr += sizeof(long);
-
- if (type == OFF_ASCII) /* Read info from the ascii file */
- {
- /* Read in all the data */
- for(i = 0; i < npts; i++)
- {
- for(j = 0; j < strlen(pProp->DataFormat); j++)
- {
- ptr += padding[j];
- if (pProp->DataFormat[j] == 's')
- {
- fscanf(ascfd, format[j], bigstr);
- lptr = (long *) ptr;
- *lptr = (long) malloc(strlen(bigstr) + 1);
- strcpy(*lptr, bigstr);
- }
- else if (pProp->DataFormat[j] == 'b')
- { fscanf(ascfd, format[j], &ch); *ptr = ch; }
- else
- fscanf(ascfd, format[j], ptr);
-
- ptr += size[j];
- }
-
- ptr += endpad;
- }
- }
-
- else /* Read info from the binary file */
-
- {
- if (nostrings)
- read(binfd, ptr, datasize * npts); /* Read object vertex array */
- else
- {
- for(i = 0; i < npts; i++)
- {
- for(j = 0; j < strlen(pProp->DataFormat); j++)
- {
- ptr += padding[j];
- if (padding[j] != 0) read(binfd, ptr, padding[j]);
- if (strcmp(format[j], "%s") != 0)
- read(binfd, ptr, size[j]);
- else
- {
- read(binfd, &strlength, sizeof(char *));
- lptr = (long *) ptr;
- *lptr = (long) malloc(strlength);
- read(binfd, bigstr, strlength);
- strcpy(*lptr, bigstr);
- if ((strlength % 4) != 0)
- read(binfd, &junk, 4 - strlength % 4);
- }
- ptr += size[j];
- }
- if (endpad != 0) read(binfd, ptr, endpad);
- ptr += endpad;
- }
- }
- }
-
- /* Close the data file */
- if (type == OFF_ASCII)
- fclose(ascfd);
- else
- close(binfd);
-
- return(0);
- }
-
-