1: #include <X/mit-copyright.h>
   2: 
   3: /* Copyright 	Massachusetts Institute of Technology  1985 */
   4: /* $Header: XReadBitmapF.c,v 10.8 86/02/01 15:39:24 tony 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:     else if (StringEndsWith (variable, "y_hot"))
  39:             if (y_hot) *y_hot = value;
  40:     }
  41: 
  42:     if (*width <= 0) {
  43:     fclose (file);
  44:     errno = EINVAL;
  45:     return (-1);
  46:     }
  47: 
  48:     if (*height <= 0) {
  49:     fclose (file);
  50:     errno = EINVAL;
  51:     return (-2);
  52:     }
  53: 
  54:     data_length = BitmapSize (*width, *height);
  55:     *data = (short *) malloc (data_length);
  56:     data_length /= sizeof(short);
  57:     if (*data == NULL) {
  58:     fclose (file);
  59:         return (-3);
  60:     }
  61: 
  62:     status = fscanf (file, "static short %80s = { 0x%4hx", variable,
  63:     *data);  /* fills in 0th element of *data array */
  64:     if ((status != 2) || !StringEndsWith (variable, "bits[]")) {
  65:     free (*data);
  66:     fclose (file);
  67:         errno = EINVAL;
  68:     return (-4);
  69:     }
  70: 
  71:     for (i=1;i<data_length;i++) {
  72:     /* fill in i'th element of data array */
  73:     status = fscanf (file, ", 0x%4hx", *data + i);
  74:     if (status != 1) {
  75:         free (*data);
  76:         fclose (file);
  77:         errno = EINVAL;
  78:         return (-5);
  79:         }
  80:         }
  81: 
  82:     fclose (file);
  83:     return (1);
  84:     }
  85: 
  86: /* StringEndsWith returns TRUE if "s" ends with "suffix", else returns FALSE */
  87: static boolean StringEndsWith (s, suffix)
  88:   char *s, *suffix;
  89:   {
  90:   int s_len = strlen (s);
  91:   int suffix_len = strlen (suffix);
  92:   return (strcmp (s + s_len - suffix_len, suffix) == 0);
  93:   }

Defined functions

StringEndsWith defined in line 87; used 5 times

Defined macros

boolean defined in line 10; used 1 times
  • in line 87
Last modified: 1986-02-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 513
Valid CSS Valid XHTML 1.0 Strict