Re: Can someone please comment on this short program

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

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?

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?

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?


-------------------------------- 8< ------------------------------------
// example (C++):

int main()
    using namespace Gnome::Vfs;

    DirectoryHandle dir;
    bool has_next = true;
    std::string path = "/home/matthias/test";;

    while (true)
	    Glib::RefPtr<FileInfo> finfo = dir.read_next(has_next);
	    if (!has_next)
	    Glib::ustring filename =
		Glib::build_filename(path, finfo->get_name());

	    if (!filename.validate())
		filename = Glib::filename_to_utf8(filename);

	    assert (filename.validate());

	    Glib::ustring uristr = get_uri_from_local_path(filename);
	    Glib::RefPtr<Uri> uri = Uri::create(uristr);

	    std::cout << "exists: " << uri->uri_exists() << std::endl;
	catch (Glib::Error& e)
	    std::cout << e.what() << std::endl;
-------------------------------- 8< ------------------------------------
Files in directory:

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

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