|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - downloadIndex: ┃ T W ┃
Length: 5659 (0x161b) Types: TextFile Names: »WriteIndPoly.c«
└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki └─ ⟦this⟧ »EUUGD11/euug-87hel/sec1/offconv/WriteIndPoly.c«
/* * * Description * Write an indexed_poly data file. * * Output * * Input * pProp Pointer to property structure from which to get data * fname Full path/file name of file to be written * type File type (OFF_ASCII or OFF_BINARY) * * 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 OFFWriteIndexedPoly(pProp, fname, type) OFFProperty *pProp; char *fname; int type; { FILE *ascfd; int binfd; long npts, npolys, nconnects; char *ptr, *ptr2; int i, j, k; long *lptr; char format[MAX_DATA_ITEMS][10]; int padding[MAX_DATA_ITEMS]; int size[MAX_DATA_ITEMS]; int datasize = 0; char ch; char bigstr[OFF_BIGSTR]; int nostrings = 1; long strlength; long code; long zeros = 0; int endpad; char dstring[80]; long wsize; /* Open the file, punt if file open fails */ if (type == OFF_ASCII) { ascfd = fopen(fname, "w"); if (ascfd == NULL) { fprintf(stderr, "OFFWriteIndexedPoly: cannot open data file %s\n", fname); return(-1); } } else { binfd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (binfd < 0) { fprintf(stderr, "OFFWriteIndexedPoly: cannot open data file %s\n", fname); return(-1); } code = OFF_INDEXED_POLY_MAGIC; write(binfd, &code, sizeof(long)); } /* Write out the number of data items for the list */ ptr = pProp->PropData; lptr = (long *) ptr; npts = *lptr++; npolys = *lptr++; nconnects = *lptr++; ptr = (char *) lptr; if (type == OFF_ASCII) fprintf(ascfd,"%d\t%d\t%d\n", npts, npolys, nconnects); else { write(binfd, &npts, sizeof(long)); write(binfd, &npolys, sizeof(long)); write(binfd, &nconnects, sizeof(long)); } /* 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\t"); break; case 'f': size[i] = sizeof(float); padding[i] = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4; strcpy(format[i], "%g\t"); break; case 'd': size[i] = sizeof(double); padding[i] = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4; strcpy(format[i], "%*.*g\t"); break; case 'h': size[i] = sizeof(short); padding[i] = ((datasize % 2) == 0) ? 0 : 1; strcpy(format[i], "%d\t"); break; case 'b': size[i] = sizeof(char); padding[i] = 0; strcpy(format[i], "%d\t"); break; case 's': size[i] = sizeof(char *); padding[i] = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4; strcpy(format[i], "%s\t"); nostrings = 0; break; default: fprintf(stderr, "OFFWriteIndexedPoly: data format not "); fprintf(stderr, "valid for indexed_poly data type\n"); return (-1); } datasize += padding[i] + size[i]; } endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4; datasize += endpad; if (type == OFF_ASCII) /* Write info to the ascii file */ { for(i = 0; i < npts; i++) { for(j = 0; j < strlen(pProp->DataFormat); j++) { ptr += padding[j]; if (pProp->DataFormat[j] == 'f') fprintf(ascfd, format[j], *((float *) ptr)); else if (pProp->DataFormat[j] == 'i') fprintf(ascfd, format[j], *((long *) ptr)); else if (pProp->DataFormat[j] == 'd') { /* I wouldn't have to do this if %g format worked right*/ sprintf(dstring, "%-30.30g", *((double *) ptr)); k = 0; while (dstring[k] != ' ') k++; fprintf(ascfd, format[j], k, k, *((double *) ptr)); } else if (pProp->DataFormat[j] == 'h') fprintf(ascfd, format[j], *((short *) ptr)); else if (pProp->DataFormat[j] == 's') fprintf(ascfd, format[j], *((char **) ptr)); else if (pProp->DataFormat[j] == 'b') fprintf(ascfd, format[j], *((unsigned char *) ptr)); ptr += size[j]; } fprintf(ascfd, "\n"); ptr += endpad; } ptr2 = ptr + sizeof(short) * npolys; for (i = 0; i < npolys; i++) { fprintf(ascfd, "%hd\t", *((short *) ptr)); for (j = 0; j < *((short *) ptr); j++) { fprintf(ascfd, "%hd ", *((short *) ptr2)); ptr2 += sizeof(short); } fprintf(ascfd, "\n"); ptr += sizeof(short); } } else /* Write info to the binary file */ { wsize = datasize * npts + sizeof(short) * (npolys + nconnects); if (nostrings) write(binfd, ptr, wsize); /* Write 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) write(binfd, &zeros, padding[j]); if (format[j][1] != 's') write(binfd, ptr, size[j]); else { strlength = strlen(*((char **) ptr)) + 1; write(binfd, &strlength, sizeof(long)); write(binfd, *((char **) ptr), strlength); strlength = ((strlength % 4) == 0) ? 0 : 4 - strlength % 4; write(binfd, &zeros, strlength); } ptr += size[j]; } if (endpad != 0) write(binfd, &zeros, endpad); ptr += endpad; } write(binfd, ptr, sizeof(short) * (npolys + nconnects)); } } /* Close the data file */ if (type == OFF_ASCII) fclose(ascfd); else close(binfd); return(0); }