diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | gio/src/file.ccg | 21 | ||||
-rw-r--r-- | gio/src/file.hg | 19 | ||||
-rw-r--r-- | tests/giomm_simple/main.cc | 30 |
4 files changed, 73 insertions, 6 deletions
@@ -1,3 +1,12 @@ +2008-01-23 Murray Cumming <murrayc@murrayc.com> + + * gio/src/file.ccg: + * gio/src/file.hg: Added a read() method overload with no + cancellable parameter. + * tests/giomm_simple/main.cc: Try loading the contents of a file. + Seems to work, though there is an unknown GError domain when the file + does not exist. + 2.15.2: 2008-01-21 Murray Cumming <murrayc@murrayc.com> diff --git a/gio/src/file.ccg b/gio/src/file.ccg index fea3f4ff..de79b19b 100644 --- a/gio/src/file.ccg +++ b/gio/src/file.ccg @@ -1146,4 +1146,25 @@ Glib::RefPtr<FileMonitor> File::monitor_file(FileMonitorFlags flags, std::auto_ return retvalue; } +#ifdef GLIBMM_EXCEPTIONS_ENABLED +Glib::RefPtr<FileInputStream> File::read() +#else +Glib::RefPtr<FileInputStream> File::read(std::auto_ptr<Glib::Error>& error) +#endif //GLIBMM_EXCEPTIONS_ENABLED +{ + GError* gerror = 0; + Glib::RefPtr<FileInputStream> retvalue = Glib::wrap(g_file_read(gobj(), NULL, &(gerror))); +#ifdef GLIBMM_EXCEPTIONS_ENABLED + if(gerror) + ::Glib::Error::throw_exception(gerror); +#else + if(gerror) + error = ::Glib::Error::throw_exception(gerror); +#endif //GLIBMM_EXCEPTIONS_ENABLED + + if(retvalue) + retvalue->reference(); //The function does not do a ref for us. + return retvalue; +} + } // namespace Gio diff --git a/gio/src/file.hg b/gio/src/file.hg index cbf2cd9c..693aff58 100644 --- a/gio/src/file.hg +++ b/gio/src/file.hg @@ -160,12 +160,25 @@ public: g_file_has_uri_scheme) _WRAP_METHOD(std::string get_uri_scheme() const, g_file_get_uri_scheme) + + //TODO: We don't have both const and unconst versions because a FileInputStream can't really change the File. _WRAP_METHOD(Glib::RefPtr<FileInputStream> read(const Glib::RefPtr<Cancellable>& cancellable), g_file_read, refreturn, errthrow) - _WRAP_METHOD(Glib::RefPtr<const FileInputStream> read(const Glib::RefPtr<Cancellable>& cancellable) const, - g_file_read, - refreturn, constversion, errthrow) + + /** Opens a file for reading. The result is a FileInputStream that + * can be used to read the contents of the file. + * + * If the file does not exist, the IO_ERROR_NOT_FOUND error will be returned. + * If the file is a directory, theIO_ERROR_IS_DIRECTORY error will be returned. + * Other errors are possible too, and depend on what kind of filesystem the file is on. + * @return FileInputStream or an empty RefPtr on error. + */ + #ifdef GLIBMM_EXCEPTIONS_ENABLED + Glib::RefPtr<FileInputStream> read(); + #else + Glib::RefPtr<FileInputStream> read(std::auto_ptr<Glib::Error>& error); + #endif //GLIBMM_EXCEPTIONS_ENABLED /** Asynchronously opens the file for reading. * For more details, see read() which is the synchronous version of this call. diff --git a/tests/giomm_simple/main.cc b/tests/giomm_simple/main.cc index e0062f25..470ceea0 100644 --- a/tests/giomm_simple/main.cc +++ b/tests/giomm_simple/main.cc @@ -5,9 +5,33 @@ int main(int argc, char** argv) { Glib::init(); Gio::init(); - - Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/home/murrayc/test.txt"); - + + try + { + Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/home/murrayc/test.txt"); + if(!file) + std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl; + + Glib::RefPtr<Gio::FileInputStream> stream = file->read(); + if(!stream) + std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl; + + gchar buffer[1000]; //TODO: This is unpleasant. + memset(buffer, 0, 1000); + const gsize bytes_read = stream->read(buffer, 1000); + + if(bytes_read) + std::cout << "File contents read: " << buffer << std::endl; + else + std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl; + + } + catch(const Glib::Exception& ex) + { + std::cerr << "Exception caught: " << ex.what() << std::endl; + } + + return 0; } |