DO NOT REPLY TO THIS MESSAGE.  INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.

[STR Pending]

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: FL/Fl_Text_Buffer.H
===================================================================
--- FL/Fl_Text_Buffer.H (revision 7966)
+++ FL/Fl_Text_Buffer.H (working copy)
@@ -300,20 +300,21 @@
    non-zero on error (strerror() contains reason).  1 indicates open 
    for read failed (no data loaded). 2 indicates error occurred 
    while reading data (data was partially loaded).
+   File can be UTF-8 or CP1252-encoded.
+   If the input file is not UTF-8-encoded, the Fl_Text_Buffer widget will 
contain
+   UTF-8-recoded data. The message 
Fl_Text_Buffer::file_encoding_warning_message
+   will warn the user about this.
    */
   int insertfile(const char *file, int pos, int buflen = 128*1024);
   
   /**
-   Appends the named file to the end of the buffer. Returns 0 on 
-   success, non-zero on error (strerror() contains reason).  1 indicates 
-   open for read failed (no data loaded). 2 indicates error occurred 
-   while reading data (data was partially loaded).
+   Appends the named file to the end of the buffer. See also insertfile().
    */
   int appendfile(const char *file, int buflen = 128*1024)
   { return insertfile(file, length(), buflen); }
   
   /** 
-   Loads a text file into the buffer 
+   Loads a text file into the buffer. See also insertfile().
    */
   int loadfile(const char *file, int buflen = 128*1024)
   { select(0, length()); remove_selection(); return appendfile(file, buflen); }
@@ -763,6 +764,11 @@
   int mPreferredGapSize;          /**< the default allocation for the text gap 
is 1024
                                    bytes and should only be increased if 
frequent
                                    and large changes in buffer size are 
expected */
+
+  /** This message is displayed using the fl_alert() function when a file
+   which was not UTF-8 encoded is input.
+   */
+  static const char* file_encoding_warning_message;
 };
 
 #endif
Index: src/Fl_Text_Buffer.cxx
===================================================================
--- src/Fl_Text_Buffer.cxx      (revision 7970)
+++ src/Fl_Text_Buffer.cxx      (working copy)
@@ -32,6 +32,7 @@
 #include <ctype.h>
 #include <FL/Fl.H>
 #include <FL/Fl_Text_Buffer.H>
+#include <FL/fl_ask.H>
 
 
 /*
@@ -1514,27 +1515,95 @@
 }
 
 
+static int read_f(char *b, int l, void *d)
+{
+  return fread(b, 1, l, (FILE*)d);
+}
+
 /*
+ filter that produces from an input stream fed by calls to the *read_f 
function 
+ a UTF-8-encoded output stream written in buffer.
+ Input can be UTF-8. If it is not, it is decoded with CP1252.
+ Output is true UTF-8.
+ *input_was_changed is set to true if the input was not strict UTF-8 so output
+ differs from input.
+ */
+static int utf8_input_filter(char *buffer, int buflen, char *line, int sline, 
char* &endline, 
+             int (*read_f)(char *b, int l, void *d), void *rdata, int 
*input_was_changed)
+{
+  char *p, *q, multibyte[5];
+  int l, lp, lq, r;
+  unsigned u;
+  p = endline = line;
+  q = buffer;
+  while (q < buffer + buflen) {
+    if (p >= endline) {
+      r = read_f(line, sline, rdata);
+      endline = line + r; 
+      if (r == 0) return q - buffer;
+      p = line;
+    }
+    l = fl_utf8len1(*p);
+    if (p + l > endline) {
+      memmove(line, p, endline - p);
+      endline -= (p - line);
+      r = read_f(endline, sline - (endline - line), rdata);
+      endline += r;
+      p = line;
+      if (endline - line < l) break;
+    }
+    while ( l > 0) {
+      u = fl_utf8decode(p, p+l, &lp);
+      lq = fl_utf8encode(u, multibyte);
+      if (lp != l || lq != l) *input_was_changed = true;
+      if (q + lq > buffer + buflen) {
+       memmove(line, p, endline - p);
+       endline -= (p - line);
+       return q - buffer;
+      }
+      memcpy(q, multibyte, lq);
+      q += lq; 
+      p += lp;
+      l -= lp;
+    }
+  }
+  memmove(line, p, endline - p);
+  endline -= (p - line);
+  return q - buffer;
+}
+
+const char *Fl_Text_Buffer::file_encoding_warning_message = 
+"Displayed text contains the UTF-8 recoding\n"
+"of file which was not UTF-8 encoded.\n"
+"Some changes may have occurred";
+
+/*
  Insert text from a file.
- Unicode safe. Input must be correct UTF-8!
+ Unicode safe. Input must be correct UTF-8 or is interpreted as CP1252.
+ Output is 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;
-  fseek(fp, 0, SEEK_END);
-  size_t filesize = ftell(fp);
-  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;
+  char *buffer = new char[buflen + 1];  
+  char *endline, line[100];
+  int l;
+  int input_was_changed = false;
+  endline = line;
+  while (true) {
+    l = utf8_input_filter(buffer, buflen, line, sizeof(line), endline, read_f, 
+                         fp, &input_was_changed);
+    if (l == 0) break;
+    buffer[l] = 0;
     insert(pos, buffer);
-  }
+    pos += l;
+    }
   int e = ferror(fp) ? 2 : 0;
   fclose(fp);
   delete[]buffer;
+  if (input_was_changed && !e) fl_alert(file_encoding_warning_message);
   return e;
 }
 
_______________________________________________
fltk-bugs mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-bugs

Reply via email to