diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2010-12-08 14:47:11 +0000 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2010-12-08 14:47:11 +0000 |
| commit | ee3e8180b40d11a5d08b6fefd5ae79f2d27d6e82 (patch) | |
| tree | 06a13fc60314f99fc31de98ea6f6185eb141333b | |
| parent | 97b4b0c704aef8194ab0f5d814d39452d0b3221f (diff) | |
Fixed Windows text file line endings, as discussed in STR 2348 and
fltk.development. Side effect: All Windows text files written are
in Windows, aka DOS format (with CR/LF line endings), no matter what
format they had when read. This is compatible with FLTK 1.1.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7979 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | src/Fl_Text_Buffer.cxx | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx index 0f5b635ba..abafcc29b 100644 --- a/src/Fl_Text_Buffer.cxx +++ b/src/Fl_Text_Buffer.cxx @@ -1518,8 +1518,7 @@ int Fl_Text_Buffer::findchar_backward(int startPos, unsigned int searchChar, Insert text from a file. Unicode safe. Input must be correct UTF-8! */ -int Fl_Text_Buffer::insertfile(const char *file, int pos, int /*buflen*/) -{ +int Fl_Text_Buffer::insertfile(const char *file, int pos, int /*buflen*/) { FILE *fp; if (!(fp = fl_fopen(file, "r"))) return 1; @@ -1528,8 +1527,11 @@ int Fl_Text_Buffer::insertfile(const char *file, int pos, int /*buflen*/) fseek(fp, 0, SEEK_SET); if (!filesize) return 0; char *buffer = new char[filesize+1]; - if (fread(buffer, 1, filesize, fp)==filesize) { - buffer[filesize] = (char) 0; + // Note: If we read Windows text files in text mode, then Windows + // strips the <CR>'s from the text. Hence, rsize < filesize ! + size_t rsize = fread(buffer, 1, filesize, fp); + if (rsize > 0) { + buffer[rsize] = (char) 0; insert(pos, buffer); } int e = ferror(fp) ? 2 : 0; @@ -1543,11 +1545,11 @@ int Fl_Text_Buffer::insertfile(const char *file, int pos, int /*buflen*/) Write text to file. Unicode safe. */ -int Fl_Text_Buffer::outputfile(const char *file, int start, int end, - int buflen) -{ +int Fl_Text_Buffer::outputfile(const char *file, + int start, int end, + int buflen) { FILE *fp; - if (!(fp = fl_fopen(file, "wb"))) + if (!(fp = fl_fopen(file, "w"))) return 1; for (int n; (n = min(end - start, buflen)); start += n) { const char *p = text_range(start, start + n); |
