Re: Can someone please comment on this short program

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<<.

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.

> The vicious circle here is that by converting the argument safely to 
> UTF-8 to let create() succeed, I thereby break functions like uri_exists().

Again, I don't think that the string is being changed at all before the
uri_exists() check happens. That's maybe some other problem.

Murray Cumming
murrayc murrayc com
#include <libgnomevfs/gnome-vfs-init.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <glib.h>
#include <string.h>

int main(int argc, char** argv)
     const char* filename = "t�";
     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);
     //Get a displayable name for that dodgy filename:
     uri_as_string = gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE);

     uri_as_string_displayable = g_filename_display_name(uri_as_string);

     //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_locale_from_utf8(uri_as_string_displayable, strlen(uri_as_string_displayable), 0, &bytes_written, &error);

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


     return 0;

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