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 / XpmWrFFrData.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  3KB  |  114 lines

  1. /* Copyright 1990,91 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * XpmWrFFrData.c:                                                             *
  4. *                                                                             *
  5. *  XPM library                                                                *
  6. *  Parse an Xpm array and write a file that corresponds to it.                *
  7. *                                                                             *
  8. *  Developed by Dan Greening dgreen@cs.ucla.edu / dgreen@sti.com              *
  9. \*****************************************************************************/
  10.  
  11. #include "xpmP.h"
  12. #ifdef VMS
  13. #include "sys$library:string.h"
  14. #else
  15. #ifdef SYSV
  16. #include <string.h>
  17. #define index strchr
  18. #define rindex strrchr
  19. #else
  20. #include <strings.h>
  21. #endif
  22. #endif
  23.  
  24. int
  25. XpmWriteFileFromData(filename, data)
  26.     char *filename;
  27.     char **data;
  28. {
  29.     xpmData mdata, mfile;
  30.     char *name, *dot, *s, *new_name = NULL;
  31.     int ErrorStatus;
  32.     XpmAttributes attributes;
  33.     xpmInternAttrib attrib;
  34.     int i;
  35.  
  36.     attributes.valuemask = XpmReturnPixels|XpmReturnInfos|XpmReturnExtensions;
  37.     if ((ErrorStatus = xpmWriteFile(filename, &mfile)) != XpmSuccess)
  38.     return (ErrorStatus);
  39.  
  40.     if (filename) {
  41. #ifdef VMS
  42.     name = filename;
  43. #else
  44.     if (!(name = rindex(filename, '/')))
  45.         name = filename;
  46.     else
  47.         name++;
  48. #endif
  49.     if (dot = index(name, '.')) {
  50.         new_name = (char*)strdup(name);
  51.         if (!new_name) {
  52.         new_name = NULL;
  53.         name = "image_name";
  54.         } else {
  55.         /* change '.' to '_' to get a valid C syntax name */
  56.         name = s = new_name;
  57.         while (dot = index(s, '.')) {
  58.             *dot = '_';
  59.             s = dot;
  60.         }
  61.         }
  62.     }
  63.     } else
  64.     name = "image_name";
  65.  
  66.     xpmInitInternAttrib(&attrib);
  67.  
  68.     /*
  69.      * Parse data then write it out 
  70.      */
  71.  
  72.     xpmOpenArray(data, &mdata);
  73.  
  74.     ErrorStatus = xpmParseData(&mdata, &attrib, &attributes);
  75.     if (ErrorStatus == XpmSuccess) {
  76.     attributes.mask_pixel = UNDEF_PIXEL;
  77.  
  78.     /* maximum of allocated pixels will be the number of colors */
  79.     attributes.pixels = (Pixel *) malloc(sizeof(Pixel) * attrib.ncolors);
  80.     attrib.xcolors = (XColor*) malloc(sizeof(XColor) * attrib.ncolors);
  81.  
  82.     if (!attributes.pixels || !attrib.xcolors)
  83.         ErrorStatus == XpmNoMemory;
  84.     else {
  85.         int i;
  86.  
  87.         for (i = 0; i < attrib.ncolors; i++) {
  88.         /* Fake colors */
  89.         attrib.xcolors[i].pixel = attributes.pixels[i] = i + 1;
  90.         }
  91.         xpmSetAttributes(&attrib, &attributes);
  92.         if (!(attrib.colorStrings =
  93.           (char**) malloc(attributes.ncolors * sizeof(char*))))
  94.         ErrorStatus == XpmNoMemory;
  95.         else {
  96.         attrib.ncolors = attributes.ncolors;
  97.         for (i = 0; i < attributes.ncolors; i++)
  98.             attrib.colorStrings[i] = attributes.colorTable[i][0];
  99.  
  100.         attrib.name = name;
  101.         ErrorStatus = xpmWriteData(&mfile, &attrib, &attributes);
  102.         }
  103.     }
  104.     }
  105.     if (new_name)
  106.     free(name);
  107.     XpmFreeAttributes(&attributes);
  108.     xpmFreeInternAttrib(&attrib);
  109.     XpmDataClose(&mfile);
  110.     XpmDataClose(&mdata);
  111.  
  112.     return (ErrorStatus);
  113. }
  114.