| 1 |
/*--------------------------------------------------------------------- |
| 2 |
* $Date: 2009-08-18 15:23:20 $ $Revision: 1.2 $ |
| 3 |
*--------------------------------------------------------------------- |
| 4 |
* |
| 5 |
* |
| 6 |
* Copyright (c) 1992, Visual Edge Software Ltd. |
| 7 |
* |
| 8 |
* ALL RIGHTS RESERVED. Permission to use, copy, modify, and |
| 9 |
* distribute this software and its documentation for any purpose |
| 10 |
* and without fee is hereby granted, provided that the above |
| 11 |
* copyright notice appear in all copies and that both that |
| 12 |
* copyright notice and this permission notice appear in supporting |
| 13 |
* documentation, and that the name of Visual Edge Software not be |
| 14 |
* used in advertising or publicity pertaining to distribution of |
| 15 |
* the software without specific, written prior permission. The year |
| 16 |
* included in the notice is the year of the creation of the work. |
| 17 |
*-------------------------------------------------------------------*/ |
| 18 |
/* |
| 19 |
* Copyright 1990, 1991 GROUPE BULL |
| 20 |
* |
| 21 |
* Permission to use, copy, modify, and distribute this software and its |
| 22 |
* documentation for any purpose and without fee is hereby granted, provided |
| 23 |
* that the above copyright notice appear in all copies and that both that |
| 24 |
* copyright notice and this permission notice appear in supporting |
| 25 |
* documentation, and that the name of GROUPE BULL not be used in advertising |
| 26 |
* or publicity pertaining to distribution of the software without specific, |
| 27 |
* written prior permission. GROUPE BULL makes no representations about the |
| 28 |
* suitability of this software for any purpose. It is provided "as is" |
| 29 |
* without express or implied warranty. |
| 30 |
* |
| 31 |
* GROUPE BULL disclaims all warranties with regard to this software, |
| 32 |
* including all implied warranties of merchantability and fitness, |
| 33 |
* in no event shall GROUPE BULL be liable for any special, |
| 34 |
* indirect or consequential damages or any damages |
| 35 |
* whatsoever resulting from loss of use, data or profits, |
| 36 |
* whether in an action of contract, negligence or other tortious |
| 37 |
* action, arising out of or in connection with the use |
| 38 |
* or performance of this software. |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
/*****************************************************************************\ |
| 43 |
* misc.c: * |
| 44 |
* * |
| 45 |
* XPM library * |
| 46 |
* Miscellaneous utilities * |
| 47 |
* * |
| 48 |
* Developed by Arnaud Le Hors * |
| 49 |
|
| 50 |
.VERSION |
| 51 |
090818 last modif |
| 52 |
|
| 53 |
\*****************************************************************************/ |
| 54 |
|
| 55 |
#include "stdlib.h" |
| 56 |
#include "xpmP.h" |
| 57 |
|
| 58 |
/* |
| 59 |
* Free the computed color table |
| 60 |
*/ |
| 61 |
|
| 62 |
int xpmFreeColorTable(colorTable, ncolors) |
| 63 |
char ***colorTable; |
| 64 |
int ncolors; |
| 65 |
{ |
| 66 |
int a, b; |
| 67 |
char ***ct, **cts; |
| 68 |
|
| 69 |
if (colorTable) { |
| 70 |
for (a = 0, ct = colorTable; a < ncolors; a++, ct++) |
| 71 |
if (*ct) { |
| 72 |
for (b = 0, cts = *ct; b <= NKEYS; b++, cts++) |
| 73 |
if (*cts) |
| 74 |
free(*cts); |
| 75 |
free(*ct); |
| 76 |
} |
| 77 |
free(colorTable); |
| 78 |
} |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/* |
| 84 |
* Intialize the xpmInternAttrib pointers to Null to know |
| 85 |
* which ones must be freed later on. |
| 86 |
*/ |
| 87 |
|
| 88 |
int xpmInitInternAttrib(attrib) |
| 89 |
xpmInternAttrib *attrib; |
| 90 |
{ |
| 91 |
attrib->ncolors = 0; |
| 92 |
attrib->colorTable = NULL; |
| 93 |
attrib->pixelindex = NULL; |
| 94 |
attrib->xcolors = NULL; |
| 95 |
attrib->colorStrings = NULL; |
| 96 |
attrib->mask_pixel = UNDEF_PIXEL; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/* |
| 102 |
* Free the xpmInternAttrib pointers which have been allocated |
| 103 |
*/ |
| 104 |
|
| 105 |
int xpmFreeInternAttrib(attrib) |
| 106 |
xpmInternAttrib *attrib; |
| 107 |
{ |
| 108 |
unsigned int a, ncolors; |
| 109 |
char **sptr; |
| 110 |
|
| 111 |
if (attrib->colorTable) |
| 112 |
xpmFreeColorTable(attrib->colorTable, attrib->ncolors); |
| 113 |
if (attrib->pixelindex) |
| 114 |
free(attrib->pixelindex); |
| 115 |
if (attrib->xcolors) |
| 116 |
free(attrib->xcolors); |
| 117 |
if (attrib->colorStrings) { |
| 118 |
ncolors = attrib->ncolors; |
| 119 |
for (a = 0, sptr = attrib->colorStrings; a < ncolors; a++, sptr++) |
| 120 |
if (*sptr) |
| 121 |
free(*sptr); |
| 122 |
free(attrib->colorStrings); |
| 123 |
} |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/* |
| 129 |
* Free array of extensions |
| 130 |
*/ |
| 131 |
int XpmFreeExtensions(extensions, nextensions) |
| 132 |
XpmExtension *extensions; |
| 133 |
int nextensions; |
| 134 |
{ |
| 135 |
unsigned int i, j, nlines; |
| 136 |
XpmExtension *ext; |
| 137 |
char **sptr; |
| 138 |
|
| 139 |
for (i = 0, ext = extensions; i < nextensions; i++, ext++) { |
| 140 |
free(ext->name); |
| 141 |
nlines = ext->nlines; |
| 142 |
for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++) |
| 143 |
if (*sptr) |
| 144 |
free(*sptr); |
| 145 |
if (ext->lines) |
| 146 |
free(ext->lines); |
| 147 |
} |
| 148 |
free(extensions); |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/* |
| 154 |
* Return the XpmAttributes structure size |
| 155 |
*/ |
| 156 |
|
| 157 |
int XpmAttributesSize() |
| 158 |
{ |
| 159 |
return sizeof(XpmAttributes); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* |
| 164 |
* Free the XpmAttributes structure members |
| 165 |
* but the structure itself |
| 166 |
*/ |
| 167 |
|
| 168 |
int XpmFreeAttributes(attributes) |
| 169 |
XpmAttributes *attributes; |
| 170 |
{ |
| 171 |
if (attributes) { |
| 172 |
if (attributes->valuemask & XpmReturnPixels && attributes->pixels) { |
| 173 |
free(attributes->pixels); |
| 174 |
attributes->pixels = NULL; |
| 175 |
attributes->npixels = 0; |
| 176 |
} |
| 177 |
if (attributes->valuemask & XpmInfos) { |
| 178 |
if (attributes->colorTable) { |
| 179 |
xpmFreeColorTable(attributes->colorTable, attributes->ncolors); |
| 180 |
attributes->colorTable = NULL; |
| 181 |
attributes->ncolors = 0; |
| 182 |
} |
| 183 |
if (attributes->hints_cmt) { |
| 184 |
free(attributes->hints_cmt); |
| 185 |
attributes->hints_cmt = NULL; |
| 186 |
} |
| 187 |
if (attributes->colors_cmt) { |
| 188 |
free(attributes->colors_cmt); |
| 189 |
attributes->colors_cmt = NULL; |
| 190 |
} |
| 191 |
if (attributes->pixels_cmt) { |
| 192 |
free(attributes->pixels_cmt); |
| 193 |
attributes->pixels_cmt = NULL; |
| 194 |
} |
| 195 |
if (attributes->pixels) { |
| 196 |
free(attributes->pixels); |
| 197 |
attributes->pixels = NULL; |
| 198 |
} |
| 199 |
} |
| 200 |
if (attributes->valuemask & XpmReturnExtensions |
| 201 |
&& attributes->nextensions) { |
| 202 |
XpmFreeExtensions(attributes->extensions, attributes->nextensions); |
| 203 |
attributes->nextensions = 0; |
| 204 |
attributes->extensions = NULL; |
| 205 |
} |
| 206 |
attributes->valuemask = 0; |
| 207 |
} |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* |
| 213 |
* Store into the XpmAttributes structure the required informations stored in |
| 214 |
* the xpmInternAttrib structure. |
| 215 |
*/ |
| 216 |
|
| 217 |
int xpmSetAttributes(attrib, attributes) |
| 218 |
xpmInternAttrib *attrib; |
| 219 |
XpmAttributes *attributes; |
| 220 |
{ |
| 221 |
if (attributes) { |
| 222 |
if (attributes->valuemask & XpmReturnInfos) { |
| 223 |
attributes->cpp = attrib->cpp; |
| 224 |
attributes->ncolors = attrib->ncolors; |
| 225 |
attributes->colorTable = attrib->colorTable; |
| 226 |
|
| 227 |
attrib->ncolors = 0; |
| 228 |
attrib->colorTable = NULL; |
| 229 |
} |
| 230 |
attributes->width = attrib->width; |
| 231 |
attributes->height = attrib->height; |
| 232 |
attributes->valuemask |= XpmSize; |
| 233 |
} |
| 234 |
return 0; |
| 235 |
} |