Re: ustring::validate() costs?



On Saturday 03 December 2005 09:18, Matthias Kaeppler wrote:

[snip]

> Yes, with this background, that sounds reasonable. I think it's highly
> irritating though that g_filename_to_utf8() isn't the suggested way to
> convert filenames to UTF-8, but rather g_locale_to_utf8() (I think this
> is essentially what you said right?). It would be very nice to have this
> information in the API reference directly.

I only point I was making on this aspect of your post was that:

        filename = file_info->get_name();
        if (filename.validate())
        {
          Glib::setenv("G_FILENAME_ENCODING", "UTF-8");
          Glib::setenv("G_BROKEN_FILENAMES", "0");
        }
        else
        {
          std::string charset;
          Glib::get_charset(charset);
          std::cout << "Current locale: " << charset << std::endl;
          Glib::setenv("G_FILENAME_ENCODING", charset);
          Glib::setenv("G_BROKEN_FILENAMES", "1");
        }
        // this call throws if the filename contains special chars
        // and is not encoded in UTF-8, how can this happen??
        filename = Glib::filename_to_utf8(filename);

Could be replaced by:
        filename = file_info->get_name();
        if (!filename.validate()) filename = Glib::locale_to_utf8(filename);
which would produce the same result in a more rational way.

I am not saying this is how you should do it.  On a minor point of 
implementation, you should catch any exceptions thrown by 
Glib::locale_to_utf8() because if the filename passed to it is not in your 
locale codeset you will get an exception - which would happen in this code if 
the filename you have extracted is in neither the local system's locale nor 
UTF-8 codesets.  (Likewise you should do the same with Glib::filename_to_utf8
().)

The right way to do it is to set the G_FILENAME_ENCODING or G_BROKEN_FILENAMES 
environmental variable in /etc/profile or an executable shell file 
in /etc/profile.d, depending on your distribution (which is how system wide 
environmental variable should be set), and then use Glib::filename_to_utf8().  
This will enable your code to be usable (if G_FILENAME_ENCODING is 
appropriately set by the user) with filenames in other than the UTF-8 or the 
local system's locale codeset.

Chris




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