Re: Can someone please comment on this short program



Murray Cumming wrote:
On Wed, 2005-12-14 at 17:28 +0100, Matthias Kaeppler wrote:

Example:
	RefPtr<Uri> uri = Uri::create("t�");

matthias:testing$ ./a.out
terminate called after throwing an instance of 'Glib::ConvertError'
Uri: Aborted


That's happening when you use std::cout in the next line. std::cout
doesn't understand UTF-8 (C++ hasn't caught up with the 21st century
completely). The std::string must be converted to your locale, from
UTF8. But it's not UTF8 so the conversion fails when doing
g_locale_to_utf8() in operator<<.

Alright, I have cut all the lines which are cout'ing the filename from the test case now. Thanks.

So, I tried using g_filename_display_name() to get some UTF-8 for this
filename. g_utf8_validate() says that the display name is OK, but
g_locale_from_utf8() reports an error.

The attached test case is all C. If I'm doing something wrong then it
should be obvious. Forgive the char* leaks.

That's really strange. I played around with your code after getting the same error, and noticed that using g_filename_from_utf8() instead of g_locale_from_utf8() will get rid of the conversion error--but shouldn't do g_filename_from_utf8() just call g_locale_from_utf8() anyway if I don't explicitly say otherwise in G_FILENAME_ENCODING??

I have attached the test case again with the modification; the output is now for me:

matthias:testing$ gcc -g3 test_conly.c `pkg-config --cflags --libs gnome-vfsmm-2.6`
matthias:testing$ ./a.out
** Message: display name: file:t?st

You may notice the '?'. My idea is that g_filename_display_name() checks for unexpected characters and silently replaces them with a dummy character, so UTF-8 validation succeeds thereafter. I'm just guessing though (and this still doesn't explain why conversion using g_filename_from_utf8() succeeds).

Regards,
Matthias

#include <libgnomevfs/gnome-vfs-init.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <glib.h>
#include <string.h>

int main(int argc, char** argv)
{
     gnome_vfs_init();
   
     const char* filename = "täst";
     char* uri_as_string = 0;
     char* uri_as_string_displayable = 0;
     const char* parse_end = 0;
     gboolean is_valid_utf8 = FALSE;


     //Create VFS URI from dodgy filename:
     GnomeVFSURI* uri = gnome_vfs_uri_new(filename);
     g_assert(uri);
  
     //Get a displayable name for that dodgy filename:
     uri_as_string = gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE);
     g_assert(uri_as_string);

     uri_as_string_displayable = g_filename_display_name(uri_as_string);
     g_assert(uri_as_string_displayable);

     //Check that it is really displayable UTF-8:
     is_valid_utf8 = g_utf8_validate(uri_as_string_displayable, strlen(uri_as_string_displayable), &parse_end);
     g_assert(is_valid_utf8); //Strangely, this does not fail, but g_locale_from_utf8() does fail.


     //Try converting the displayable (in GTK+) UTF-8 string to the locale encoding.
     {
       gsize bytes_written = 0;
       GError* error = 0;
       const char* as_locale = g_filename_from_utf8(uri_as_string_displayable, strlen(uri_as_string_displayable), 0, &bytes_written, &error);

       if(error != 0)
         g_message("GError: %s", error->message);
       else
         g_message("display name: %s", as_locale);
     }

     return 0;
}


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