]>
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 \*****************************************************************************/
41 #define Getc(data, file) getc(file)
42 #define Ungetc(data, c, file) ungetc(c, file)
46 /* Visual Age cannot deal with old, non-ansi, code */
48 ParseComment(xpmData
* data
)
55 if (data
->type
== XPMBUFFER
) {
57 register unsigned int n
= 0;
64 /* skip the string beginning comment */
71 } while (c
== *s2
&& *s2
!= '\0' && c
);
74 /* this wasn't the beginning of a comment */
79 data
->Comment
[0] = *s
;
85 while (*s
!= *s2
&& c
) {
87 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
94 data
->CommentLength
= n
;
97 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
104 } while (c
== *s2
&& *s2
!= '\0' && c
);
106 /* this is the end of the comment */
113 FILE *file
= data
->stream
.file
;
115 register unsigned int n
= 0, a
;
122 /* skip the string beginning comment */
125 c
= Getc(data
, file
);
129 } while (c
== *s2
&& *s2
!= '\0' && c
!= EOF
);
132 /* this wasn't the beginning of a comment */
133 /* put characters back in the order that we got them */
134 for (a
= n
; a
> 0; a
--, s
--)
135 Ungetc(data
, *s
, file
);
139 data
->Comment
[0] = *s
;
145 while (*s
!= *s2
&& c
!= EOF
) {
146 c
= Getc(data
, file
);
147 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
154 data
->CommentLength
= n
;
156 c
= Getc(data
, file
);
157 if (n
== XPMMAXCMTLEN
- 1) { /* forget it */
164 } while (c
== *s2
&& *s2
!= '\0' && c
!= EOF
);
166 /* this is the end of the comment */
168 Ungetc(data
, *s
, file
);
176 * skip to the end of the current string and the beginning of the next one
179 /* Visual Age cannot deal with old, non-ansi, code */
180 int xpmNextString(xpmData
* data
)
188 data
->cptr
= (data
->stream
.data
)[++data
->line
];
189 else if (data
->type
== XPMBUFFER
) {
192 /* get to the end of the current string */
194 while ((c
= *data
->cptr
++) && c
!= data
->Eos
);
197 * then get to the beginning of the next string looking for possible
201 while ((c
= *data
->cptr
++) && c
!= data
->Bos
)
202 if (data
->Bcmt
&& c
== data
->Bcmt
[0])
204 } else if (data
->Bcmt
) { /* XPM2 natural */
205 while ((c
= *data
->cptr
++) == data
->Bcmt
[0])
211 FILE *file
= data
->stream
.file
;
213 /* get to the end of the current string */
215 while ((c
= Getc(data
, file
)) != data
->Eos
&& c
!= EOF
);
218 * then get to the beginning of the next string looking for possible
222 while ((c
= Getc(data
, file
)) != data
->Bos
&& c
!= EOF
)
223 if (data
->Bcmt
&& c
== data
->Bcmt
[0])
226 } else if (data
->Bcmt
) { /* XPM2 natural */
227 while ((c
= Getc(data
, file
)) == data
->Bcmt
[0])
229 Ungetc(data
, c
, file
);
237 * skip whitespace and return the following word
240 /* Visual Age cannot deal with old, non-ansi, code */
241 unsigned int xpmNextWord(
244 , unsigned int buflen
248 xpmNextWord(data
, buf
, buflen
)
254 register unsigned int n
= 0;
257 if (!data
->type
|| data
->type
== XPMBUFFER
) {
258 while (isspace(c
= *data
->cptr
) && c
!= data
->Eos
)
264 } while (!isspace(c
) && c
!= data
->Eos
&& n
< buflen
);
268 FILE *file
= data
->stream
.file
;
270 while ((c
= Getc(data
, file
)) != EOF
&& isspace(c
) && c
!= data
->Eos
);
271 while (!isspace(c
) && c
!= data
->Eos
&& c
!= EOF
&& n
< buflen
) {
274 c
= Getc(data
, file
);
276 Ungetc(data
, c
, file
);
282 * skip whitespace and compute the following unsigned int,
283 * returns 1 if one is found and 0 if not
286 /* Visual Age cannot deal with old, non-ansi, code */
288 xpmNextUI(xpmData
* data
, unsigned int* ui_return
)
291 xpmNextUI(data
, ui_return
)
293 unsigned int *ui_return
;
299 l
= xpmNextWord(data
, buf
, BUFSIZ
);
300 return xpmatoui(buf
, l
, ui_return
);
304 * return end of string - WARNING: malloc!
307 /* Visual Age cannot deal with old, non-ansi, code */
308 int xpmGetString(xpmData
* data
, char** sptr
, unsigned int* l
)
311 xpmGetString(data
, sptr
, l
)
317 unsigned int i
, n
= 0;
319 char *p
= NULL
, *q
, buf
[BUFSIZ
];
321 if (!data
->type
|| data
->type
== XPMBUFFER
) {
323 char *start
= data
->cptr
;
324 while ((c
= *data
->cptr
) && c
!= data
->Eos
)
326 n
= data
->cptr
- start
+ 1;
327 p
= (char *) XpmMalloc(n
);
329 return (XpmNoMemory
);
330 strncpy(p
, start
, n
);
331 if (data
->type
) /* XPMBUFFER */
335 FILE *file
= data
->stream
.file
;
337 if ((c
= Getc(data
, file
)) == EOF
)
338 return (XpmFileInvalid
);
342 p
= (char *) XpmMalloc(1);
343 while (c
!= data
->Eos
&& c
!= EOF
) {
345 /* get to the end of the buffer */
346 /* malloc needed memory */
347 q
= (char *) XpmRealloc(p
, n
+ i
);
350 return (XpmNoMemory
);
354 /* and copy what we already have */
362 c
= Getc(data
, file
);
366 return (XpmFileInvalid
);
369 /* malloc needed memory */
370 q
= (char *) XpmRealloc(p
, n
+ i
+ 1);
373 return (XpmNoMemory
);
377 /* and copy the buffer */
385 Ungetc(data
, c
, file
);
393 * get the current comment line
396 /* Visual Age cannot deal with old, non-ansi, code */
397 int xpmGetCmt(xpmData
* data
, char** cmt
)
407 else if (data
->CommentLength
) {
408 *cmt
= (char *) XpmMalloc(data
->CommentLength
+ 1);
409 strncpy(*cmt
, data
->Comment
, data
->CommentLength
);
410 (*cmt
)[data
->CommentLength
] = '\0';
411 data
->CommentLength
= 0;
417 xpmDataType xpmDataTypes
[] =
419 "", "!", "\n", '\0', '\n', "", "", "", "", /* Natural type */
420 "C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n",
421 "Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n",
425 NULL
, NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
433 /* Visual Age cannot deal with old, non-ansi, code */
434 int xpmParseHeader(xpmData
* data
)
447 data
->Bcmt
= data
->Ecmt
= NULL
;
448 l
= xpmNextWord(data
, buf
, BUFSIZ
);
449 if (l
== 7 && !strncmp("#define", buf
, 7)) {
450 /* this maybe an XPM 1 file */
453 l
= xpmNextWord(data
, buf
, BUFSIZ
);
455 return (XpmFileInvalid
);
457 ptr
= rindex(buf
, '_');
458 if (!ptr
|| strncmp("_format", ptr
, l
- (ptr
- buf
)))
459 return XpmFileInvalid
;
460 /* this is definitely an XPM 1 file */
462 n
= 1; /* handle XPM1 as mainly XPM2 C */
466 * skip the first word, get the second one, and see if this is
469 l
= xpmNextWord(data
, buf
, BUFSIZ
);
470 if ((l
== 3 && !strncmp("XPM", buf
, 3)) ||
471 (l
== 4 && !strncmp("XPM2", buf
, 4))) {
473 n
= 1; /* handle XPM as XPM2 C */
475 /* get the type key word */
476 l
= xpmNextWord(data
, buf
, BUFSIZ
);
479 * get infos about this type
481 while (xpmDataTypes
[n
].type
482 && strncmp(xpmDataTypes
[n
].type
, buf
, l
))
487 /* nope this is not an XPM file */
488 return XpmFileInvalid
;
490 if (xpmDataTypes
[n
].type
) {
491 if (n
== 0) { /* natural type */
492 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
493 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
494 xpmNextString(data
); /* skip the end of the headerline */
495 data
->Bos
= xpmDataTypes
[n
].Bos
;
496 data
->Eos
= xpmDataTypes
[n
].Eos
;
498 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
499 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
500 if (!data
->format
) { /* XPM 2 or 3 */
501 data
->Bos
= xpmDataTypes
[n
].Bos
;
503 /* get to the beginning of the first string */
505 data
->Eos
= xpmDataTypes
[n
].Eos
;
506 } else /* XPM 1 skip end of line */
510 /* we don't know about that type of XPM file... */
511 return XpmFileInvalid
;