home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / xpm / XpmRdFToData.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  4KB  |  116 lines

  1. /* Copyright 1990,91 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * XpmRdFToData.c:                                                             *
  4. *                                                                             *
  5. *  XPM library                                                                *
  6. *  Parse an XPM file and create an array of strings corresponding to it.      *
  7. *                                                                             *
  8. *  Developed by Dan Greening dgreen@cs.ucla.edu / dgreen@sti.com              *
  9. \*****************************************************************************/
  10.  
  11. #include "xpmP.h"
  12.  
  13. int
  14. XpmReadFileToData(filename, data_return)
  15.     char *filename;
  16.     char ***data_return;
  17. {
  18.     xpmData mdata;
  19.     char buf[BUFSIZ];
  20.     int l, n = 0;
  21.     XpmAttributes attributes;
  22.     xpmInternAttrib attrib;
  23.     int ErrorStatus;
  24.     XGCValues gcv;
  25.     GC gc;
  26.  
  27.     attributes.valuemask = XpmReturnPixels|XpmReturnInfos|XpmReturnExtensions;
  28.     /*
  29.      * initialize return values 
  30.      */
  31.     if (data_return) {
  32.     *data_return = NULL;
  33.     }
  34.  
  35.     if ((ErrorStatus = xpmReadFile(filename, &mdata)) != XpmSuccess)
  36.     return (ErrorStatus);
  37.     xpmInitInternAttrib(&attrib);
  38.     /*
  39.      * parse the header file 
  40.      */
  41.     mdata.Bos = '\0';
  42.     mdata.Eos = '\n';
  43.     mdata.Bcmt = mdata.Ecmt = NULL;
  44.     xpmNextWord(&mdata, buf);        /* skip the first word */
  45.     l = xpmNextWord(&mdata, buf);    /* then get the second word */
  46.     if ((l == 3 && !strncmp("XPM", buf, 3)) ||
  47.     (l == 4 && !strncmp("XPM2", buf, 4))) {
  48.     if (l == 3)
  49.         n = 1;            /* handle XPM as XPM2 C */
  50.     else {
  51.         l = xpmNextWord(&mdata, buf); /* get the type key word */
  52.  
  53.         /*
  54.          * get infos about this type 
  55.          */
  56.         while (xpmDataTypes[n].type
  57.            && strncmp(xpmDataTypes[n].type, buf, l))
  58.         n++;
  59.     }
  60.     if (xpmDataTypes[n].type) {
  61.         if (n == 0) {        /* natural type */
  62.         mdata.Bcmt = xpmDataTypes[n].Bcmt;
  63.         mdata.Ecmt = xpmDataTypes[n].Ecmt;
  64.         xpmNextString(&mdata);    /* skip the end of headerline */
  65.         mdata.Bos = xpmDataTypes[n].Bos;
  66.         } else {
  67.         xpmNextString(&mdata);    /* skip the end of headerline */
  68.         mdata.Bcmt = xpmDataTypes[n].Bcmt;
  69.         mdata.Ecmt = xpmDataTypes[n].Ecmt;
  70.         mdata.Bos = xpmDataTypes[n].Bos;
  71.         mdata.Eos = '\0';
  72.         xpmNextString(&mdata);    /* skip the assignment line */
  73.         }
  74.         mdata.Eos = xpmDataTypes[n].Eos;
  75.  
  76.         ErrorStatus = xpmParseData(&mdata, &attrib, &attributes);
  77.     } else
  78.         ErrorStatus = XpmFileInvalid;
  79.     } else
  80.     ErrorStatus = XpmFileInvalid;
  81.  
  82.     if (ErrorStatus == XpmSuccess) {
  83.     int i;
  84.     
  85.     /* maximum of allocated pixels will be the number of colors */
  86.     attributes.pixels = (Pixel *) malloc(sizeof(Pixel) * attrib.ncolors);
  87.     attrib.xcolors = (XColor*) malloc(sizeof(XColor) * attrib.ncolors);
  88.  
  89.     if (!attributes.pixels || !attrib.xcolors)
  90.         ErrorStatus = XpmNoMemory;
  91.     else {
  92.         for (i = 0; i < attrib.ncolors; i++) {
  93.         /* Fake colors */
  94.         attrib.xcolors[i].pixel = attributes.pixels[i] = i + 1;
  95.         }
  96.         xpmSetAttributes(&attrib, &attributes);
  97.         if (!(attrib.colorStrings =
  98.           (char**) malloc(attributes.ncolors * sizeof(char*))))
  99.         ErrorStatus = XpmNoMemory;
  100.         else {
  101.         attrib.ncolors = attributes.ncolors;
  102.         attributes.mask_pixel = attrib.mask_pixel;
  103.         for (i = 0; i < attributes.ncolors; i++)
  104.             attrib.colorStrings[i] = attributes.colorTable[i][0];
  105.         }
  106.     }
  107.     }
  108.     if (ErrorStatus == XpmSuccess)
  109.     ErrorStatus = xpmCreateData(data_return, &attrib, &attributes);
  110.     XpmFreeAttributes(&attributes);
  111.     xpmFreeInternAttrib(&attrib);
  112.     XpmDataClose(&mdata);
  113.  
  114.     return (ErrorStatus);
  115. }
  116.