Mike's Musings

Do you want code with that mongoose?

1

Reading a file in with GnomeVFSmm

Recently I have being working on a project (which I will post about later) in which I wanted to use GnomeVFSmm the c++ port of GnomeVFS, but when I looked on the web I noticed a distinct lack of examples or tutorials using GnomeVFSmm, in the end I managed to figure it out, but for future reference and others looking for similar code here is a function for reading in a file.

bool read_file(Glib::RefPtr uri, Glib::ustring *contents)
{
	if(!uri)
		return false;

	if(!uri->uri_exists())
		return false;

	try {
		Gnome::Vfs::Handle file;

		file.open(uri, Gnome::Vfs::OPEN_READ);

		Glib::RefPtr info = file.get_file_info();

		char buffer[info->get_size() + 1];

		file.read(buffer, info->get_size());
		buffer[info->get_size()] = 0;

		(*contents) = buffer;

		file.close();
	} catch (Gnome::Vfs::exception &ex) {
		// Raise an exception dialog here
		return false;
	}

	return true;
}

Granted GVFS isn’t too far off but until it’s released this might still be helpful.

One Response to “Reading a file in with GnomeVFSmm”

  1. [...] as a while ago I posted on reading a file in with GnomeVFSmm since now and then the folks at GNOME have release GIO their new i/o library.  Thankfully this [...]

Leave a Reply