home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / xpm-3.2a / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-16  |  4.4 KB  |  185 lines

  1. /* Copyright 1990,91 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * misc.c:                                                                     *
  4. *                                                                             *
  5. *  XPM library                                                               *
  6. *  Miscellaneous utilities                                                    *
  7. *                                                                             *
  8. *  Developed by Arnaud Le Hors                                                *
  9. \*****************************************************************************/
  10.  
  11. #include "xpmP.h"
  12.  
  13. /*
  14.  * Free the computed color table
  15.  */
  16.  
  17. xpmFreeColorTable(colorTable, ncolors)
  18.     char ***colorTable;
  19.     int ncolors;
  20. {
  21.     int a, b;
  22.     char ***ct, **cts;
  23.  
  24.     if (colorTable) {
  25.     for (a = 0, ct = colorTable; a < ncolors; a++, ct++)
  26.         if (*ct) {
  27.         for (b = 0, cts = *ct; b <= NKEYS; b++, cts++)
  28.             if (*cts)
  29.             free(*cts);
  30.         free(*ct);
  31.         }
  32.     free(colorTable);
  33.     }
  34. }
  35.  
  36.  
  37. /*
  38.  * Intialize the xpmInternAttrib pointers to Null to know
  39.  * which ones must be freed later on.
  40.  */
  41.  
  42. xpmInitInternAttrib(attrib)
  43.     xpmInternAttrib *attrib;
  44. {
  45.     attrib->ncolors = 0;
  46.     attrib->colorTable = NULL;
  47.     attrib->pixelindex = NULL;
  48.     attrib->xcolors = NULL;
  49.     attrib->colorStrings = NULL;
  50.     attrib->mask_pixel = UNDEF_PIXEL;
  51. }
  52.  
  53.  
  54. /*
  55.  * Free the xpmInternAttrib pointers which have been allocated
  56.  */
  57.  
  58. xpmFreeInternAttrib(attrib)
  59.     xpmInternAttrib *attrib;
  60. {
  61.     unsigned int a, ncolors;
  62.     char **sptr;
  63.  
  64.     if (attrib->colorTable)
  65.     xpmFreeColorTable(attrib->colorTable, attrib->ncolors);
  66.     if (attrib->pixelindex)
  67.     free(attrib->pixelindex);
  68.     if (attrib->xcolors)
  69.     free(attrib->xcolors);
  70.     if (attrib->colorStrings) {
  71.     ncolors = attrib->ncolors;
  72.     for (a = 0, sptr = attrib->colorStrings; a < ncolors; a++, sptr++)
  73.         if (*sptr)
  74.         free(*sptr);
  75.     free(attrib->colorStrings);
  76.     }
  77. }
  78.  
  79.  
  80. /*
  81.  * Free array of extensions
  82.  */
  83. XpmFreeExtensions(extensions, nextensions)
  84.     XpmExtension *extensions;
  85.     int          nextensions;
  86. {
  87.     unsigned int i, j, nlines;
  88.     XpmExtension *ext;
  89.     char **sptr;
  90.  
  91.     for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
  92.     free(ext->name);
  93.     nlines = ext->nlines;
  94.     for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
  95.         if (*sptr)
  96.         free(*sptr);
  97.     if (ext->lines)
  98.         free(ext->lines);
  99.     }
  100.     free(extensions);
  101. }
  102.  
  103.  
  104. /*
  105.  * Return the XpmAttributes structure size
  106.  */
  107.  
  108. XpmAttributesSize()
  109. {
  110.     return sizeof(XpmAttributes);
  111. }
  112.  
  113.  
  114. /*
  115.  * Free the XpmAttributes structure members
  116.  * but the structure itself
  117.  */
  118.  
  119. XpmFreeAttributes(attributes)
  120.     XpmAttributes *attributes;
  121. {
  122.     if (attributes) {
  123.     if (attributes->valuemask & XpmReturnPixels && attributes->pixels) {
  124.         free(attributes->pixels);
  125.         attributes->pixels = NULL;
  126.         attributes->npixels = 0;
  127.     }
  128.     if (attributes->valuemask & XpmInfos) {
  129.         if (attributes->colorTable) {
  130.         xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
  131.         attributes->colorTable = NULL;
  132.         attributes->ncolors = 0;
  133.         }
  134.         if (attributes->hints_cmt) {
  135.         free(attributes->hints_cmt);
  136.         attributes->hints_cmt = NULL;
  137.         }
  138.         if (attributes->colors_cmt) {
  139.         free(attributes->colors_cmt);
  140.         attributes->colors_cmt = NULL;
  141.         }
  142.         if (attributes->pixels_cmt) {
  143.         free(attributes->pixels_cmt);
  144.         attributes->pixels_cmt = NULL;
  145.         }
  146.         if (attributes->pixels) {
  147.         free(attributes->pixels);
  148.         attributes->pixels = NULL;
  149.         }
  150.     }
  151.     if (attributes->valuemask & XpmReturnExtensions
  152.         && attributes->nextensions) {
  153.             XpmFreeExtensions(attributes->extensions, attributes->nextensions);
  154.         attributes->nextensions = 0;
  155.         attributes->extensions = NULL;
  156.     }
  157.     attributes->valuemask = 0;
  158.     }
  159. }
  160.  
  161.  
  162. /*
  163.  * Store into the XpmAttributes structure the required informations stored in
  164.  * the xpmInternAttrib structure.
  165.  */
  166.  
  167. xpmSetAttributes(attrib, attributes)
  168.     xpmInternAttrib *attrib;
  169.     XpmAttributes *attributes;
  170. {
  171.     if (attributes) {
  172.     if (attributes->valuemask & XpmReturnInfos) {
  173.         attributes->cpp = attrib->cpp;
  174.         attributes->ncolors = attrib->ncolors;
  175.         attributes->colorTable = attrib->colorTable;
  176.  
  177.         attrib->ncolors = 0;
  178.         attrib->colorTable = NULL;
  179.     }
  180.     attributes->width = attrib->width;
  181.     attributes->height = attrib->height;
  182.     attributes->valuemask |= XpmSize;
  183.     }
  184. }
  185.