fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_XPM_Image.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Fl_XPM_Image routines.
00005 //
00006 // Copyright 1997-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 // Contents:
00028 //
00029 //
00030 
00031 //
00032 // Include necessary header files...
00033 //
00034 
00035 #include <FL/Fl.H>
00036 #include <FL/Fl_XPM_Image.H>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <FL/fl_utf8.h>
00040 #include "flstring.h"
00041 
00042 
00043 //
00044 // 'hexdigit()' - Convert a hex digit to an integer.
00045 //
00046 
00047 static int hexdigit(int x) {    // I - Hex digit...
00048   if (isdigit(x)) return x-'0';
00049   if (isupper(x)) return x-'A'+10;
00050   if (islower(x)) return x-'a'+10;
00051   return 20;
00052 }
00053 
00054 #define MAXSIZE 2048
00055 #define INITIALLINES 256
00056 
00061 Fl_XPM_Image::Fl_XPM_Image(const char *name) : Fl_Pixmap((char *const*)0) {
00062   FILE *f;
00063 
00064   if ((f = fl_fopen(name, "rb")) == NULL) return;
00065 
00066   // read all the c-strings out of the file:
00067   char** new_data = new char *[INITIALLINES];
00068   char** temp_data;
00069   int malloc_size = INITIALLINES;
00070   char buffer[MAXSIZE+20];
00071   int i = 0;
00072   while (fgets(buffer,MAXSIZE+20,f)) {
00073     if (buffer[0] != '\"') continue;
00074     char *myp = buffer;
00075     char *q = buffer+1;
00076     while (*q != '\"' && myp < buffer+MAXSIZE) {
00077       if (*q == '\\') switch (*++q) {
00078       case '\r':
00079       case '\n':
00080         if (!fgets(q,(buffer+MAXSIZE+20)-q,f)) { /* no problem if we hit EOF */ } break;
00081       case 0:
00082         break;
00083       case 'x': {
00084         q++;
00085         int n = 0;
00086         for (int x = 0; x < 3; x++) {
00087           int xd = hexdigit(*q);
00088           if (xd > 15) break;
00089           n = (n<<4)+xd;
00090           q++;
00091         }
00092         *myp++ = n;
00093       } break;
00094       default: {
00095         int c = *q++;
00096         if (c>='0' && c<='7') {
00097           c -= '0';
00098           for (int x=0; x<2; x++) {
00099             int xd = hexdigit(*q);
00100             if (xd>7) break;
00101             c = (c<<3)+xd;
00102             q++;
00103           }
00104         }
00105         *myp++ = c;
00106       } break;
00107       } else {
00108         *myp++ = *q++;
00109       }
00110     }
00111     *myp++ = 0;
00112     if (i >= malloc_size) {
00113       temp_data = new char *[malloc_size + INITIALLINES];
00114       memcpy(temp_data, new_data, sizeof(char *) * malloc_size);
00115       delete[] new_data;
00116       new_data = temp_data;
00117       malloc_size += INITIALLINES;
00118     }
00119     new_data[i] = new char[myp-buffer+1];
00120     memcpy(new_data[i], buffer,myp-buffer);
00121     new_data[i][myp-buffer] = 0;
00122     i++;
00123   }
00124 
00125   fclose(f);
00126 
00127   data((const char **)new_data, i);
00128   alloc_data = 1;
00129 
00130   measure();
00131 }
00132 
00133 
00134 //
00135 // End of "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".
00136 //