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

  1.  
  2. /*
  3.  *
  4.  * Description
  5.  *    Write an indexed data file.
  6.  *
  7.  * Output
  8.  *
  9.  * Input
  10.  *    pProp        Pointer to property structure from which to get data
  11.  *    fname        Full path/file name of file to be written
  12.  *    type        File type (OFF_ASCII or OFF_BINARY)
  13.  *
  14.  * Diagnostics
  15.  *    Returns 0 if successful, -1 if unsuccessful for any reason.
  16.  *
  17.  * Author
  18.  *    Randi J. Rost
  19.  *    Digital Equipment Corp.
  20.  *    Workstation Systems Engineering
  21.  *    Palo Alto, CA
  22.  *
  23.  * History
  24.  *    11-Nov-89    Created
  25.  *
  26.  */
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <sys/file.h>
  31. #include "off.h"
  32.  
  33. #define MAX_DATA_ITEMS    30
  34.  
  35. OFFWriteIndexed(pProp, fname, type)
  36.     OFFProperty    *pProp;
  37.     char    *fname;
  38.     int        type;
  39.  
  40.     {
  41.     FILE    *ascfd;
  42.     int        binfd;
  43.     long    nitems, nindices;
  44.     char    *ptr;
  45.     int        i, j, k;
  46.     long    *lptr;
  47.     char    format[MAX_DATA_ITEMS][10];
  48.     int        padding[MAX_DATA_ITEMS];
  49.     int        size[MAX_DATA_ITEMS];
  50.     int        datasize = 0;
  51.     int        nostrings = 1;
  52.     long    strlength;
  53.     long    code;
  54.     long    zeros = 0;
  55.     int        endpad;
  56.     char    dstring[80];
  57.     long    wsize;
  58.  
  59.  
  60. /*  Open the file, punt if file open fails  */
  61.     if (type == OFF_ASCII)
  62.     {
  63.     ascfd = fopen(fname, "w");
  64.     if (ascfd == NULL)
  65.         {
  66.         fprintf(stderr, "OFFWriteIndexed: cannot open data file %s\n",
  67.             fname);
  68.         return(-1);
  69.         }
  70.     }
  71.     else
  72.     {
  73.     binfd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  74.     if (binfd < 0)
  75.         {
  76.         fprintf(stderr, "OFFWriteIndexed: cannot open data file %s\n",
  77.             fname);
  78.         return(-1);
  79.         }
  80.     code = OFF_INDEXED_MAGIC;
  81.     write(binfd, &code, sizeof(long));
  82.     }
  83.  
  84.  
  85. /*  Write out the number of data items for the list  */
  86.     ptr = pProp->PropData;
  87.     lptr = (long *) ptr;
  88.     nitems = *lptr++;
  89.     nindices = *lptr++;
  90.     ptr = (char *) lptr;
  91.     if (type == OFF_ASCII)
  92.     fprintf(ascfd,"%d\t%d\n", nitems, nindices);
  93.     else
  94.     {
  95.     write(binfd, &nitems, sizeof(long));
  96.     write(binfd, &nindices, sizeof(long));
  97.     }
  98.  
  99. /*  Compute data size  */
  100.     for (i = 0; i < strlen(pProp->DataFormat); i++)
  101.     {
  102.     switch (pProp->DataFormat[i])
  103.         {
  104.         case 'i': size[i] = sizeof(long);
  105.               padding[i] = ((datasize % 4) == 0) ?
  106.                 0 : 4 - datasize % 4;
  107.               strcpy(format[i], "%d\t");
  108.               break;
  109.         case 'f': size[i] = sizeof(float);
  110.               padding[i] = ((datasize % 4) == 0) ?
  111.                 0 : 4 - datasize % 4;
  112.               strcpy(format[i], "%g\t");
  113.               break;
  114.         case 'd': size[i] = sizeof(double);
  115.               padding[i] = ((datasize % 4) == 0) ?
  116.                 0 : 4 - datasize % 4;
  117.               strcpy(format[i], "%*.*g\t");
  118.               break;
  119.         case 'h': size[i] = sizeof(short);
  120.               padding[i] = ((datasize % 2) == 0) ? 0 : 1;
  121.               strcpy(format[i], "%d\t");
  122.               break;
  123.         case 'b': size[i] = sizeof(char);
  124.               padding[i] = 0;
  125.               strcpy(format[i], "%d\t");
  126.               break;
  127.         case 's': size[i] = sizeof(char *);
  128.               padding[i] = ((datasize % 4) == 0) ?
  129.                 0 : 4 - datasize % 4;
  130.               strcpy(format[i], "%s\t");
  131.               nostrings = 0;
  132.               break;
  133.         default:  fprintf(stderr, "OFFWriteIndexed: data format not ");
  134.               fprintf(stderr, "valid for indexed data type\n");
  135.               return (-1);
  136.         }
  137.     datasize += padding[i] + size[i];
  138.     }
  139.  
  140.     endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
  141.     datasize += endpad;
  142.  
  143.  
  144.     if (type == OFF_ASCII)    /* Write info to the ascii file */
  145.     {
  146.     for(i = 0; i < nitems; i++)
  147.         {
  148.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  149.         {
  150.         ptr += padding[j];
  151.         if (pProp->DataFormat[j] == 'f')
  152.             fprintf(ascfd, format[j], *((float *) ptr));
  153.         else if (pProp->DataFormat[j] == 'i')
  154.             fprintf(ascfd, format[j], *((long *) ptr));
  155.         else if (pProp->DataFormat[j] == 'd')
  156.             { /* I wouldn't have to do this if %g format worked right*/
  157.             sprintf(dstring, "%-30.30g", *((double *) ptr));
  158.             k = 0;
  159.             while (dstring[k] != ' ') k++;
  160.             fprintf(ascfd, format[j], k, k, *((double *) ptr));
  161.             }
  162.         else if (pProp->DataFormat[j] == 'h')
  163.             fprintf(ascfd, format[j], *((short *) ptr));
  164.         else if (pProp->DataFormat[j] == 's')
  165.             fprintf(ascfd, format[j], *((char **) ptr));
  166.         else if (pProp->DataFormat[j] == 'b')
  167.             fprintf(ascfd, format[j], *((unsigned char *) ptr));
  168.  
  169.         ptr += size[j];
  170.         }
  171.  
  172.         fprintf(ascfd, "\n");
  173.         ptr += endpad;
  174.         }
  175.     for (i = 0; i < nindices; i++)
  176.         {
  177.         fprintf(ascfd, "%hd\n", *((short *) ptr));
  178.         ptr += sizeof(short);
  179.         }
  180.     }
  181.  
  182.     else    /* Write info to the binary file */
  183.  
  184.     {
  185.     wsize = datasize * nitems + endpad + sizeof(short) * nindices;
  186.     if (nostrings)
  187.         write(binfd, ptr, wsize); /* Write indexed data item array */
  188.     else
  189.         {
  190.         for(i = 0; i < nitems; i++)
  191.         {
  192.         for(j = 0; j < strlen(pProp->DataFormat); j++)
  193.             {
  194.             ptr += padding[j];
  195.             if (padding[j] != 0)  write(binfd, &zeros, padding[j]);
  196.             if (format[j][1] != 's')
  197.             write(binfd, ptr, size[j]);
  198.             else
  199.             {
  200.             strlength = strlen(*((char **) ptr)) + 1;
  201.             write(binfd, &strlength, sizeof(long));
  202.             write(binfd, *((char **) ptr), strlength);
  203.             strlength = ((strlength % 4) == 0) ?
  204.                 0 : 4 - strlength % 4;
  205.             write(binfd, &zeros, strlength);
  206.             }
  207.             ptr += size[j];
  208.             }
  209.         if (endpad != 0)  write(binfd, &zeros, endpad);
  210.         ptr += endpad;
  211.         }
  212.         write(binfd, ptr, sizeof(short) * nindices);
  213.         }
  214.     }
  215.  
  216. /*  Close the data file  */
  217.     if (type == OFF_ASCII)
  218.     fclose(ascfd);
  219.     else
  220.     close(binfd);
  221.  
  222.     return(0);
  223.     }
  224.  
  225.