Glib::ConvertError (or: I obviously still don't understand Glib::ustring)



Hi.

additionally to the other thread going on about Glib::ustring and the (im)proper usage thereof, I have found an error in my code, which I (despite the pretty good answers in that other thread) still don't understand.

I am reading directory contents into a list using gnome-vfs. For each entry in a directory, DirectoryHandle gives me a FileInfo object. This object can deliver the filename as a std::string.

Okay, since I don't want any std::strings no more, I thought this would be the perfect reason to use Glib::filename_to_utf8() to convert it to a Glib::ustring. But this conversion fails as soon as the program arrives at a file I intentionally created such that it contains a German umlaut (the file in question is named "tästfile"):

    // open current directory
    DirectoryHandle dir;
    FileInfoOptions options =
	FILE_INFO_FOLLOW_LINKS |
	FILE_INFO_GET_MIME_TYPE |
	FILE_INFO_FORCE_FAST_MIME_TYPE;

    dir.open(working_directory_.get_uri_string(), options);

    Glib::ustring filename;
    Glib::RefPtr<FileInfo> file_info;
    bool file_exists = true;

    // iterate over directory content
    while (true)
    {
	file_info = dir.read_next(file_exists);
	if (!file_exists)
	    break;

	try
	{
            // the next line throws with the following reason:
            // "Invalid byte sequence in conversion input"
            // the file in question is called "tästfile"
            // (note the umlaut)
	    filename = Glib::filename_to_utf8(file_info->get_name());
	    std::cout << filename << std::endl;
	}
	catch (const Glib::ConvertError& e)
	{
	    std::cerr << e.what() << std::endl;
	    exit(1);
	}

	if (filename == "." || filename == "/")
	    continue;

	File file (working_directory_);
	if (::is_directory(file_info))
	    file.append_path(filename);
	else
	    file.append_filename(filename);

	files_.push_back(file);
    }

I tried to replace filename_to_utf8() with locale_to_utf8() (I still don't get where the difference is, really, but I thought it was worth a try). Didn't work either: The conversion itself suddenly didn't fail anymore, but as soon as I try to create a Gnome::Vfs::Uri from it, I am getting an exception thrown with the reason: "File does not exist." (yet it does exist of course).

I'm lost.

Regards,
Matthias

PS: Actually I'm no friend of filenames containing Umlauts and other weird characters, although I am German myself, but I want to get this to work, for the heck of it. :)




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