Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/data.c

raw
1 /*---------------------------------------------------------------------
2 *---------------------------------------------------------------------
3 *
4 *
5 * Copyright (c) 1992, Visual Edge Software Ltd.
6 *
7 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
8 * distribute this software and its documentation for any purpose
9 * and without fee is hereby granted, provided that the above
10 * copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of Visual Edge Software not be
13 * used in advertising or publicity pertaining to distribution of
14 * the software without specific, written prior permission. The year
15 * included in the notice is the year of the creation of the work.
16 *-------------------------------------------------------------------*/
17 /*
18 * Copyright 1990, 1991 GROUPE BULL
19 *
20 * Permission to use, copy, modify, and distribute this software and its
21 * documentation for any purpose and without fee is hereby granted, provided
22 * that the above copyright notice appear in all copies and that both that
23 * copyright notice and this permission notice appear in supporting
24 * documentation, and that the name of GROUPE BULL not be used in advertising
25 * or publicity pertaining to distribution of the software without specific,
26 * written prior permission. GROUPE BULL makes no representations about the
27 * suitability of this software for any purpose. It is provided "as is"
28 * without express or implied warranty.
29 *
30 * GROUPE BULL disclaims all warranties with regard to this software,
31 * including all implied warranties of merchantability and fitness,
32 * in no event shall GROUPE BULL be liable for any special,
33 * indirect or consequential damages or any damages
34 * whatsoever resulting from loss of use, data or profits,
35 * whether in an action of contract, negligence or other tortious
36 * action, arising out of or in connection with the use
37 * or performance of this software.
38 *
39
40 .VERSION
41 090818 last modif
42
43 */
44
45 /*****************************************************************************\
46 * data.c: *
47 * *
48 * XPM library *
49 * IO utilities *
50 * *
51 * Developed by Arnaud Le Hors *
52 \*****************************************************************************/
53
54 /* Official version number ./
55 static char *RCS_Version = "$XpmVersion: 3.2 $";
56
57 /. Internal version number ./
58 static char *RCS_Id = "$Id: data.c,v 1.3 2009-08-18 15:23:20 kbanse Exp $";
59 */
60
61 #include <stdlib.h>
62
63 #include "xpmP.h"
64 #ifdef VMS
65 #include "sys$library:stat.h"
66 #include "sys$library:ctype.h"
67 #else
68 #include <sys/stat.h>
69 #include <ctype.h>
70 #endif
71
72 FUNC(atoui, unsigned int, (char *p, unsigned int l, unsigned int *ui_return));
73 LFUNC(ParseComment, int, (xpmData *mdata));
74
75 unsigned int atoui(p, l, ui_return)
76 register char *p;
77 unsigned int l;
78 unsigned int *ui_return;
79 {
80 register int n, i;
81
82 n = 0;
83 for (i = 0; i < l; i++)
84 if (*p >= '0' && *p <= '9')
85 n = n * 10 + *p++ - '0';
86 else
87 break;
88
89 if (i != 0 && i == l) {
90 *ui_return = n;
91 return 1;
92 } else
93 return 0;
94 }
95
96 static int
97 ParseComment(mdata)
98 xpmData *mdata;
99 {
100 FILE *file = mdata->stream.file;
101 register int c;
102 register unsigned int n = 0, a;
103 unsigned int notend;
104 char *s, *s2;
105
106 s = mdata->Comment;
107 *s = mdata->Bcmt[0];
108
109 /* skip the string beginning comment */
110 s2 = mdata->Bcmt;
111 do {
112 c = getc(file);
113 *++s = c;
114 n++;
115 s2++;
116 } while (c == *s2 && *s2 != '\0'
117 && c != EOF && c != mdata->Bos);
118
119 if (*s2 != '\0') {
120 /* this wasn't the beginning of a comment */
121 /* put characters back in the order that we got them */
122 for (a = n; a > 0; a--, s--)
123 ungetc(*s, file);
124 return 0;
125 }
126
127 /* store comment */
128 mdata->Comment[0] = *s;
129 s = mdata->Comment;
130 notend = 1;
131 n = 0;
132 while (notend) {
133 s2 = mdata->Ecmt;
134 while (*s != *s2 && c != EOF && c != mdata->Bos) {
135 c = getc(file);
136 *++s = c;
137 n++;
138 }
139 mdata->CommentLength = n;
140 do {
141 c = getc(file);
142 n++;
143 *++s = c;
144 s2++;
145 } while (c == *s2 && *s2 != '\0'
146 && c != EOF && c != mdata->Bos);
147 if (*s2 == '\0') {
148 /* this is the end of the comment */
149 notend = 0;
150 ungetc(*s, file);
151 }
152 }
153 return 0;
154 }
155
156 /*
157 * skip to the end of the current string and the beginning of the next one
158 */
159 int xpmNextString(mdata)
160 xpmData *mdata;
161 {
162 if (!mdata->type)
163 mdata->cptr = (mdata->stream.data)[++mdata->line];
164 else {
165 register int c=0;
166 FILE *file = mdata->stream.file;
167
168 /* get to the end of the current string */
169 if (mdata->Eos)
170 while ((c = getc(file)) != mdata->Eos && c != EOF);
171
172 /* then get to the beginning of the next string
173 * looking for possible comment */
174 if (mdata->Bos) {
175 while ((c = getc(file)) != mdata->Bos && c != EOF)
176 if (mdata->Bcmt && c == mdata->Bcmt[0])
177 ParseComment(mdata);
178
179 } else { /* XPM2 natural */
180 while (mdata->Bcmt && (c = getc(file)) == mdata->Bcmt[0])
181 ParseComment(mdata);
182 ungetc(c, file);
183 }
184 }
185 return 0;
186 }
187
188
189 /*
190 * skip whitespace and compute the following unsigned int,
191 * returns 1 if one is found and 0 if not
192 */
193 int
194 xpmNextUI(mdata, ui_return)
195 xpmData *mdata;
196 unsigned int *ui_return;
197 {
198 char buf[BUFSIZ];
199 int l;
200
201 l = xpmNextWord(mdata, buf);
202 return atoui(buf, l, ui_return);
203 }
204
205 /*
206 * skip whitespace and return the following word
207 */
208 unsigned int
209 xpmNextWord(mdata, buf)
210 xpmData *mdata;
211 char *buf;
212 {
213 register unsigned int n = 0;
214 int c;
215
216 if (!mdata->type) {
217 while (isspace(c = *mdata->cptr) && c != mdata->Eos)
218 mdata->cptr++;
219 do {
220 c = *mdata->cptr++;
221 *buf++ = c;
222 n++;
223 } while (!isspace(c) && c != mdata->Eos);
224 n--;
225 mdata->cptr--;
226 } else {
227 FILE *file = mdata->stream.file;
228 while (isspace(c = getc(file)) && c != mdata->Eos);
229 while (!isspace(c) && c != mdata->Eos && c != EOF) {
230 *buf++ = c;
231 n++;
232 c = getc(file);
233 }
234 ungetc(c, file);
235 }
236 return (n);
237 }
238
239 /*
240 * return end of string - WARNING: malloc!
241 */
242 int xpmGetString(mdata, sptr, l)
243 xpmData *mdata;
244 char **sptr;
245 unsigned int *l;
246 {
247 unsigned int i, n = 0;
248 int c;
249 char *p, *q, buf[BUFSIZ];
250
251 p = (char *) 0;
252
253 if (!mdata->type) {
254 if (mdata->cptr) {
255 char *start;
256 while (isspace(c = *mdata->cptr) && c != mdata->Eos)
257 mdata->cptr++;
258 start = mdata->cptr;
259 while ((c = *mdata->cptr))
260 mdata->cptr++;
261 if ((n = mdata->cptr - start)) {
262 n++;
263 p = (char *) malloc((size_t)n);
264 if (!p)
265 return (XpmNoMemory);
266 strncpy(p, start, n);
267 } else
268 free(p);
269 }
270 } else {
271 FILE *file = mdata->stream.file;
272 while (isspace(c = getc(file)) && c != mdata->Eos);
273 p = NULL;
274 i = 0;
275 q = buf;
276 p = (char *) malloc((size_t)1);
277 while (c != mdata->Eos && c != EOF) {
278 if (i == BUFSIZ) {
279 /* get to the end of the buffer */
280 /* malloc needed memory */
281 q = (char *) realloc(p, n + i);
282 if (!q) {
283 free(p);
284 return (XpmNoMemory);
285 }
286 p = q;
287 q += n;
288 /* and copy what we already have */
289 strncpy(q, buf, i);
290 n += i;
291 i = 0;
292 q = buf;
293 }
294 *q++ = c;
295 i++;
296 c = getc(file);
297 }
298 if (n + i != 0) {
299 /* malloc needed memory */
300 q = (char *) realloc(p, n + i + 1);
301 if (!q) {
302 free(p);
303 return (XpmNoMemory);
304 }
305 p = q;
306 q += n;
307 /* and copy the buffer */
308 strncpy(q, buf, i);
309 n += i;
310 p[n++] = '\0';
311 } else
312 free(p);
313 ungetc(c, file);
314 }
315 *sptr = p;
316 *l = n;
317 return (XpmSuccess);
318 }
319
320 /*
321 * get the current comment line
322 */
323 int xpmGetCmt(mdata, cmt)
324 xpmData *mdata;
325 char **cmt;
326 {
327 if (!mdata->type)
328 *cmt = NULL;
329 else
330 if (mdata->CommentLength) {
331 *cmt = (char *) malloc((size_t)(mdata->CommentLength+1));
332 strncpy(*cmt, mdata->Comment, mdata->CommentLength);
333 (*cmt)[mdata->CommentLength] = '\0';
334 mdata->CommentLength = 0;
335 } else
336 *cmt = NULL;
337 return 0;
338 }
339
340 /*
341 * open the given file to be read as an xpmData which is returned.
342 */
343 int
344 xpmReadFile(filename, mdata)
345 char *filename;
346 xpmData *mdata;
347 {
348
349 if (!filename) {
350 mdata->stream.file = (stdin);
351 mdata->type = XPMFILE;
352 } else {
353 #ifdef ZPIPE
354 if ((strlen(filename) > 2) &&
355 !strcmp(".Z", filename + (strlen(filename) - 2))) {
356 mdata->type = XPMPIPE;
357 sprintf(buf, "uncompress -c %s", filename);
358 if (!(mdata->stream.file = popen(buf, "r")))
359 return (XpmOpenFailed);
360
361 } else {
362 if (!(compressfile = (char *) malloc((size_t)(strlen(filename)+3))))
363 return (XpmNoMemory);
364
365 strcpy(compressfile, filename);
366 strcat(compressfile, ".Z");
367 if (!stat(compressfile, &status)) {
368 sprintf(buf, "uncompress -c %s", compressfile);
369 if (!(mdata->stream.file = popen(buf, "r"))) {
370 free(compressfile);
371 return (XpmOpenFailed);
372 }
373 mdata->type = XPMPIPE;
374 } else {
375 #endif
376 if (!(mdata->stream.file = fopen(filename, "r"))) {
377 #ifdef ZPIPE
378 free(compressfile);
379 #endif
380 return (XpmOpenFailed);
381 }
382 mdata->type = XPMFILE;
383 #ifdef ZPIPE
384 }
385 free(compressfile);
386 }
387 #endif
388 }
389 mdata->CommentLength = 0;
390 return (XpmSuccess);
391 }
392
393 /*
394 * open the given file to be written as an xpmData which is returned
395 */
396 int
397 xpmWriteFile(filename, mdata)
398 char *filename;
399 xpmData *mdata;
400 {
401
402 if (!filename) {
403 mdata->stream.file = (stdout);
404 mdata->type = XPMFILE;
405 } else {
406 #ifdef ZPIPE
407 if (strlen(filename) > 2
408 && !strcmp(".Z", filename + (strlen(filename) - 2))) {
409 sprintf(buf, "compress > %s", filename);
410 if (!(mdata->stream.file = popen(buf, "w")))
411 return (XpmOpenFailed);
412
413 mdata->type = XPMPIPE;
414 } else {
415 #endif
416 if (!(mdata->stream.file = fopen(filename, "w")))
417 return (XpmOpenFailed);
418
419 mdata->type = XPMFILE;
420 #ifdef ZPIPE
421 }
422 #endif
423 }
424 return (XpmSuccess);
425 }
426
427 /*
428 * open the given array to be read or written as an xpmData which is returned
429 */
430 int
431 xpmOpenArray(data, mdata)
432 char **data;
433 xpmData *mdata;
434 {
435 mdata->type = XPMARRAY;
436 mdata->stream.data = data;
437 mdata->cptr = *data;
438 mdata->line = 0;
439 mdata->CommentLength = 0;
440 mdata->Bcmt = mdata->Ecmt = NULL;
441 mdata->Bos = mdata->Eos = '\0';
442 return (XpmSuccess);
443 }
444
445 /*
446 * close the file related to the xpmData if any
447 */
448 int XpmDataClose(mdata)
449 xpmData *mdata;
450 {
451 switch (mdata->type) {
452 case XPMARRAY:
453 break;
454 case XPMFILE:
455 if (mdata->stream.file != (stdout) && mdata->stream.file != (stdin))
456 fclose(mdata->stream.file);
457 break;
458 #ifdef ZPIPE
459 case XPMPIPE:
460 pclose(mdata->stream.file);
461 break;
462 #endif
463 }
464 return 0;
465 }