DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Fl_XPM_Image routines. |
| 5 | // |
| 6 | // Copyright 1997-2010 by Bill Spitzak and others. |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU Library General Public |
| 10 | // License as published by the Free Software Foundation; either |
| 11 | // version 2 of the License, or (at your option) any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // Library General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU Library General Public |
| 19 | // License along with this library; if not, write to the Free Software |
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | // USA. |
| 22 | // |
| 23 | // Please report all bugs and problems on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | // Contents: |
| 28 | // |
| 29 | // |
| 30 | |
| 31 | // |
| 32 | // Include necessary header files... |
| 33 | // |
| 34 | |
| 35 | #include <FL/Fl.H> |
| 36 | #include <FL/Fl_XPM_Image.H> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <FL/fl_utf8.h> |
| 40 | #include "flstring.h" |
| 41 | |
| 42 | |
| 43 | // |
| 44 | // 'hexdigit()' - Convert a hex digit to an integer. |
| 45 | // |
| 46 | |
| 47 | static int hexdigit(int x) { // I - Hex digit... |
| 48 | if (isdigit(x)) return x-'0'; |
| 49 | if (isupper(x)) return x-'A'+10; |
| 50 | if (islower(x)) return x-'a'+10; |
| 51 | return 20; |
| 52 | } |
| 53 | |
| 54 | #define MAXSIZE 2048 |
| 55 | #define INITIALLINES 256 |
| 56 | /** |
| 57 | The constructor loads the XPM image from the name filename. |
| 58 | <P>The destructor free all memory and server resources that are used by |
| 59 | the image. |
| 60 | */ |
| 61 | Fl_XPM_Image::Fl_XPM_Image(const char *name) : Fl_Pixmap((char *const*)0) { |
| 62 | FILE *f; |
| 63 | |
| 64 | if ((f = fl_fopen(name, "rb")) == NULL) return; |
| 65 | |
| 66 | // read all the c-strings out of the file: |
| 67 | char** new_data = new char *[INITIALLINES]; |
| 68 | char** temp_data; |
| 69 | int malloc_size = INITIALLINES; |
| 70 | char buffer[MAXSIZE+20]; |
| 71 | int i = 0; |
| 72 | while (fgets(buffer,MAXSIZE+20,f)) { |
| 73 | if (buffer[0] != '\"') continue; |
| 74 | char *myp = buffer; |
| 75 | char *q = buffer+1; |
| 76 | while (*q != '\"' && myp < buffer+MAXSIZE) { |
| 77 | if (*q == '\\') switch (*++q) { |
| 78 | case '\r': |
| 79 | case '\n': |
| 80 | if (!fgets(q,(buffer+MAXSIZE+20)-q,f)) { /* no problem if we hit EOF */ } break; |
| 81 | case 0: |
| 82 | break; |
| 83 | case 'x': { |
| 84 | q++; |
| 85 | int n = 0; |
| 86 | for (int x = 0; x < 3; x++) { |
| 87 | int xd = hexdigit(*q); |
| 88 | if (xd > 15) break; |
| 89 | n = (n<<4)+xd; |
| 90 | q++; |
| 91 | } |
| 92 | *myp++ = n; |
| 93 | } break; |
| 94 | default: { |
| 95 | int c = *q++; |
| 96 | if (c>='0' && c<='7') { |
| 97 | c -= '0'; |
| 98 | for (int x=0; x<2; x++) { |
| 99 | int xd = hexdigit(*q); |
| 100 | if (xd>7) break; |
| 101 | c = (c<<3)+xd; |
| 102 | q++; |
| 103 | } |
| 104 | } |
| 105 | *myp++ = c; |
| 106 | } break; |
| 107 | } else { |
| 108 | *myp++ = *q++; |
| 109 | } |
| 110 | } |
| 111 | *myp++ = 0; |
| 112 | if (i >= malloc_size) { |
| 113 | temp_data = new char *[malloc_size + INITIALLINES]; |
| 114 | memcpy(temp_data, new_data, sizeof(char *) * malloc_size); |
| 115 | delete[] new_data; |
| 116 | new_data = temp_data; |
| 117 | malloc_size += INITIALLINES; |
| 118 | } |
| 119 | new_data[i] = new char[myp-buffer+1]; |
| 120 | memcpy(new_data[i], buffer,myp-buffer); |
| 121 | new_data[i][myp-buffer] = 0; |
| 122 | i++; |
| 123 | } |
| 124 | |
| 125 | fclose(f); |
| 126 | |
| 127 | data((const char **)new_data, i); |
| 128 | alloc_data = 1; |
| 129 | |
| 130 | measure(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | // |
| 135 | // End of "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 136 | // |