|
|
1.1 root 1: #include <X/mit-copyright.h>
2:
3: /* Copyright Massachusetts Institute of Technology 1985 */
4: /* $Header: XReadBitmapF.c,v 10.10 86/11/18 11:54:02 jg Rel $ */
5: #include "XlibInternal.h"
6: #include <stdio.h>
7: #include <errno.h>
8: #include <strings.h>
9:
10: #define boolean int
11:
12: extern int errno;
13:
14: Status XReadBitmapFile(filename, width, height, data, x_hot, y_hot)
15: char *filename;
16: register int *width, *height; /* RETURN; must be non-NULL */
17: register short **data; /* RETURN */
18: int *x_hot, *y_hot; /* RETURN; may be NULL */
19: {
20: char variable[81];
21: int status, value, i, data_length;
22: FILE *file = fopen (filename, "r");
23:
24: if (file == NULL)
25: return (0);
26:
27: *width = *height = -1;
28: if (x_hot) *x_hot = -1;
29: if (y_hot) *y_hot = -1;
30: while ((status = fscanf (file, "#define %80s %2d\n", variable, &value))==2)
31: {
32: if (StringEndsWith (variable, "width"))
33: *width = value;
34: else if (StringEndsWith (variable, "height"))
35: *height = value;
36: else if (StringEndsWith (variable, "x_hot")) {
37: if (x_hot) *x_hot = value;
38: }
39: else if (StringEndsWith (variable, "y_hot")) {
40: if (y_hot) *y_hot = value;
41: }
42: }
43:
44: if (*width <= 0) {
45: fclose (file);
46: errno = EINVAL;
47: return (-1);
48: }
49:
50: if (*height <= 0) {
51: fclose (file);
52: errno = EINVAL;
53: return (-2);
54: }
55:
56: data_length = BitmapSize (*width, *height);
57: *data = (short *) malloc (data_length);
58: data_length /= sizeof(short);
59: if (*data == NULL) {
60: fclose (file);
61: return (-3);
62: }
63:
64: status = fscanf (file, "static short %80s = { 0x%4hx", variable,
65: *data); /* fills in 0th element of *data array */
66: if ((status != 2) || !StringEndsWith (variable, "bits[]")) {
67: free ((char *)*data);
68: fclose (file);
69: errno = EINVAL;
70: return (-4);
71: }
72:
73: for (i=1;i<data_length;i++) {
74: /* fill in i'th element of data array */
75: status = fscanf (file, ", 0x%4hx", *data + i);
76: if (status != 1) {
77: free ((char *)*data);
78: fclose (file);
79: errno = EINVAL;
80: return (-5);
81: }
82: }
83:
84: fclose (file);
85: return (1);
86: }
87:
88: /* StringEndsWith returns TRUE if "s" ends with "suffix", else returns FALSE */
89: static boolean StringEndsWith (s, suffix)
90: char *s, *suffix;
91: {
92: int s_len = strlen (s);
93: int suffix_len = strlen (suffix);
94: return (strcmp (s + s_len - suffix_len, suffix) == 0);
95: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.