]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/dataxpm.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)
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
183 data
->cptr
= (data
->stream
.data
)[++data
->line
];
184 else if (data
->type
== XPMBUFFER
) {
187 /* get to the end of the current string */
189 while ((c
= *data
->cptr
++)!=NULL
&& c
!= data
->Eos
)
193 * then get to the beginning of the next string looking for possible
197 while ((c
= *data
->cptr
++)!=NULL
&& 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
))!=NULL
!= data
->Eos
&& c
!= EOF
)
215 * then get to the beginning of the next string looking for possible
219 while ((c
= Getc(data
, file
)) != data
->Bos
&& c
!= EOF
)
220 if (data
->Bcmt
&& c
== data
->Bcmt
[0])
223 } else if (data
->Bcmt
) { /* XPM2 natural */
224 while ((c
= Getc(data
, file
)) == data
->Bcmt
[0])
226 Ungetc(data
, c
, file
);
234 * skip whitespace and return the following word
237 xpmNextWord(data
, buf
, buflen
)
242 register unsigned int n
= 0;
245 if (!data
->type
|| data
->type
== XPMBUFFER
) {
246 while (isspace(c
= *data
->cptr
) && c
!= data
->Eos
)
252 } while (!isspace(c
) && c
!= data
->Eos
&& n
< buflen
);
256 FILE *file
= data
->stream
.file
;
258 while ((c
= Getc(data
, file
)) != EOF
&& isspace(c
) && c
!= data
->Eos
)
261 while (!isspace(c
) && c
!= data
->Eos
&& c
!= EOF
&& n
< buflen
) {
264 c
= Getc(data
, file
);
266 Ungetc(data
, c
, file
);
272 * skip whitespace and compute the following unsigned int,
273 * returns 1 if one is found and 0 if not
276 xpmNextUI(data
, ui_return
)
278 unsigned int *ui_return
;
283 l
= xpmNextWord(data
, buf
, BUFSIZ
);
284 return xpmatoui(buf
, l
, ui_return
);
288 * return end of string - WARNING: malloc!
291 xpmGetString(data
, sptr
, l
)
296 unsigned int i
, n
= 0;
298 char *p
= NULL
, *q
, buf
[BUFSIZ
];
300 if (!data
->type
|| data
->type
== XPMBUFFER
) {
302 char *start
= data
->cptr
;
303 while ((c
= *data
->cptr
)!=NULL
&& c
!= data
->Eos
)
305 n
= data
->cptr
- start
+ 1;
306 p
= (char *) XpmMalloc(n
);
308 return (XpmNoMemory
);
309 strncpy(p
, start
, n
);
310 if (data
->type
) /* XPMBUFFER */
314 FILE *file
= data
->stream
.file
;
316 if ((c
= Getc(data
, file
)) == EOF
)
317 return (XpmFileInvalid
);
321 p
= (char *) XpmMalloc(1);
322 while (c
!= data
->Eos
&& c
!= EOF
) {
324 /* get to the end of the buffer */
325 /* malloc needed memory */
326 q
= (char *) XpmRealloc(p
, n
+ i
);
329 return (XpmNoMemory
);
333 /* and copy what we already have */
341 c
= Getc(data
, file
);
345 return (XpmFileInvalid
);
348 /* malloc needed memory */
349 q
= (char *) XpmRealloc(p
, n
+ i
+ 1);
352 return (XpmNoMemory
);
356 /* and copy the buffer */
364 Ungetc(data
, c
, file
);
372 * get the current comment line
381 else if (data
->CommentLength
) {
382 *cmt
= (char *) XpmMalloc(data
->CommentLength
+ 1);
383 strncpy(*cmt
, data
->Comment
, data
->CommentLength
);
384 (*cmt
)[data
->CommentLength
] = '\0';
385 data
->CommentLength
= 0;
391 xpmDataType xpmDataTypes
[] =
393 "", "!", "\n", '\0', '\n', "", "", "", "", /* Natural type */
394 "C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n",
395 "Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n",
399 NULL
, NULL
, NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
416 data
->Bcmt
= data
->Ecmt
= NULL
;
417 l
= xpmNextWord(data
, buf
, BUFSIZ
);
418 if (l
== 7 && !strncmp("#define", buf
, 7)) {
419 /* this maybe an XPM 1 file */
422 l
= xpmNextWord(data
, buf
, BUFSIZ
);
424 return (XpmFileInvalid
);
426 #if defined(macintosh) || defined(__APPLE__)
427 ptr
= strrchr(buf
, '_');
429 ptr
= rindex(buf
, '_');
431 if (!ptr
|| strncmp("_format", ptr
, l
- (ptr
- buf
)))
432 return XpmFileInvalid
;
433 /* this is definitely an XPM 1 file */
435 n
= 1; /* handle XPM1 as mainly XPM2 C */
439 * skip the first word, get the second one, and see if this is
442 l
= xpmNextWord(data
, buf
, BUFSIZ
);
443 if ((l
== 3 && !strncmp("XPM", buf
, 3)) ||
444 (l
== 4 && !strncmp("XPM2", buf
, 4))) {
446 n
= 1; /* handle XPM as XPM2 C */
448 /* get the type key word */
449 l
= xpmNextWord(data
, buf
, BUFSIZ
);
452 * get infos about this type
454 while (xpmDataTypes
[n
].type
455 && strncmp(xpmDataTypes
[n
].type
, buf
, l
))
460 /* nope this is not an XPM file */
461 return XpmFileInvalid
;
463 if (xpmDataTypes
[n
].type
) {
464 if (n
== 0) { /* natural type */
465 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
466 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
467 xpmNextString(data
); /* skip the end of the headerline */
468 data
->Bos
= xpmDataTypes
[n
].Bos
;
469 data
->Eos
= xpmDataTypes
[n
].Eos
;
471 data
->Bcmt
= xpmDataTypes
[n
].Bcmt
;
472 data
->Ecmt
= xpmDataTypes
[n
].Ecmt
;
473 if (!data
->format
) { /* XPM 2 or 3 */
474 data
->Bos
= xpmDataTypes
[n
].Bos
;
476 /* get to the beginning of the first string */
478 data
->Eos
= xpmDataTypes
[n
].Eos
;
479 } else /* XPM 1 skip end of line */
483 /* we don't know about that type of XPM file... */
484 return XpmFileInvalid
;