Reading utf8 from a FileInputStream



Hi there!

I'm trying to port an application (Referencer) from GnomeVFS to Gio. I
have run into some trouble, and I suspect the problem has to do with the
encoding read from a FileInputStream not "fitting correctly" into a
Glib::ustring. However, both the old and the new code creates a ustring
where validate() returns True.

Using the ported code I get "Exception: Invalid byte sequence in
conversion input" at various places. In addition debug output looks
different, old:
...A Constrainøæåt Solver...
new:
...A Constrain\u00f8\u00e6\u00e5t Solver...

Below is the relevant diff of the changes I have made.

Any help is appreciated, and thanks in advance for your time :-)

==================================
diff -r 3d903830a3dd src/Library.C
--- a/src/Library.C	Mon May 31 16:14:50 2010 +0200
+++ b/src/Library.C	Mon May 31 18:25:23 2010 +0200
@@ -161,48 +160,50 @@
 // True on success
 bool Library::load (Glib::ustring const &libfilename)
 {
-	Gnome::Vfs::Handle libfile;
-
-	Glib::RefPtr<Gnome::Vfs::Uri> liburi = Gnome::Vfs::Uri::create
(libfilename);
+	Glib::RefPtr<Gio::File> libfile = Gio::File::create_for_uri
(libfilename);
+	Glib::RefPtr<Gio::FileInfo> fileinfo = libfile->query_info ();
+	Glib::RefPtr<Gio::FileInputStream> libfile_is;
 
 	Progress progress (tagwindow_);
 
 	progress.start (String::ucompose (
 		_("Opening %1"),
-		liburi->extract_short_name ()));
+		fileinfo->get_display_name ()));
 
 	try {
-		libfile.open (libfilename, Gnome::Vfs::OPEN_READ);
-	} catch (const Gnome::Vfs::exception ex) {
+		libfile_is = libfile->read();
+	} catch (const Gio::Error ex) {
 		Utility::exceptionDialog (&ex, "opening library '"
-			+ Gnome::Vfs::Uri::format_for_display (libfilename) + "'");
+			+ libfile->get_parse_name() + "'");
 		return false;
 	}
 
-	Glib::RefPtr<Gnome::Vfs::FileInfo> fileinfo;
-	fileinfo = libfile.get_file_info ();
 
 	char *buffer = (char *) malloc (sizeof(char) * (fileinfo->get_size() +
1));
+	gsize bytes_read = 0;
 	if (!buffer) {
 		DEBUG ("Warning: RefWindow::loadLibrary: couldn't allocate buffer");
 		return false;
 	}
 
 	try {
-		libfile.read (buffer, fileinfo->get_size());
-	} catch (const Gnome::Vfs::exception ex) {
+		libfile_is->read_all (buffer, fileinfo->get_size(), bytes_read);
+	} catch (const Gio::Error ex) {
 		Utility::exceptionDialog (&ex, "reading library '"
-			+ Gnome::Vfs::Uri::format_for_display (libfilename) + "'");
+			+ libfile->get_parse_name() + "'");
 		free (buffer);
 		return false;
 	}
-	buffer[fileinfo->get_size()] = 0;
+	buffer[bytes_read] = 0;
 
 	progress.update (0.1);
 
 	Glib::ustring rawtext = buffer;
 	free (buffer);
-	libfile.close ();
+	libfile_is->close ();
+
+	DEBUG("Rawtext valid UTF-8: %1", rawtext.validate());
+	//DEBUG(rawtext);
 
 	DEBUG ("Reading XML...");
 	if (!readXML (rawtext)) {
==================================
-- 
Mads Chr. Olesen <shiyee shiyee dk>
shiyee.dk



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]