DO NOT REPLY TO THIS MESSAGE. INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.
[STR New]
Link: http://www.fltk.org/str.php?L2348
Version: 1.3-current
Link: http://www.fltk.org/str.php?L2348
Version: 1.3-current
Index: src/Fl_Text_Buffer.cxx
===================================================================
--- src/Fl_Text_Buffer.cxx (revision 7936)
+++ src/Fl_Text_Buffer.cxx (working copy)
@@ -1516,19 +1516,61 @@
/*
Insert text from a file.
- Unicode safe. Inout must be correct UTF-8!
+ Unicode safe. Input must be correct UTF-8 or is interpreted as CP1252
*/
int Fl_Text_Buffer::insertfile(const char *file, int pos, int buflen)
{
FILE *fp;
if (!(fp = fl_fopen(file, "r")))
return 1;
- char *buffer = new char[buflen];
- for (int r; (r = fread(buffer, 1, buflen - 1, fp)) > 0; pos += r) {
- buffer[r] = (char) 0;
- insert(pos, buffer);
- }
-
+ char *buffer = new char[buflen + 1];
+ char *endline, line[100], *q, *p;
+ int l, sline = sizeof(line), r;
+ q = buffer;
+ while (true) {
+ r = fread(line, 1, sline, fp);
+ if (r == 0) break;
+ endline = line + r;
+ p = line;
+ while (p < endline) {
+ l = fl_utf8len(*p);
+ if (l > 0) {
+ if (p + l > endline) {
+ memmove(line, p, endline - p);
+ endline -= (p - line);
+ r = fread(endline, 1, sline - (endline - line), fp);
+ endline += r;
+ p = line;
+ if (endline - line < l) break;
+ }
+ if (q + l > buffer + buflen) {
+ *q = 0;
+ insert(pos, buffer);
+ pos += strlen(buffer);
+ q = buffer;
+ }
+ memcpy(q, p, l);
+ q += l; p += l;
+ }
+ else {
+ char multibyte[5];
+ unsigned u = fl_utf8decode(p, p, &l);
+ l = fl_utf8encode(u, multibyte);
+ if (q + l > buffer + buflen) {
+ *q = 0;
+ insert(pos, buffer);
+ pos += strlen(buffer);
+ q = buffer;
+ }
+ memcpy(q, multibyte, l);
+ q += l;
+ p++;
+ }
+ }
+ }
+ *q = 0;
+ if (q > buffer) insert(pos, buffer);
+
int e = ferror(fp) ? 2 : 0;
fclose(fp);
delete[]buffer;
_______________________________________________
fltk-bugs mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-bugs