DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: Fl_Browser_load.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // File loading routines for the Fast Light Tool Kit (FLTK). |
| 5 | // |
| 6 | // Copyright 1998-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 | |
| 28 | #include <FL/Fl.H> |
| 29 | #include <FL/Fl_Browser.H> |
| 30 | #include <stdio.h> |
| 31 | #include <FL/fl_utf8.h> |
| 32 | |
| 33 | /** |
| 34 | Clears the browser and reads the file, adding each line from the file |
| 35 | to the browser. If the filename is NULL or a zero-length |
| 36 | string then this just clears the browser. This returns zero if there |
| 37 | was any error in opening or reading the file, in which case errno |
| 38 | is set to the system error. The data() of each line is set |
| 39 | to NULL. |
| 40 | \param[in] filename The filename to load |
| 41 | \returns 1 if OK, 0 on error (errno has reason) |
| 42 | \see add() |
| 43 | */ |
| 44 | int Fl_Browser::load(const char *filename) { |
| 45 | #define MAXFL_BLINE 1024 |
| 46 | char newtext[MAXFL_BLINE]; |
| 47 | int c; |
| 48 | int i; |
| 49 | clear(); |
| 50 | if (!filename || !(filename[0])) return 1; |
| 51 | FILE *fl = fl_fopen(filename,"r"); |
| 52 | if (!fl) return 0; |
| 53 | i = 0; |
| 54 | do { |
| 55 | c = getc(fl); |
| 56 | if (c == '\n' || c <= 0 || i>=(MAXFL_BLINE-1)) { |
| 57 | newtext[i] = 0; |
| 58 | add(newtext); |
| 59 | i = 0; |
| 60 | } else { |
| 61 | newtext[i++] = c; |
| 62 | } |
| 63 | } while (c >= 0); |
| 64 | fclose(fl); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | // |
| 69 | // End of "$Id: Fl_Browser_load.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 70 | // |