| 1 |
/*--------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* |
| 4 |
* Copyright (c) 1992, Visual Edge Software Ltd. |
| 5 |
* |
| 6 |
* ALL RIGHTS RESERVED. Permission to use, copy, modify, and |
| 7 |
* distribute this software and its documentation for any purpose |
| 8 |
* and without fee is hereby granted, provided that the above |
| 9 |
* copyright notice appear in all copies and that both that |
| 10 |
* copyright notice and this permission notice appear in supporting |
| 11 |
* documentation, and that the name of Visual Edge Software not be |
| 12 |
* used in advertising or publicity pertaining to distribution of |
| 13 |
* the software without specific, written prior permission. The year |
| 14 |
* included in the notice is the year of the creation of the work. |
| 15 |
*-------------------------------------------------------------------*/ |
| 16 |
/* |
| 17 |
* Copyright 1990, 1991 GROUPE BULL |
| 18 |
* |
| 19 |
* Permission to use, copy, modify, and distribute this software and its |
| 20 |
* documentation for any purpose and without fee is hereby granted, provided |
| 21 |
* that the above copyright notice appear in all copies and that both that |
| 22 |
* copyright notice and this permission notice appear in supporting |
| 23 |
* documentation, and that the name of GROUPE BULL not be used in advertising |
| 24 |
* or publicity pertaining to distribution of the software without specific, |
| 25 |
* written prior permission. GROUPE BULL makes no representations about the |
| 26 |
* suitability of this software for any purpose. It is provided "as is" |
| 27 |
* without express or implied warranty. |
| 28 |
* |
| 29 |
* GROUPE BULL disclaims all warranties with regard to this software, |
| 30 |
* including all implied warranties of merchantability and fitness, |
| 31 |
* in no event shall GROUPE BULL be liable for any special, |
| 32 |
* indirect or consequential damages or any damages |
| 33 |
* whatsoever resulting from loss of use, data or profits, |
| 34 |
* whether in an action of contract, negligence or other tortious |
| 35 |
* action, arising out of or in connection with the use |
| 36 |
* or performance of this software. |
| 37 |
* |
| 38 |
|
| 39 |
.VERSION |
| 40 |
090818 last modif |
| 41 |
|
| 42 |
*/ |
| 43 |
|
| 44 |
/*****************************************************************************\ |
| 45 |
* parse.c: * |
| 46 |
* * |
| 47 |
* XPM library * |
| 48 |
* Parse an XPM file or array and store the found informations * |
| 49 |
* in an an xpmInternAttrib structure which is returned. * |
| 50 |
* * |
| 51 |
* Developed by Arnaud Le Hors * |
| 52 |
\*****************************************************************************/ |
| 53 |
|
| 54 |
|
| 55 |
#include "xpmP.h" |
| 56 |
#ifdef VMS |
| 57 |
#include "sys$library:ctype.h" |
| 58 |
#else |
| 59 |
#include <ctype.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#include <stdlib.h> |
| 63 |
|
| 64 |
extern unsigned int atoui(); |
| 65 |
extern int xpmGetString(); |
| 66 |
|
| 67 |
|
| 68 |
LFUNC(ParseValues, int, (xpmData *data, unsigned int *width, |
| 69 |
unsigned int *height, unsigned int *ncolors, |
| 70 |
unsigned int *cpp, unsigned int *x_hotspot, |
| 71 |
unsigned int *y_hotspot, unsigned int *hotspot, |
| 72 |
unsigned int *extensions)); |
| 73 |
|
| 74 |
LFUNC(ParseColors, int, (xpmData *data, unsigned int ncolors, unsigned int cpp, |
| 75 |
char ****colorTablePtr, xpmHashTable *hashtable)); |
| 76 |
|
| 77 |
LFUNC(ParsePixels, int, (xpmData *data, unsigned int width, |
| 78 |
unsigned int height, unsigned int ncolors, |
| 79 |
unsigned int cpp, char ***colorTable, |
| 80 |
xpmHashTable *hashtable, unsigned int **pixels)); |
| 81 |
|
| 82 |
LFUNC(ParseExtensions, int, (xpmData *data, XpmExtension **extensions, |
| 83 |
unsigned int *nextensions)); |
| 84 |
|
| 85 |
char *xpmColorKeys[] = |
| 86 |
{ |
| 87 |
"s", /* key #1: symbol */ |
| 88 |
"m", /* key #2: mono visual */ |
| 89 |
"g4", /* key #3: 4 grays visual */ |
| 90 |
"g", /* key #4: gray visual */ |
| 91 |
"c", /* key #5: color visual */ |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
/* function call in case of error, frees only locally allocated variables */ |
| 96 |
#undef RETURN |
| 97 |
#define RETURN(status) \ |
| 98 |
{ if (colorTable) xpmFreeColorTable(colorTable, ncolors); \ |
| 99 |
if (pixelindex) free(pixelindex); \ |
| 100 |
if (hints_cmt) free(hints_cmt); \ |
| 101 |
if (colors_cmt) free(colors_cmt); \ |
| 102 |
if (pixels_cmt) free(pixels_cmt); \ |
| 103 |
return(status); } |
| 104 |
|
| 105 |
/* |
| 106 |
* This function parses an Xpm file or data and store the found informations |
| 107 |
* in an an xpmInternAttrib structure which is returned. |
| 108 |
*/ |
| 109 |
int |
| 110 |
xpmParseData(data, attrib_return, attributes) |
| 111 |
xpmData *data; |
| 112 |
xpmInternAttrib *attrib_return; |
| 113 |
XpmAttributes *attributes; |
| 114 |
{ |
| 115 |
/* variables to return */ |
| 116 |
unsigned int width, height, ncolors, cpp; |
| 117 |
unsigned int x_hotspot, y_hotspot, hotspot = 0, extensions = 0; |
| 118 |
char ***colorTable = NULL; |
| 119 |
unsigned int *pixelindex = NULL; |
| 120 |
char *hints_cmt = NULL; |
| 121 |
char *colors_cmt = NULL; |
| 122 |
char *pixels_cmt = NULL; |
| 123 |
|
| 124 |
int ErrorStatus; |
| 125 |
xpmHashTable hashtable; |
| 126 |
|
| 127 |
/* |
| 128 |
* read values |
| 129 |
*/ |
| 130 |
ErrorStatus = ParseValues(data, &width, &height, &ncolors, &cpp, |
| 131 |
&x_hotspot, &y_hotspot, &hotspot, &extensions); |
| 132 |
if (ErrorStatus != XpmSuccess) |
| 133 |
return(ErrorStatus); |
| 134 |
|
| 135 |
/* |
| 136 |
* store the hints comment line |
| 137 |
*/ |
| 138 |
if (attributes && (attributes->valuemask & XpmReturnInfos)) |
| 139 |
xpmGetCmt(data, &hints_cmt); |
| 140 |
|
| 141 |
/* |
| 142 |
* init the hastable |
| 143 |
*/ |
| 144 |
if (USE_HASHTABLE) |
| 145 |
xpmHashTableInit(&hashtable); |
| 146 |
|
| 147 |
/* |
| 148 |
* read colors |
| 149 |
*/ |
| 150 |
ErrorStatus = ParseColors(data, ncolors, cpp, &colorTable, &hashtable); |
| 151 |
if (ErrorStatus != XpmSuccess) |
| 152 |
RETURN(ErrorStatus); |
| 153 |
|
| 154 |
/* |
| 155 |
* store the colors comment line |
| 156 |
*/ |
| 157 |
if (attributes && (attributes->valuemask & XpmReturnInfos)) |
| 158 |
xpmGetCmt(data, &colors_cmt); |
| 159 |
|
| 160 |
/* |
| 161 |
* read pixels and index them on color number |
| 162 |
*/ |
| 163 |
ErrorStatus = ParsePixels(data, width, height, ncolors, cpp, colorTable, |
| 164 |
&hashtable, &pixelindex); |
| 165 |
|
| 166 |
/* |
| 167 |
* free the hastable |
| 168 |
*/ |
| 169 |
if (USE_HASHTABLE) |
| 170 |
xpmHashTableFree(&hashtable); |
| 171 |
|
| 172 |
if (ErrorStatus != XpmSuccess) |
| 173 |
RETURN(ErrorStatus); |
| 174 |
|
| 175 |
/* |
| 176 |
* store the pixels comment line |
| 177 |
*/ |
| 178 |
if (attributes && (attributes->valuemask & XpmReturnInfos)) |
| 179 |
xpmGetCmt(data, &pixels_cmt); |
| 180 |
|
| 181 |
/* |
| 182 |
* parse extensions |
| 183 |
*/ |
| 184 |
if (extensions && attributes |
| 185 |
&& (attributes->valuemask & XpmReturnExtensions)) { |
| 186 |
ErrorStatus = ParseExtensions(data, &attributes->extensions, |
| 187 |
&attributes->nextensions); |
| 188 |
if (ErrorStatus != XpmSuccess) |
| 189 |
RETURN(ErrorStatus); |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* store found informations in the xpmInternAttrib structure |
| 194 |
*/ |
| 195 |
attrib_return->width = width; |
| 196 |
attrib_return->height = height; |
| 197 |
attrib_return->cpp = cpp; |
| 198 |
attrib_return->ncolors = ncolors; |
| 199 |
attrib_return->colorTable = colorTable; |
| 200 |
attrib_return->pixelindex = pixelindex; |
| 201 |
|
| 202 |
if (attributes) { |
| 203 |
if (attributes->valuemask & XpmReturnInfos) { |
| 204 |
attributes->hints_cmt = hints_cmt; |
| 205 |
attributes->colors_cmt = colors_cmt; |
| 206 |
attributes->pixels_cmt = pixels_cmt; |
| 207 |
} |
| 208 |
if (hotspot) { |
| 209 |
attributes->x_hotspot = x_hotspot; |
| 210 |
attributes->y_hotspot = y_hotspot; |
| 211 |
attributes->valuemask |= XpmHotspot; |
| 212 |
} |
| 213 |
} |
| 214 |
return (XpmSuccess); |
| 215 |
} |
| 216 |
|
| 217 |
static int |
| 218 |
ParseValues(data, width, height, ncolors, cpp, |
| 219 |
x_hotspot, y_hotspot, hotspot, extensions) |
| 220 |
xpmData *data; |
| 221 |
unsigned int *width, *height, *ncolors, *cpp; |
| 222 |
unsigned int *x_hotspot, *y_hotspot, *hotspot; |
| 223 |
unsigned int *extensions; |
| 224 |
{ |
| 225 |
unsigned int l; |
| 226 |
char buf[BUFSIZ]; |
| 227 |
|
| 228 |
/* |
| 229 |
* read values: width, height, ncolors, chars_per_pixel |
| 230 |
*/ |
| 231 |
if (!(xpmNextUI(data, width) && xpmNextUI(data, height) |
| 232 |
&& xpmNextUI(data, ncolors) && xpmNextUI(data, cpp))) |
| 233 |
return(XpmFileInvalid); |
| 234 |
|
| 235 |
/* |
| 236 |
* read optional information (hotspot and/or XPMEXT) if any |
| 237 |
*/ |
| 238 |
l = xpmNextWord(data, buf); |
| 239 |
if (l) { |
| 240 |
*extensions = l == 6 && !strncmp("XPMEXT", buf, 6); |
| 241 |
if (*extensions) |
| 242 |
*hotspot = xpmNextUI(data, x_hotspot) |
| 243 |
&& xpmNextUI(data, y_hotspot); |
| 244 |
else { |
| 245 |
*hotspot = atoui(buf, l, x_hotspot) && xpmNextUI(data, y_hotspot); |
| 246 |
l = xpmNextWord(data, buf); |
| 247 |
*extensions = l == 6 && !strncmp("XPMEXT", buf, 6); |
| 248 |
} |
| 249 |
} |
| 250 |
return (XpmSuccess); |
| 251 |
} |
| 252 |
|
| 253 |
static int |
| 254 |
ParseColors(data, ncolors, cpp, colorTablePtr, hashtable) |
| 255 |
xpmData *data; |
| 256 |
unsigned int ncolors; |
| 257 |
unsigned int cpp; |
| 258 |
char ****colorTablePtr; /* Jee, that's something! */ |
| 259 |
xpmHashTable *hashtable; |
| 260 |
{ |
| 261 |
unsigned int key, l, a, b; |
| 262 |
unsigned int curkey; /* current color key */ |
| 263 |
unsigned int lastwaskey; /* key read */ |
| 264 |
char buf[BUFSIZ]; |
| 265 |
char curbuf[BUFSIZ]; /* current buffer */ |
| 266 |
char ***ct, **cts, **sptr, *s; |
| 267 |
char ***colorTable; |
| 268 |
|
| 269 |
colorTable = (char ***) calloc(ncolors, sizeof(char **)); |
| 270 |
if (!colorTable) |
| 271 |
return(XpmNoMemory); |
| 272 |
|
| 273 |
for (a = 0, ct = colorTable; a < ncolors; a++, ct++) { |
| 274 |
xpmNextString(data); /* skip the line */ |
| 275 |
cts = *ct = (char **) calloc((NKEYS + 1), sizeof(char *)); |
| 276 |
if (!cts) { |
| 277 |
xpmFreeColorTable(colorTable, ncolors); |
| 278 |
return(XpmNoMemory); |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* read pixel value |
| 283 |
*/ |
| 284 |
*cts = (char *) malloc((size_t)(cpp + 1)); /* + 1 for null terminated */ |
| 285 |
if (!*cts) { |
| 286 |
xpmFreeColorTable(colorTable, ncolors); |
| 287 |
return(XpmNoMemory); |
| 288 |
} |
| 289 |
for (b = 0, s = *cts; b < cpp; b++, s++) |
| 290 |
*s = xpmGetC(data); |
| 291 |
*s = '\0'; |
| 292 |
|
| 293 |
/* |
| 294 |
* tore the string in the hashtable with its color index number |
| 295 |
*/ |
| 296 |
if (USE_HASHTABLE) |
| 297 |
xpmHashIntern(hashtable, *cts, HashAtomData(a)); |
| 298 |
|
| 299 |
/* |
| 300 |
* read color keys and values |
| 301 |
*/ |
| 302 |
curkey = 0; |
| 303 |
lastwaskey = 0; |
| 304 |
while ((l = xpmNextWord(data, buf))) { |
| 305 |
if (!lastwaskey) { |
| 306 |
for (key = 0, sptr = xpmColorKeys; key < NKEYS; key++, sptr++) |
| 307 |
if ((strlen(*sptr) == l) && (!strncmp(*sptr, buf, l))) |
| 308 |
break; |
| 309 |
} |
| 310 |
if (!lastwaskey && key < NKEYS) { /* open new key */ |
| 311 |
if (curkey) { /* flush string */ |
| 312 |
s = cts[curkey] = (char *) malloc((size_t)(strlen(curbuf) + 1)); |
| 313 |
if (!s) { |
| 314 |
xpmFreeColorTable(colorTable, ncolors); |
| 315 |
return(XpmNoMemory); |
| 316 |
} |
| 317 |
strcpy(s, curbuf); |
| 318 |
} |
| 319 |
curkey = key + 1; /* set new key */ |
| 320 |
*curbuf = '\0'; /* reset curbuf */ |
| 321 |
lastwaskey = 1; |
| 322 |
} else { |
| 323 |
if (!curkey) { /* key without value */ |
| 324 |
xpmFreeColorTable(colorTable, ncolors); |
| 325 |
return(XpmFileInvalid); |
| 326 |
} |
| 327 |
if (!lastwaskey) |
| 328 |
strcat(curbuf, " "); /* append space */ |
| 329 |
buf[l] = '\0'; |
| 330 |
strcat(curbuf, buf); /* append buf */ |
| 331 |
lastwaskey = 0; |
| 332 |
} |
| 333 |
} |
| 334 |
if (!curkey) { /* key without value */ |
| 335 |
xpmFreeColorTable(colorTable, ncolors); |
| 336 |
return(XpmFileInvalid); |
| 337 |
} |
| 338 |
s = cts[curkey] = (char *) malloc((size_t)(strlen(curbuf) + 1)); |
| 339 |
if (!s) { |
| 340 |
xpmFreeColorTable(colorTable, ncolors); |
| 341 |
return(XpmNoMemory); |
| 342 |
} |
| 343 |
strcpy(s, curbuf); |
| 344 |
} |
| 345 |
*colorTablePtr = colorTable; |
| 346 |
return(XpmSuccess); |
| 347 |
} |
| 348 |
|
| 349 |
static int |
| 350 |
ParsePixels(data, width, height, ncolors, cpp, colorTable, hashtable, pixels) |
| 351 |
xpmData *data; |
| 352 |
unsigned int width; |
| 353 |
unsigned int height; |
| 354 |
unsigned int ncolors; |
| 355 |
unsigned int cpp; |
| 356 |
char ***colorTable; |
| 357 |
xpmHashTable *hashtable; |
| 358 |
unsigned int **pixels; |
| 359 |
{ |
| 360 |
unsigned int *iptr, *iptr2; |
| 361 |
char *s; |
| 362 |
unsigned int a, x, y; |
| 363 |
char buf[BUFSIZ]; |
| 364 |
|
| 365 |
iptr2 = (unsigned int *) malloc((size_t)(sizeof(unsigned int) * width * height)); |
| 366 |
if (!iptr2) |
| 367 |
return(XpmNoMemory); |
| 368 |
|
| 369 |
buf[cpp] = '\0'; |
| 370 |
iptr = iptr2; |
| 371 |
if (USE_HASHTABLE) { |
| 372 |
xpmHashAtom *slot; |
| 373 |
|
| 374 |
for (y = 0; y < height; y++) { |
| 375 |
xpmNextString(data); |
| 376 |
for (x = 0; x < width; x++, iptr++) { |
| 377 |
for (a = 0, s = buf; a < cpp; a++, s++) |
| 378 |
*s = xpmGetC(data); |
| 379 |
slot = xpmHashSlot(hashtable, buf); |
| 380 |
if (!*slot) { /* no color matches */ |
| 381 |
free(iptr2); |
| 382 |
return(XpmFileInvalid); |
| 383 |
} |
| 384 |
*iptr = HashColorIndex(slot); |
| 385 |
} |
| 386 |
} |
| 387 |
} else { |
| 388 |
for (y = 0; y < height; y++) { |
| 389 |
xpmNextString(data); |
| 390 |
for (x = 0; x < width; x++, iptr++) { |
| 391 |
for (a = 0, s = buf; a < cpp; a++, s++) |
| 392 |
*s = xpmGetC(data); |
| 393 |
for (a = 0; a < ncolors; a++) |
| 394 |
if (!strcmp(colorTable[a][0], buf)) |
| 395 |
break; |
| 396 |
if (a == ncolors) { /* no color matches */ |
| 397 |
free(iptr2); |
| 398 |
return(XpmFileInvalid); |
| 399 |
} |
| 400 |
*iptr = a; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
*pixels = iptr2; |
| 405 |
return (XpmSuccess); |
| 406 |
} |
| 407 |
|
| 408 |
static int |
| 409 |
ParseExtensions(data, extensions, nextensions) |
| 410 |
xpmData *data; |
| 411 |
XpmExtension **extensions; |
| 412 |
unsigned int *nextensions; |
| 413 |
{ |
| 414 |
XpmExtension *exts = NULL, *ext; |
| 415 |
unsigned int num = 0; |
| 416 |
unsigned int nlines, a, l, notstart, notend; |
| 417 |
int status; |
| 418 |
char *string, *s, *s2, **sp; |
| 419 |
|
| 420 |
notend = 0; |
| 421 |
|
| 422 |
xpmNextString(data); |
| 423 |
exts = (XpmExtension *) malloc((size_t)(sizeof(XpmExtension))); |
| 424 |
/* get the whole string */ |
| 425 |
status = xpmGetString(data, &string, &l); |
| 426 |
if (status != XpmSuccess) { |
| 427 |
free(exts); |
| 428 |
return(status); |
| 429 |
} |
| 430 |
/* look for the key word XPMEXT, skip lines before this */ |
| 431 |
while ((notstart = strncmp("XPMEXT", string, 6)) |
| 432 |
&& (notend = strncmp("XPMENDEXT", string, 9))) { |
| 433 |
free(string); |
| 434 |
xpmNextString(data); |
| 435 |
status = xpmGetString(data, &string, &l); |
| 436 |
if (status != XpmSuccess) { |
| 437 |
free(exts); |
| 438 |
return(status); |
| 439 |
} |
| 440 |
} |
| 441 |
while (!notstart && notend) { |
| 442 |
/* there starts an extension */ |
| 443 |
ext = (XpmExtension *) realloc(exts, (num + 1) * sizeof(XpmExtension)); |
| 444 |
if (!ext) { |
| 445 |
free(exts); |
| 446 |
return(XpmNoMemory); |
| 447 |
} |
| 448 |
exts = ext; |
| 449 |
ext += num; |
| 450 |
/* skip whitespace and store its name */ |
| 451 |
s2 = s = string + 6; |
| 452 |
while (isspace(*s2)) |
| 453 |
s2++; |
| 454 |
a = s2 - s; |
| 455 |
ext->name = (char *) malloc((size_t)(l - a - 6)); |
| 456 |
if (!ext->name) { |
| 457 |
free(exts); |
| 458 |
return(XpmNoMemory); |
| 459 |
} |
| 460 |
strncpy(ext->name, s + a, l - a - 6); |
| 461 |
free(string); |
| 462 |
/* now store the related lines */ |
| 463 |
xpmNextString(data); |
| 464 |
status = xpmGetString(data, &string, &l); |
| 465 |
if (status != XpmSuccess) { |
| 466 |
free(exts); |
| 467 |
return(status); |
| 468 |
} |
| 469 |
ext->lines = (char **) malloc((size_t)(sizeof(char *))); |
| 470 |
nlines = 0; |
| 471 |
while ((notstart = strncmp("XPMEXT", string, 6)) |
| 472 |
&& (notend = strncmp("XPMENDEXT", string, 9))) { |
| 473 |
sp = (char **) realloc(ext->lines, (nlines + 1) * sizeof(char *)); |
| 474 |
if (!sp) { |
| 475 |
free(exts); |
| 476 |
return(XpmNoMemory); |
| 477 |
} |
| 478 |
ext->lines = sp; |
| 479 |
ext->lines[nlines] = string; |
| 480 |
nlines++; |
| 481 |
xpmNextString(data); |
| 482 |
status = xpmGetString(data, &string, &l); |
| 483 |
if (status != XpmSuccess) { |
| 484 |
free(exts); |
| 485 |
return(status); |
| 486 |
} |
| 487 |
} |
| 488 |
if (!nlines) { |
| 489 |
free(ext->lines); |
| 490 |
ext->lines = NULL; |
| 491 |
} |
| 492 |
ext->nlines = nlines; |
| 493 |
num++; |
| 494 |
} |
| 495 |
if (!num) { |
| 496 |
free(string); |
| 497 |
free(exts); |
| 498 |
exts = NULL; |
| 499 |
} |
| 500 |
if (!notend) |
| 501 |
free(string); |
| 502 |
*nextensions = num; |
| 503 |
*extensions = exts; |
| 504 |
return(XpmSuccess); |
| 505 |
} |