home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Encyclopedia of Graphics File Formats Companion
/
GFF_CD.ISO
/
formats
/
off
/
code
/
readinde.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-06-20
|
6KB
|
242 lines
/*
*
* Description
* Read an indexed 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
* 11-Oct-89 Created
*
*/
#include <stdio.h>
#include <sys/file.h>
#include "off.h"
#define MAX_DATA_ITEMS 30
OFFReadIndexed(pProp, fname)
OFFProperty *pProp;
char *fname;
{
FILE *ascfd;
int binfd;
int code;
char *ptr;
int i, j;
long nitems, nindices;
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, "OFFReadIndexed: 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_INDEXED_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, "OFFReadIndexed: cannot open data file %s\n",
fname);
return(-1);
}
type = OFF_ASCII;
}
/* Read in the number of data items and indices for the object */
if (type == OFF_ASCII)
fscanf(ascfd,"%d %d\n", &nitems, &nindices);
else
{
read(binfd, &nitems, sizeof(long));
read(binfd, &nindices, sizeof(long));
}
pProp->PropCount = nitems;
/* 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, "OFFReadIndexed: data format not ");
fprintf(stderr, "valid for indexed data type\n");
return (-1);
}
datasize += padding[i] + size[i];
}
endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
datasize += endpad;
/* Allocate memory for the items and indices */
pProp->PropData = (char *) malloc(sizeof(long) * 2
+ datasize * nitems
+ sizeof(short) * nindices);
ptr = pProp->PropData;
lptr = (long *) ptr;
*lptr++ = nitems;
*lptr++ = nindices;
ptr = (char *) lptr;
if (type == OFF_ASCII) /* Read info from the ascii file */
{
/* Read in all the data items */
for(i = 0; i < nitems; 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;
}
/* Read in all the indices */
for(i = 0; i < nindices; i++)
{
fscanf(ascfd,"%hd", (short *) ptr);
ptr += sizeof(short);
}
}
else /* Read info from the binary file */
{
if (nostrings)
{
read(binfd, ptr, datasize * nitems); /* Read data items */
ptr += datasize * nitems;
}
else
{
for(i = 0; i < nitems; 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;
}
/* Read indices */
read(binfd, ptr, sizeof(short) * nindices);
}
/* Close the data file */
if (type == OFF_ASCII)
fclose(ascfd);
else
close(binfd);
return(0);
}