home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / xpm / misc.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  5KB  |  207 lines

  1. /* Copyright 1990-92 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.     if (extensions) {
  92.     for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
  93.         if (ext->name)
  94.         free(ext->name);
  95.         nlines = ext->nlines;
  96.         for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
  97.         if (*sptr)
  98.             free(*sptr);
  99.         if (ext->lines)
  100.         free(ext->lines);
  101.     }
  102.     free(extensions);
  103.     }
  104. }
  105.  
  106.  
  107. /*
  108.  * Return the XpmAttributes structure size
  109.  */
  110.  
  111. XpmAttributesSize()
  112. {
  113.     return sizeof(XpmAttributes);
  114. }
  115.  
  116.  
  117. /*
  118.  * Free the XpmAttributes structure members
  119.  * but the structure itself
  120.  */
  121.  
  122. XpmFreeAttributes(attributes)
  123.     XpmAttributes *attributes;
  124. {
  125.     if (attributes) {
  126.     if (attributes->valuemask & XpmReturnPixels && attributes->pixels) {
  127.         free(attributes->pixels);
  128.         attributes->pixels = NULL;
  129.         attributes->npixels = 0;
  130.     }
  131.     if (attributes->valuemask & XpmInfos) {
  132.         if (attributes->colorTable) {
  133.         xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
  134.         attributes->colorTable = NULL;
  135.         attributes->ncolors = 0;
  136.         }
  137.         if (attributes->hints_cmt) {
  138.         free(attributes->hints_cmt);
  139.         attributes->hints_cmt = NULL;
  140.         }
  141.         if (attributes->colors_cmt) {
  142.         free(attributes->colors_cmt);
  143.         attributes->colors_cmt = NULL;
  144.         }
  145.         if (attributes->pixels_cmt) {
  146.         free(attributes->pixels_cmt);
  147.         attributes->pixels_cmt = NULL;
  148.         }
  149.         if (attributes->pixels) {
  150.         free(attributes->pixels);
  151.         attributes->pixels = NULL;
  152.         }
  153.     }
  154.     if (attributes->valuemask & XpmReturnExtensions
  155.         && attributes->nextensions) {
  156.             XpmFreeExtensions(attributes->extensions, attributes->nextensions);
  157.         attributes->nextensions = 0;
  158.         attributes->extensions = NULL;
  159.     }
  160.     attributes->valuemask = 0;
  161.     }
  162. }
  163.  
  164.  
  165. /*
  166.  * Store into the XpmAttributes structure the required informations stored in
  167.  * the xpmInternAttrib structure.
  168.  */
  169.  
  170. xpmSetAttributes(attrib, attributes)
  171.     xpmInternAttrib *attrib;
  172.     XpmAttributes *attributes;
  173. {
  174.     if (attributes) {
  175.     if (attributes->valuemask & XpmReturnInfos) {
  176.         attributes->cpp = attrib->cpp;
  177.         attributes->ncolors = attrib->ncolors;
  178.         attributes->colorTable = attrib->colorTable;
  179.  
  180.         attrib->ncolors = 0;
  181.         attrib->colorTable = NULL;
  182.     }
  183.     attributes->width = attrib->width;
  184.     attributes->height = attrib->height;
  185.     attributes->valuemask |= XpmSize;
  186.     }
  187. }
  188.  
  189. #ifdef NEED_STRDUP
  190.  
  191. /*
  192.  * in case strdup is not provided by the system here is one
  193.  * which does the trick
  194.  */
  195. char *
  196. strdup (s1)
  197.     char *s1;
  198. {
  199.     char *s2;
  200.     int l = strlen(s1) + 1;
  201.     if (s2 = (char *) malloc(l))
  202.     strncpy(s2, s1, l);
  203.     return s2;
  204. }
  205.  
  206. #endif
  207.