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

  1.  
  2. /*
  3.  *
  4.  * Description
  5.  *    Free a property structure and associated memory resources.
  6.  *
  7.  * Output
  8.  *
  9.  * Input
  10.  *    pProp        Pointer to the property structure to be deallocated.
  11.  *
  12.  * Diagnostics
  13.  *    Returns 0 if successful, -1 if unsuccessful (unknown data type).
  14.  *
  15.  * Author
  16.  *    Randi J. Rost
  17.  *    Digital Equipment Corp.
  18.  *    Workstation Systems Engineering
  19.  *    Palo Alto, CA
  20.  *
  21.  * History
  22.  *    17-Nov-86    Created
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include "off.h"
  28.  
  29.  
  30. OFFFreeProperty(pProp)
  31.     OFFProperty    *pProp;
  32.  
  33.     {
  34.     int        i;
  35.     int        nostrings = 1;
  36.  
  37.     if (strcmp(pProp->PropName, "comment") == 0)
  38.     {
  39.     free(pProp->PropData);
  40.     free(pProp);
  41.     return(0);
  42.     }
  43.  
  44.     if (strcmp(pProp->PropName, "nl") == 0)
  45.     {
  46.     free(pProp);
  47.     return(0);
  48.     }
  49.  
  50.     if (strcmp(pProp->PropName, "name") == 0 ||
  51.     strcmp(pProp->PropName, "author") == 0 ||
  52.     strcmp(pProp->PropName, "type") == 0 ||
  53.     strcmp(pProp->PropName, "description") == 0 ||
  54.     strcmp(pProp->PropName, "copyright") == 0)
  55.     {
  56.     free(pProp->PropData);
  57.     free(pProp);
  58.     return(0);
  59.     }
  60.  
  61.     else
  62.     {
  63.     for (i = 0; i < strlen(pProp->DataFormat); i++)
  64.         if (pProp->DataFormat[i] == 's') nostrings = 0;
  65.     switch(pProp->PropType)
  66.         {
  67.         case OFF_DEFAULT_DATA:
  68.         if (!nostrings)
  69.             FreeStrings(pProp->PropData, pProp->DataFormat, 1);
  70.         break;
  71.  
  72.         case OFF_GENERIC_DATA:
  73.         if (!nostrings)
  74.             FreeStrings(pProp->PropData + sizeof(long),
  75.             pProp->DataFormat, pProp->PropCount);
  76.         break;
  77.  
  78.         case OFF_INDEXED_DATA:
  79.         if (nostrings)
  80.             FreeStrings(pProp->PropData + 2 * sizeof(long),
  81.             pProp->DataFormat, pProp->PropCount);
  82.         break;
  83.  
  84.         case OFF_INDEXED_POLY_DATA:
  85.         if (nostrings)
  86.             FreeStrings(pProp->PropData + 3 * sizeof(long),
  87.             pProp->DataFormat, pProp->PropCount);
  88.         break;
  89.  
  90.         default:
  91.         return(-1);
  92.         break;
  93.         } /* switch */
  94.  
  95.     free(pProp->PropData);
  96.     free(pProp);
  97.     }
  98.  
  99.     return(0);
  100.     }
  101.  
  102.  
  103.  
  104. static int FreeStrings(dataptr, format)
  105.     char    *dataptr;
  106.     char    *format;
  107.  
  108.     {
  109.     int        i;
  110.     char    *ptr;
  111.  
  112.     ptr = dataptr;
  113.     for (i = 0; i < strlen(format); i++)
  114.     {
  115.     switch(format[i])
  116.         {
  117.         case 'i':
  118.          /* Make sure we're aligned on word boundary */
  119.          ptr += (((int) ptr % 4) == 0) ? 0 : 4 - (int) ptr % 4;
  120.          ptr += sizeof(long);
  121.          break;
  122.         case 'b':
  123.          ptr++;
  124.          break;
  125.         case 'd':
  126.          /* Make sure we're aligned on word boundary */
  127.          ptr += (((int) ptr % 4) == 0) ? 0 : 4 - (int) ptr % 4;
  128.          ptr += sizeof(double);
  129.          break;
  130.         case 'h':
  131.          /* Make sure we're aligned on halfword boundary */
  132.          ptr += (((int) ptr % 2) == 0) ? 0 : 1;
  133.          ptr += sizeof(short);
  134.          break;
  135.         case 'f':
  136.          /* Make sure we're aligned on word boundary */
  137.          ptr += (((int) ptr % 4) == 0) ? 0 : 4 - (int) ptr % 4;
  138.          ptr += sizeof(float);
  139.          break;
  140.         case 's':
  141.          /* Make sure we're aligned on word boundary */
  142.          ptr += (((int) ptr % 4) == 0) ? 0 : 4 - (int) ptr % 4;
  143.          free(*((char **) ptr));
  144.          ptr += sizeof(char *);
  145.          break;
  146.         default:
  147.          return(-1);
  148.         }
  149.     }
  150.     return(0);
  151.     }
  152.