]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/xpm/data.c
fix for assert when setting tooltip for a readonly combo
[wxWidgets.git] / src / xpm / data.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
20 *
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.
24 */
25
26/*****************************************************************************\
27* data.c: *
28* *
29* XPM library *
30* IO utilities *
31* *
32* Developed by Arnaud Le Hors *
33\*****************************************************************************/
34
35#ifndef CXPMPROG
36/* Official version number */
37static char *RCS_Version = "$XpmVersion: 3.4k $";
38
39/* Internal version number */
40static char *RCS_Id = "$Id$";
41
42#include "XpmI.h"
43#endif
44#include <ctype.h>
45
46#ifndef CXPMPROG
47#define Getc(data, file) getc(file)
48#define Ungetc(data, c, file) ungetc(c, file)
49#endif
50
51#ifdef __OS2__
52/* Visual Age cannot deal with old, non-ansi, code */
53static int
54ParseComment(xpmData* data)
55#else
56static int
57ParseComment(data)
58 xpmData *data;
59#endif
60{
61 if (data->type == XPMBUFFER) {
62 register char c;
63 register unsigned int n = 0;
64 unsigned int notend;
65 char *s, *s2;
66
67 s = data->Comment;
68 *s = data->Bcmt[0];
69
70 /* skip the string beginning comment */
71 s2 = data->Bcmt;
72 do {
73 c = *data->cptr++;
74 *++s = c;
75 n++;
76 s2++;
77 } while (c == *s2 && *s2 != '\0' && c);
78
79 if (*s2 != '\0') {
80 /* this wasn't the beginning of a comment */
81 data->cptr -= n;
82 return 0;
83 }
84 /* store comment */
85 data->Comment[0] = *s;
86 s = data->Comment;
87 notend = 1;
88 n = 0;
89 while (notend) {
90 s2 = data->Ecmt;
91 while (*s != *s2 && c) {
92 c = *data->cptr++;
93 if (n == XPMMAXCMTLEN - 1) { /* forget it */
94 s = data->Comment;
95 n = 0;
96 }
97 *++s = c;
98 n++;
99 }
100 data->CommentLength = n;
101 do {
102 c = *data->cptr++;
103 if (n == XPMMAXCMTLEN - 1) { /* forget it */
104 s = data->Comment;
105 n = 0;
106 }
107 *++s = c;
108 n++;
109 s2++;
110 } while (c == *s2 && *s2 != '\0' && c);
111 if (*s2 == '\0') {
112 /* this is the end of the comment */
113 notend = 0;
114 data->cptr--;
115 }
116 }
117 return 0;
118 } else {
119 FILE *file = data->stream.file;
120 register int c;
121 register unsigned int n = 0, a;
122 unsigned int notend;
123 char *s, *s2;
124
125 s = data->Comment;
126 *s = data->Bcmt[0];
127
128 /* skip the string beginning comment */
129 s2 = data->Bcmt;
130 do {
131 c = Getc(data, file);
132 *++s = c;
133 n++;
134 s2++;
135 } while (c == *s2 && *s2 != '\0' && c != EOF);
136
137 if (*s2 != '\0') {
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);
142 return 0;
143 }
144 /* store comment */
145 data->Comment[0] = *s;
146 s = data->Comment;
147 notend = 1;
148 n = 0;
149 while (notend) {
150 s2 = data->Ecmt;
151 while (*s != *s2 && c != EOF) {
152 c = Getc(data, file);
153 if (n == XPMMAXCMTLEN - 1) { /* forget it */
154 s = data->Comment;
155 n = 0;
156 }
157 *++s = c;
158 n++;
159 }
160 data->CommentLength = n;
161 do {
162 c = Getc(data, file);
163 if (n == XPMMAXCMTLEN - 1) { /* forget it */
164 s = data->Comment;
165 n = 0;
166 }
167 *++s = c;
168 n++;
169 s2++;
170 } while (c == *s2 && *s2 != '\0' && c != EOF);
171 if (*s2 == '\0') {
172 /* this is the end of the comment */
173 notend = 0;
174 Ungetc(data, *s, file);
175 }
176 }
177 return 0;
178 }
179}
180
181/*
182 * skip to the end of the current string and the beginning of the next one
183 */
184#ifdef __OS2__
185/* Visual Age cannot deal with old, non-ansi, code */
186int xpmNextString(xpmData* data)
187#else
188int
189xpmNextString(data)
190 xpmData *data;
191#endif
192{
193 if (!data->type)
194 data->cptr = (data->stream.data)[++data->line];
195 else if (data->type == XPMBUFFER) {
196 register char c;
197
198 /* get to the end of the current string */
199 if (data->Eos)
200 while ((c = *data->cptr++) && c != data->Eos);
201
202 /*
203 * then get to the beginning of the next string looking for possible
204 * comment
205 */
206 if (data->Bos) {
207 while ((c = *data->cptr++) && c != data->Bos)
208 if (data->Bcmt && c == data->Bcmt[0])
209 ParseComment(data);
210 } else if (data->Bcmt) { /* XPM2 natural */
211 while ((c = *data->cptr++) == data->Bcmt[0])
212 ParseComment(data);
213 data->cptr--;
214 }
215 } else {
216 register int c;
217 FILE *file = data->stream.file;
218
219 /* get to the end of the current string */
220 if (data->Eos)
221 while ((c = Getc(data, file)) != data->Eos && c != EOF);
222
223 /*
224 * then get to the beginning of the next string looking for possible
225 * comment
226 */
227 if (data->Bos) {
228 while ((c = Getc(data, file)) != data->Bos && c != EOF)
229 if (data->Bcmt && c == data->Bcmt[0])
230 ParseComment(data);
231
232 } else if (data->Bcmt) { /* XPM2 natural */
233 while ((c = Getc(data, file)) == data->Bcmt[0])
234 ParseComment(data);
235 Ungetc(data, c, file);
236 }
237 }
238 return 0;
239}
240
241
242/*
243 * skip whitespace and return the following word
244 */
245#ifdef __OS2__
246/* Visual Age cannot deal with old, non-ansi, code */
247unsigned int xpmNextWord(
248 xpmData* data
249, char* buf
250, unsigned int buflen
251)
252#else
253unsigned int
254xpmNextWord(data, buf, buflen)
255 xpmData *data;
256 char *buf;
257 unsigned int buflen;
258#endif
259{
260 register unsigned int n = 0;
261 int c;
262
263 if (!data->type || data->type == XPMBUFFER) {
264 while (isspace(c = *data->cptr) && c != data->Eos)
265 data->cptr++;
266 do {
267 c = *data->cptr++;
268 *buf++ = c;
269 n++;
270 } while (!isspace(c) && c != data->Eos && n < buflen);
271 n--;
272 data->cptr--;
273 } else {
274 FILE *file = data->stream.file;
275
276 while ((c = Getc(data, file)) != EOF && isspace(c) && c != data->Eos);
277 while (!isspace(c) && c != data->Eos && c != EOF && n < buflen) {
278 *buf++ = c;
279 n++;
280 c = Getc(data, file);
281 }
282 Ungetc(data, c, file);
283 }
284 return (n);
285}
286
287/*
288 * skip whitespace and compute the following unsigned int,
289 * returns 1 if one is found and 0 if not
290 */
291#ifdef __OS2__
292/* Visual Age cannot deal with old, non-ansi, code */
293int
294xpmNextUI(xpmData* data, unsigned int* ui_return)
295#else
296int
297xpmNextUI(data, ui_return)
298 xpmData *data;
299 unsigned int *ui_return;
300#endif
301{
302 char buf[BUFSIZ];
303 int l;
304
305 l = xpmNextWord(data, buf, BUFSIZ);
306 return xpmatoui(buf, l, ui_return);
307}
308
309/*
310 * return end of string - WARNING: malloc!
311 */
312#ifdef __OS2__
313/* Visual Age cannot deal with old, non-ansi, code */
314int xpmGetString(xpmData* data, char** sptr, unsigned int* l)
315#else
316int
317xpmGetString(data, sptr, l)
318 xpmData *data;
319 char **sptr;
320 unsigned int *l;
321#endif
322{
323 unsigned int i, n = 0;
324 int c;
325 char *p = NULL, *q, buf[BUFSIZ];
326
327 if (!data->type || data->type == XPMBUFFER) {
328 if (data->cptr) {
329 char *start = data->cptr;
330 while ((c = *data->cptr) && c != data->Eos)
331 data->cptr++;
332 n = data->cptr - start + 1;
333 p = (char *) XpmMalloc(n);
334 if (!p)
335 return (XpmNoMemory);
336 strncpy(p, start, n);
337 if (data->type) /* XPMBUFFER */
338 p[n - 1] = '\0';
339 }
340 } else {
341 FILE *file = data->stream.file;
342
343 if ((c = Getc(data, file)) == EOF)
344 return (XpmFileInvalid);
345
346 i = 0;
347 q = buf;
348 p = (char *) XpmMalloc(1);
349 while (c != data->Eos && c != EOF) {
350 if (i == BUFSIZ) {
351 /* get to the end of the buffer */
352 /* malloc needed memory */
353 q = (char *) XpmRealloc(p, n + i);
354 if (!q) {
355 XpmFree(p);
356 return (XpmNoMemory);
357 }
358 p = q;
359 q += n;
360 /* and copy what we already have */
361 strncpy(q, buf, i);
362 n += i;
363 i = 0;
364 q = buf;
365 }
366 *q++ = c;
367 i++;
368 c = Getc(data, file);
369 }
370 if (c == EOF) {
371 XpmFree(p);
372 return (XpmFileInvalid);
373 }
374 if (n + i != 0) {
375 /* malloc needed memory */
376 q = (char *) XpmRealloc(p, n + i + 1);
377 if (!q) {
378 XpmFree(p);
379 return (XpmNoMemory);
380 }
381 p = q;
382 q += n;
383 /* and copy the buffer */
384 strncpy(q, buf, i);
385 n += i;
386 p[n++] = '\0';
387 } else {
388 *p = '\0';
389 n = 1;
390 }
391 Ungetc(data, c, file);
392 }
393 *sptr = p;
394 *l = n;
395 return (XpmSuccess);
396}
397
398/*
399 * get the current comment line
400 */
401#ifdef __OS2__
402/* Visual Age cannot deal with old, non-ansi, code */
403int xpmGetCmt(xpmData* data, char** cmt)
404#else
405int
406xpmGetCmt(data, cmt)
407 xpmData *data;
408 char **cmt;
409#endif
410{
411 if (!data->type)
412 *cmt = NULL;
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;
418 } else
419 *cmt = NULL;
420 return 0;
421}
422
423xpmDataType xpmDataTypes[] =
424{
425 "", "!", "\n", '\0', '\n', "", "", "", "", /* Natural type */
426 "C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n",
427 "Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n",
428#ifdef VMS
429 NULL
430#else
431 NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL
432#endif
433};
434
435/*
436 * parse xpm header
437 */
438#ifdef __OS2__
439/* Visual Age cannot deal with old, non-ansi, code */
440int xpmParseHeader(xpmData* data)
441#else
442int
443xpmParseHeader(data)
444 xpmData *data;
445#endif
446{
447 char buf[BUFSIZ];
448 int l, n = 0;
449
450 if (data->type) {
451 data->Bos = '\0';
452 data->Eos = '\n';
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 */
457 char *ptr;
458
459 l = xpmNextWord(data, buf, BUFSIZ);
460 if (!l)
461 return (XpmFileInvalid);
462 buf[l] = '\0';
463 ptr = rindex(buf, '_');
464 if (!ptr || strncmp("_format", ptr, l - (ptr - buf)))
465 return XpmFileInvalid;
466 /* this is definitely an XPM 1 file */
467 data->format = 1;
468 n = 1; /* handle XPM1 as mainly XPM2 C */
469 } else {
470
471 /*
472 * skip the first word, get the second one, and see if this is
473 * XPM 2 or 3
474 */
475 l = xpmNextWord(data, buf, BUFSIZ);
476 if ((l == 3 && !strncmp("XPM", buf, 3)) ||
477 (l == 4 && !strncmp("XPM2", buf, 4))) {
478 if (l == 3)
479 n = 1; /* handle XPM as XPM2 C */
480 else {
481 /* get the type key word */
482 l = xpmNextWord(data, buf, BUFSIZ);
483
484 /*
485 * get infos about this type
486 */
487 while (xpmDataTypes[n].type
488 && strncmp(xpmDataTypes[n].type, buf, l))
489 n++;
490 }
491 data->format = 0;
492 } else
493 /* nope this is not an XPM file */
494 return XpmFileInvalid;
495 }
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;
503 } else {
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;
508 data->Eos = '\0';
509 /* get to the beginning of the first string */
510 xpmNextString(data);
511 data->Eos = xpmDataTypes[n].Eos;
512 } else /* XPM 1 skip end of line */
513 xpmNextString(data);
514 }
515 } else
516 /* we don't know about that type of XPM file... */
517 return XpmFileInvalid;
518 }
519 return XpmSuccess;
520}