Debian Codegraph @

src/data.c

raw
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 /* October 2004, source code review by Thomas Biege <[email protected]> */
36
37 #ifndef CXPMPROG
38 #if 0
39 /* Official version number */
40 static char *RCS_Version = "$XpmVersion: 3.4k $";
41
42 /* Internal version number */
43 static char *RCS_Id = "Id: xpm.shar,v 3.71 1998/03/19 19:47:14 lehors Exp $";
44 #endif
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48 #include "XpmI.h"
49 #endif
50 #include <ctype.h>
51
52 #ifndef CXPMPROG
53 #define Getc(data, file) getc(file)
54 #define Ungetc(data, c, file) ungetc(c, file)
55 #endif
56
57 static int
58 ParseComment(xpmData *data)
59 {
60 if (data->type == XPMBUFFER) {
61 register char c;
62 register unsigned int n = 0;
63 unsigned int notend;
64 char *s;
65 const char *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' || c == '\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;
124 const char *s2;
125
126 s = data->Comment;
127 *s = data->Bcmt[0];
128
129 /* skip the string beginning comment */
130 s2 = data->Bcmt;
131 do {
132 c = Getc(data, file);
133 *++s = c;
134 n++;
135 s2++;
136 } while (c == *s2 && *s2 != '\0' && c != EOF);
137
138 if (*s2 != '\0') {
139 /* this wasn't the beginning of a comment */
140 /* put characters back in the order that we got them */
141 for (a = n; a > 0; a--, s--)
142 Ungetc(data, *s, file);
143 return 0;
144 }
145 /* store comment */
146 data->Comment[0] = *s;
147 s = data->Comment;
148 notend = 1;
149 n = 0;
150 while (notend) {
151 s2 = data->Ecmt;
152 while (*s != *s2 && c != EOF) {
153 c = Getc(data, file);
154 if (n == XPMMAXCMTLEN - 1) { /* forget it */
155 s = data->Comment;
156 n = 0;
157 }
158 *++s = c;
159 n++;
160 }
161 data->CommentLength = n;
162 do {
163 c = Getc(data, file);
164 if (n == XPMMAXCMTLEN - 1) { /* forget it */
165 s = data->Comment;
166 n = 0;
167 }
168 *++s = c;
169 n++;
170 s2++;
171 } while (c == *s2 && *s2 != '\0' && c != EOF);
172 if (*s2 == '\0') {
173 /* this is the end of the comment */
174 notend = 0;
175 Ungetc(data, *s, file);
176 }
177 else if (c == EOF) {
178 /* hit end of file before the end of the comment */
179 return XpmFileInvalid;
180 }
181 }
182 return 0;
183 }
184 }
185
186 /*
187 * skip to the end of the current string and the beginning of the next one
188 */
189 int
190 xpmNextString(xpmData *data)
191 {
192 if (!data->type)
193 data->cptr = (data->stream.data)[++data->line];
194 else if (data->type == XPMBUFFER) {
195 register char c;
196
197 /* get to the end of the current string */
198 if (data->Eos) {
199 while ((c = *data->cptr++) && c != data->Eos && c != '\0');
200
201 if (c == '\0')
202 return XpmFileInvalid;
203 }
204
205 /*
206 * then get to the beginning of the next string looking for possible
207 * comment
208 */
209 if (data->Bos) {
210 while ((c = *data->cptr++) && c != data->Bos && c != '\0')
211 if (data->Bcmt && c == data->Bcmt[0])
212 ParseComment(data);
213
214 if (c == '\0')
215 return XpmFileInvalid;
216 } else if (data->Bcmt) { /* XPM2 natural */
217 while (((c = *data->cptr++) == data->Bcmt[0]) && c != '\0')
218 ParseComment(data);
219 data->cptr--;
220 }
221 } else {
222 register int c;
223 FILE *file = data->stream.file;
224
225 /* get to the end of the current string */
226 if (data->Eos) {
227 while ((c = Getc(data, file)) != data->Eos && c != EOF);
228
229 if (c == EOF)
230 return XpmFileInvalid;
231 }
232
233 /*
234 * then get to the beginning of the next string looking for possible
235 * comment
236 */
237 if (data->Bos) {
238 while ((c = Getc(data, file)) != data->Bos && c != EOF)
239 if (data->Bcmt && c == data->Bcmt[0])
240 ParseComment(data);
241
242 } else if (data->Bcmt) { /* XPM2 natural */
243 while ((c = Getc(data, file)) == data->Bcmt[0])
244 ParseComment(data);
245 Ungetc(data, c, file);
246 }
247 }
248 return XpmSuccess;
249 }
250
251
252 /*
253 * skip whitespace and return the following word
254 */
255 unsigned int
256 xpmNextWord(
257 xpmData *data,
258 char *buf,
259 unsigned int buflen)
260 {
261 register unsigned int n = 0;
262 int c;
263
264 if (!data->type || data->type == XPMBUFFER) {
265 while ((c = *data->cptr) && isspace(c) && (c != data->Eos))
266 data->cptr++;
267 do {
268 c = *data->cptr++;
269 *buf++ = c;
270 n++;
271 } while (c && !isspace(c) && (c != data->Eos) && (n < buflen));
272 n--;
273 data->cptr--;
274 } else {
275 FILE *file = data->stream.file;
276
277 while ((c = Getc(data, file)) != EOF && isspace(c) && c != data->Eos);
278 while (!isspace(c) && c != data->Eos && c != EOF && n < buflen) {
279 *buf++ = c;
280 n++;
281 c = Getc(data, file);
282 }
283 Ungetc(data, c, file);
284 }
285 return (n); /* this returns bytes read + 1 */
286 }
287
288 /*
289 * skip whitespace and compute the following unsigned int,
290 * returns 1 if one is found and 0 if not
291 */
292 int
293 xpmNextUI(
294 xpmData *data,
295 unsigned int *ui_return)
296 {
297 char buf[BUFSIZ];
298 int l;
299
300 l = xpmNextWord(data, buf, BUFSIZ);
301 return xpmatoui(buf, l, ui_return);
302 }
303
304 /*
305 * return end of string - WARNING: malloc!
306 */
307 int
308 xpmGetString(
309 xpmData *data,
310 char **sptr,
311 unsigned int *l)
312 {
313 unsigned int i, n = 0;
314 int c;
315 char *p = NULL, *q, buf[BUFSIZ];
316
317 if (!data->type || data->type == XPMBUFFER) {
318 if (data->cptr) {
319 char *start = data->cptr;
320 while ((c = *data->cptr) && c != data->Eos)
321 data->cptr++;
322 n = data->cptr - start + 1;
323 p = (char *) XpmMalloc(n);
324 if (!p)
325 return (XpmNoMemory);
326 strncpy(p, start, n);
327 if (data->type) /* XPMBUFFER */
328 p[n - 1] = '\0';
329 }
330 } else {
331 FILE *file = data->stream.file;
332
333 if ((c = Getc(data, file)) == EOF)
334 return (XpmFileInvalid);
335
336 i = 0;
337 q = buf;
338 p = (char *) XpmMalloc(1);
339 while (c != data->Eos && c != EOF) {
340 if (i == BUFSIZ) {
341 /* get to the end of the buffer */
342 /* malloc needed memory */
343 q = (char *) XpmRealloc(p, n + i);
344 if (!q) {
345 XpmFree(p);
346 return (XpmNoMemory);
347 }
348 p = q;
349 q += n;
350 /* and copy what we already have */
351 strncpy(q, buf, i);
352 n += i;
353 i = 0;
354 q = buf;
355 }
356 *q++ = c;
357 i++;
358 c = Getc(data, file);
359 }
360 if (c == EOF) {
361 XpmFree(p);
362 return (XpmFileInvalid);
363 }
364 if (n + i != 0) {
365 /* malloc needed memory */
366 q = (char *) XpmRealloc(p, n + i + 1);
367 if (!q) {
368 XpmFree(p);
369 return (XpmNoMemory);
370 }
371 p = q;
372 q += n;
373 /* and copy the buffer */
374 strncpy(q, buf, i);
375 n += i;
376 p[n++] = '\0';
377 } else {
378 *p = '\0';
379 n = 1;
380 }
381 Ungetc(data, c, file);
382 }
383 *sptr = p;
384 *l = n;
385 return (XpmSuccess);
386 }
387
388 /*
389 * get the current comment line
390 */
391 int
392 xpmGetCmt(
393 xpmData *data,
394 char **cmt)
395 {
396 if (!data->type)
397 *cmt = NULL;
398 else if (data->CommentLength != 0 && data->CommentLength < UINT_MAX - 1) {
399 if( (*cmt = (char *) XpmMalloc(data->CommentLength + 1)) == NULL)
400 return XpmNoMemory;
401 strncpy(*cmt, data->Comment, data->CommentLength);
402 (*cmt)[data->CommentLength] = '\0';
403 data->CommentLength = 0;
404 } else
405 *cmt = NULL;
406 return 0;
407 }
408
409 xpmDataType xpmDataTypes[] =
410 {
411 {"", "!", "\n", '\0', '\n', "", "", "", ""}, /* Natural type */
412 {"C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n"},
413 {"Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n"},
414 {NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL}
415 };
416
417 /*
418 * parse xpm header
419 */
420 int
421 xpmParseHeader(xpmData *data)
422 {
423 char buf[BUFSIZ+1] = {0};
424 int l, n = 0;
425
426 if (data->type) {
427 data->Bos = '\0';
428 data->Eos = '\n';
429 data->Bcmt = data->Ecmt = NULL;
430 l = xpmNextWord(data, buf, BUFSIZ);
431 if (l == 7 && !strncmp("#define", buf, 7)) {
432 /* this maybe an XPM 1 file */
433 char *ptr;
434
435 l = xpmNextWord(data, buf, BUFSIZ);
436 if (!l)
437 return (XpmFileInvalid);
438 buf[l] = '\0';
439 ptr = strrchr(buf, '_');
440 if (!ptr || strncmp("_format", ptr, l - (ptr - buf)))
441 return XpmFileInvalid;
442 /* this is definitely an XPM 1 file */
443 data->format = 1;
444 n = 1; /* handle XPM1 as mainly XPM2 C */
445 } else {
446
447 /*
448 * skip the first word, get the second one, and see if this is
449 * XPM 2 or 3
450 */
451 l = xpmNextWord(data, buf, BUFSIZ);
452 if ((l == 3 && !strncmp("XPM", buf, 3)) ||
453 (l == 4 && !strncmp("XPM2", buf, 4))) {
454 if (l == 3)
455 n = 1; /* handle XPM as XPM2 C */
456 else {
457 /* get the type key word */
458 l = xpmNextWord(data, buf, BUFSIZ);
459
460 /*
461 * get infos about this type
462 */
463 while (xpmDataTypes[n].type
464 && strncmp(xpmDataTypes[n].type, buf, l))
465 n++;
466 }
467 data->format = 0;
468 } else
469 /* nope this is not an XPM file */
470 return XpmFileInvalid;
471 }
472 if (xpmDataTypes[n].type) {
473 if (n == 0) { /* natural type */
474 data->Bcmt = xpmDataTypes[n].Bcmt;
475 data->Ecmt = xpmDataTypes[n].Ecmt;
476 xpmNextString(data); /* skip the end of the headerline */
477 data->Bos = xpmDataTypes[n].Bos;
478 data->Eos = xpmDataTypes[n].Eos;
479 } else {
480 data->Bcmt = xpmDataTypes[n].Bcmt;
481 data->Ecmt = xpmDataTypes[n].Ecmt;
482 if (!data->format) { /* XPM 2 or 3 */
483 data->Bos = xpmDataTypes[n].Bos;
484 data->Eos = '\0';
485 /* get to the beginning of the first string */
486 xpmNextString(data);
487 data->Eos = xpmDataTypes[n].Eos;
488 } else /* XPM 1 skip end of line */
489 xpmNextString(data);
490 }
491 } else
492 /* we don't know about that type of XPM file... */
493 return XpmFileInvalid;
494 }
495 return XpmSuccess;
496 }