Re: Can someone please comment on this short program

On Thu, 2005-12-15 at 21:51 +0100, Matthias Kaeppler wrote:
> Alexander Larsson wrote:
> >>So basically "if you construct gnome_vfs_uri's, don't bother with
> >>g_filename_to/from_utf8 but just leave the filename alone and pass it
> >>as-is" ?
> > 
> > 
> > It depends a bit on where you get the filename string from. Either you
> > got the string from e.g. readdir(), or read a filename from a file. In
> > this case you know that the string corresponds to the correct byte
> > string of the filename on disk. You then construct an uri for it like
> > this:
> > 
> > char *uri_string;
> > GnomeVFSURI *uri;
> > uri_string = gnome_vfs_get_uri_from_local_path (pathname);
> > uri = gnome_vfs_uri_new (uri_string);
> > g_free (uri);
> So, converting 'pathname' to something other than its disk encoding can 
> result in 'uri' being broken (this is at least what I am observing in my 
> program), right?

The resulting uri isn't "broken". It just points to a different file
(that likely doesn't exist).

> Is that what you're saying, that the conversion to UTF-8 is only 
> supposed to take place before passing the string to a Gtk widget?

In general, yes. 

> My idea was to convert every name to UTF-8 /once/, so my code won't be 
> cluttered with conversion calls everywhere:
> 1. Read some filename from disk
> 2. If it's not valid UTF-8, convert it using g_filename_to_utf8()
> 3. Pass it to gnome_vfs_get_uri_from_local_path()
> 4. Create a GnomeVFSURI from it
> However, after this procedure, this GnomeVFSUri does not match the real 
> filename anymore, and thus gnome_vfs_uri_exists() returns false for it.
> Isn't there a way to safely and consistently convert all the filenames I 
> read in one place to UTF-8 and store them in GnomeVFSURIS without these 
> side effects?

I'm not sure what you mean. A correctly escaped URI from
gnome_vfs_get_uri_from_local_path() is valid UTF8 (in fact, its ASCII),
and you can treat it as such in all your code. However, if you ever
unescape part of it you may not get UTF8, and if you were unescaping for
display purposes you then must convert to utf8. But if you unescape for
i/o purposes you must not convert to utf8.

There is no way to safely store filenames in utf8. Its quite possible
for a filename to not be convertible to utf8, and its also possible for
e.g. both the locale-encoded and utf8 encoded versions of a string
exists as different files.

> matthias:testing$ G_FILENAME_ENCODING=ISO-8859-15 ./a.out
> exists: true
> exists: true
> exists: false <---- !!

I'm not sure why you put "!!" here. You *changed* the filename, why
would the changed filename exist? The same would happen if you e.g.
converted it to lowercase, or did any other form of modification to the

 Alexander Larsson                                            Red Hat, Inc 
                   alexl redhat com    alla lysator liu se 
He's a suicidal Jewish filmmaker with a mysterious suitcase handcuffed to his 
arm. She's a mentally unstable paranoid widow with a song in her heart and a 
spring in her step. They fight crime! 

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