2
* Copyright (C) 1989-95 GROUPE BULL
4
* Permission is hereby granted, free of charge, to any person obtaining a copy
5
* of this software and associated documentation files (the "Software"), to
6
* deal in the Software without restriction, including without limitation the
7
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
* sell copies of the Software, and to permit persons to whom the Software is
9
* furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
* GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
* Except as contained in this notice, the name of GROUPE BULL shall not be
22
* used in advertising or otherwise to promote the sale, use or other dealings
23
* in this Software without prior written authorization from GROUPE BULL.
26
/*****************************************************************************\
32
* Developed by Arnaud Le Hors *
33
\*****************************************************************************/
34
/* $XFree86: xc/extras/Xpm/lib/data.c,v 1.4 2002/01/07 19:40:49 dawes Exp $ */
38
/* Official version number */
39
static char *RCS_Version = "$XpmVersion: 3.4k $";
41
/* Internal version number */
42
static char *RCS_Id = "Id: xpm.shar,v 3.71 1998/03/19 19:47:14 lehors Exp $";
49
#define Getc(data, file) getc(file)
50
#define Ungetc(data, c, file) ungetc(c, file)
54
ParseComment(xpmData *data)
56
if (data->type == XPMBUFFER) {
58
register unsigned int n = 0;
65
/* skip the string beginning comment */
72
} while (c == *s2 && *s2 != '\0' && c);
75
/* this wasn't the beginning of a comment */
80
data->Comment[0] = *s;
86
while (*s != *s2 && c) {
88
if (n == XPMMAXCMTLEN - 1) { /* forget it */
95
data->CommentLength = n;
98
if (n == XPMMAXCMTLEN - 1) { /* forget it */
105
} while (c == *s2 && *s2 != '\0' && c);
107
/* this is the end of the comment */
114
FILE *file = data->stream.file;
116
register unsigned int n = 0, a;
123
/* skip the string beginning comment */
126
c = Getc(data, file);
130
} while (c == *s2 && *s2 != '\0' && c != EOF);
133
/* this wasn't the beginning of a comment */
134
/* put characters back in the order that we got them */
135
for (a = n; a > 0; a--, s--)
136
Ungetc(data, *s, file);
140
data->Comment[0] = *s;
146
while (*s != *s2 && c != EOF) {
147
c = Getc(data, file);
148
if (n == XPMMAXCMTLEN - 1) { /* forget it */
155
data->CommentLength = n;
157
c = Getc(data, file);
158
if (n == XPMMAXCMTLEN - 1) { /* forget it */
165
} while (c == *s2 && *s2 != '\0' && c != EOF);
167
/* this is the end of the comment */
169
Ungetc(data, *s, file);
177
* skip to the end of the current string and the beginning of the next one
184
data->cptr = (data->stream.data)[++data->line];
185
else if (data->type == XPMBUFFER) {
188
/* get to the end of the current string */
190
while ((c = *data->cptr++) && c != data->Eos);
193
* then get to the beginning of the next string looking for possible
197
while ((c = *data->cptr++) && c != data->Bos)
198
if (data->Bcmt && c == data->Bcmt[0])
200
} else if (data->Bcmt) { /* XPM2 natural */
201
while ((c = *data->cptr++) == data->Bcmt[0])
207
FILE *file = data->stream.file;
209
/* get to the end of the current string */
211
while ((c = Getc(data, file)) != data->Eos && c != EOF);
214
* then get to the beginning of the next string looking for possible
218
while ((c = Getc(data, file)) != data->Bos && c != EOF)
219
if (data->Bcmt && c == data->Bcmt[0])
222
} else if (data->Bcmt) { /* XPM2 natural */
223
while ((c = Getc(data, file)) == data->Bcmt[0])
225
Ungetc(data, c, file);
233
* skip whitespace and return the following word
236
xpmNextWord(data, buf, buflen)
241
register unsigned int n = 0;
244
if (!data->type || data->type == XPMBUFFER) {
245
while (isspace(c = *data->cptr) && c != data->Eos)
251
} while (!isspace(c) && c != data->Eos && n < buflen);
255
FILE *file = data->stream.file;
257
while ((c = Getc(data, file)) != EOF && isspace(c) && c != data->Eos);
258
while (!isspace(c) && c != data->Eos && c != EOF && n < buflen) {
261
c = Getc(data, file);
263
Ungetc(data, c, file);
269
* skip whitespace and compute the following unsigned int,
270
* returns 1 if one is found and 0 if not
273
xpmNextUI(data, ui_return)
275
unsigned int *ui_return;
280
l = xpmNextWord(data, buf, BUFSIZ);
281
return xpmatoui(buf, l, ui_return);
285
* return end of string - WARNING: malloc!
288
xpmGetString(data, sptr, l)
293
unsigned int i, n = 0;
295
char *p = NULL, *q, buf[BUFSIZ];
297
if (!data->type || data->type == XPMBUFFER) {
299
char *start = data->cptr;
300
while ((c = *data->cptr) && c != data->Eos)
302
n = data->cptr - start + 1;
303
p = (char *) XpmMalloc(n);
305
return (XpmNoMemory);
306
strncpy(p, start, n);
307
if (data->type) /* XPMBUFFER */
311
FILE *file = data->stream.file;
313
if ((c = Getc(data, file)) == EOF)
314
return (XpmFileInvalid);
318
p = (char *) XpmMalloc(1);
319
while (c != data->Eos && c != EOF) {
321
/* get to the end of the buffer */
322
/* malloc needed memory */
323
q = (char *) XpmRealloc(p, n + i);
326
return (XpmNoMemory);
330
/* and copy what we already have */
338
c = Getc(data, file);
342
return (XpmFileInvalid);
345
/* malloc needed memory */
346
q = (char *) XpmRealloc(p, n + i + 1);
349
return (XpmNoMemory);
353
/* and copy the buffer */
361
Ungetc(data, c, file);
369
* get the current comment line
378
else if (data->CommentLength) {
379
*cmt = (char *) XpmMalloc(data->CommentLength + 1);
380
strncpy(*cmt, data->Comment, data->CommentLength);
381
(*cmt)[data->CommentLength] = '\0';
382
data->CommentLength = 0;
388
xpmDataType xpmDataTypes[] =
390
{"", "!", "\n", '\0', '\n', "", "", "", ""}, /* Natural type */
391
{"C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n"},
392
{"Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n"},
393
{NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL}
409
data->Bcmt = data->Ecmt = NULL;
410
l = xpmNextWord(data, buf, BUFSIZ);
411
if (l == 7 && !strncmp("#define", buf, 7)) {
412
/* this maybe an XPM 1 file */
415
l = xpmNextWord(data, buf, BUFSIZ);
417
return (XpmFileInvalid);
419
ptr = rindex(buf, '_');
420
if (!ptr || strncmp("_format", ptr, l - (ptr - buf)))
421
return XpmFileInvalid;
422
/* this is definitely an XPM 1 file */
424
n = 1; /* handle XPM1 as mainly XPM2 C */
428
* skip the first word, get the second one, and see if this is
431
l = xpmNextWord(data, buf, BUFSIZ);
432
if ((l == 3 && !strncmp("XPM", buf, 3)) ||
433
(l == 4 && !strncmp("XPM2", buf, 4))) {
435
n = 1; /* handle XPM as XPM2 C */
437
/* get the type key word */
438
l = xpmNextWord(data, buf, BUFSIZ);
441
* get infos about this type
443
while (xpmDataTypes[n].type
444
&& strncmp(xpmDataTypes[n].type, buf, l))
449
/* nope this is not an XPM file */
450
return XpmFileInvalid;
452
if (xpmDataTypes[n].type) {
453
if (n == 0) { /* natural type */
454
data->Bcmt = xpmDataTypes[n].Bcmt;
455
data->Ecmt = xpmDataTypes[n].Ecmt;
456
xpmNextString(data); /* skip the end of the headerline */
457
data->Bos = xpmDataTypes[n].Bos;
458
data->Eos = xpmDataTypes[n].Eos;
460
data->Bcmt = xpmDataTypes[n].Bcmt;
461
data->Ecmt = xpmDataTypes[n].Ecmt;
462
if (!data->format) { /* XPM 2 or 3 */
463
data->Bos = xpmDataTypes[n].Bos;
465
/* get to the beginning of the first string */
467
data->Eos = xpmDataTypes[n].Eos;
468
} else /* XPM 1 skip end of line */
472
/* we don't know about that type of XPM file... */
473
return XpmFileInvalid;