]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/data.c
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 \*****************************************************************************/
36 /* Official version number */
37 static char *RCS_Version
= "$XpmVersion: 3.4k $";
39 /* Internal version number */
40 static char *RCS_Id
= "$Id$";
47 #define Getc(data, file) getc(file)
48 #define Ungetc(data, c, file) ungetc(c, file)
52 /* Visual Age cannot deal with old, non-ansi, code */
54 ParseComment(xpmData
* data
)
61 if (data
->type
== XPMBUFFER
) {
63 register unsigned int n
= 0;
70 /* skip the string beginning comment */
77 } while (c
== *s2
&& *s2
!= '\0' && c
);
80 /* this wasn't the beginning of a comment */
85 data
->Comment
[0] = *s
;
91 while (*s
!= *s2
&& c
) {
93 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
100 data
->CommentLength
= n
;
103 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
110 } while (c
== *s2
&& *s2
!= '\0' && c
);
112 /* this is the end of the comment */
119 FILE *file
= data
->stream
.file
;
121 register unsigned int n
= 0, a
;
128 /* skip the string beginning comment */
131 c
= Getc(data
, file
);
135 } while (c
== *s2
&& *s2
!= '\0' && c
!= EOF
);
138 /* this wasn't the beginning of a comment */
139 /* put characters back in the order that we got them */
140 for (a
= n
; a
> 0; a
--, s
--)
141 Ungetc(data
, *s
, file
);
145 data
->Comment
[0] = *s
;
151 while (*s
!= *s2
&& c
!= EOF
) {
152 c
= Getc(data
, file
);
153 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
160 data
->CommentLength
= n
;
162 c
= Getc(data
, file
);
163 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
170 } while (c
== *s2
&& *s2
!= '\0' && c
!= EOF
);
172 /* this is the end of the comment */
174 Ungetc(data
, *s
, file
);
182 * skip to the end of the current string and the beginning of the next one
185 /* Visual Age cannot deal with old, non-ansi, code */
186 int xpmNextString(xpmData
* data
)
194 data
->cptr
= (data
->stream
.data
)[++data
->line
];
195 else if (data
->type
== XPMBUFFER
) {
198 /* get to the end of the current string */
200 while ((c
= *data
->cptr
++) && c
!= data
->Eos
);
203 * then get to the beginning of the next string looking for possible
207 while ((c
= *data
->cptr
++) && c
!= data
->Bos
)
208 if (data
->Bcmt
&& c
== data
->Bcmt
[0])
210 } else if (data
->Bcmt
) { /* XPM2 natural */
211 while ((c
= *data
->cptr
++) == data
->Bcmt
[0])
217 FILE *file
= data
->stream
.file
;
219 /* get to the end of the current string */
221 while ((c
= Getc(data
, file
)) != data
->Eos
&& c
!= EOF
);
224 * then get to the beginning of the next string looking for possible
228 while ((c
= Getc(data
, file
)) != data
->Bos
&& c
!= EOF
)
229 if (data
->Bcmt
&& c
== data
->Bcmt
[0])
232 } else if (data
->Bcmt
) { /* XPM2 natural */
233 while ((c
= Getc(data
, file
)) == data
->Bcmt
[0])
235 Ungetc(data
, c
, file
);
243 * skip whitespace and return the following word
246 /* Visual Age cannot deal with old, non-ansi, code */
247 unsigned int xpmNextWord(
250 , unsigned int buflen
254 xpmNextWord(data
, buf
, buflen
)
260 register unsigned int n
= 0;
263 if (!data
->type
|| data
->type
== XPMBUFFER
) {
264 while (isspace(c
= *data
->cptr
) && c
!= data
->Eos
)
270 } while (!isspace(c
) && c
!= data
->Eos
&& n
< buflen
);
274 FILE *file
= data
->stream
.file
;
276 while ((c
= Getc(data
, file
)) != EOF
&& isspace(c
) && c
!= data
->Eos
);
277 while (!isspace(c
) && c
!= data
->Eos
&& c
!= EOF
&& n
< buflen
) {
280 c
= Getc(data
, file
);
282 Ungetc(data
, c
, file
);
288 * skip whitespace and compute the following unsigned int,
289 * returns 1 if one is found and 0 if not
292 /* Visual Age cannot deal with old, non-ansi, code */
294 xpmNextUI(xpmData
* data
, unsigned int* ui_return
)
297 xpmNextUI(data
, ui_return
)
299 unsigned int *ui_return
;
305 l
= xpmNextWord(data
, buf
, BUFSIZ
);
306 return xpmatoui(buf
, l
, ui_return
);
310 * return end of string - WARNING: malloc!
313 /* Visual Age cannot deal with old, non-ansi, code */
314 int xpmGetString(xpmData
* data
, char** sptr
, unsigned int* l
)
317 xpmGetString(data
, sptr
, l
)
323 unsigned int i
, n
= 0;
325 char *p
= NULL
, *q
, buf
[BUFSIZ
];
327 if (!data
->type
|| data
->type
== XPMBUFFER
) {
329 char *start
= data
->cptr
;
330 while ((c
= *data
->cptr
) && c
!= data
->Eos
)
332 n
= data
->cptr
- start
+ 1;
333 p
= (char *) XpmMalloc(n
);
335 return (XpmNoMemory
);
336 strncpy(p
, start
, n
);
337 if (data
->type
) /* XPMBUFFER */
341 FILE *file
= data
->stream
.file
;
343 if ((c
= Getc(data
, file
)) == EOF
)
344 return (XpmFileInvalid
);
348 p
= (char *) XpmMalloc(1);
349 while (c
!= data
->Eos
&& c
!= EOF
) {
351 /* get to the end of the buffer */
352 /* malloc needed memory */
353 q
= (char *) XpmRealloc(p
, n
+ i
);
356 return (XpmNoMemory
);
360 /* and copy what we already have */
368 c
= Getc(data
, file
);
372 return (XpmFileInvalid
);
375 /* malloc needed memory */
376 q
= (char *) XpmRealloc(p
, n
+ i
+ 1);
379 return (XpmNoMemory
);
383 /* and copy the buffer */
391 Ungetc(data
, c
, file
);
399 * get the current comment line
402 /* Visual Age cannot deal with old, non-ansi, code */
403 int xpmGetCmt(xpmData
* data
, char** cmt
)
413 else if (data
->CommentLength
) {
414 *cmt
= (char *) XpmMalloc(data
->CommentLength
+ 1);
415 strncpy(*cmt
, data
->Comment
, data
->CommentLength
);
416 (*cmt
)[data
->CommentLength
] = '\0';
417 data
->CommentLength
= 0;
423 xpmDataType xpmDataTypes
[] =
425 "", "!", "\n", '\0', '\n', "", "", "", "", /* Natural type */
426 "C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n",
427 "Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n",
431 NULL
, NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
439 /* Visual Age cannot deal with old, non-ansi, code */
440 int xpmParseHeader(xpmData
* data
)
453 data
->Bcmt
= data
->Ecmt
= NULL
;
454 l
= xpmNextWord(data
, buf
, BUFSIZ
);
455 if (l
== 7 && !strncmp("#define", buf
, 7)) {
456 /* this maybe an XPM 1 file */
459 l
= xpmNextWord(data
, buf
, BUFSIZ
);
461 return (XpmFileInvalid
);
463 ptr
= rindex(buf
, '_');
464 if (!ptr
|| strncmp("_format", ptr
, l
- (ptr
- buf
)))
465 return XpmFileInvalid
;
466 /* this is definitely an XPM 1 file */
468 n
= 1; /* handle XPM1 as mainly XPM2 C */
472 * skip the first word, get the second one, and see if this is
475 l
= xpmNextWord(data
, buf
, BUFSIZ
);
476 if ((l
== 3 && !strncmp("XPM", buf
, 3)) ||
477 (l
== 4 && !strncmp("XPM2", buf
, 4))) {
479 n
= 1; /* handle XPM as XPM2 C */
481 /* get the type key word */
482 l
= xpmNextWord(data
, buf
, BUFSIZ
);
485 * get infos about this type
487 while (xpmDataTypes
[n
].type
488 && strncmp(xpmDataTypes
[n
].type
, buf
, l
))
493 /* nope this is not an XPM file */
494 return XpmFileInvalid
;
496 if (xpmDataTypes
[n
].type
) {
497 if (n
== 0) { /* natural type */
498 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
499 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
500 xpmNextString(data
); /* skip the end of the headerline */
501 data
->Bos
= xpmDataTypes
[n
].Bos
;
502 data
->Eos
= xpmDataTypes
[n
].Eos
;
504 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
505 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
506 if (!data
->format
) { /* XPM 2 or 3 */
507 data
->Bos
= xpmDataTypes
[n
].Bos
;
509 /* get to the beginning of the first string */
511 data
->Eos
= xpmDataTypes
[n
].Eos
;
512 } else /* XPM 1 skip end of line */
516 /* we don't know about that type of XPM file... */
517 return XpmFileInvalid
;