glibmm r536 - in trunk: . gio/src tests/giomm_simple



Author: murrayc
Date: Wed Jan 23 09:21:24 2008
New Revision: 536
URL: http://svn.gnome.org/viewvc/glibmm?rev=536&view=rev

Log:
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.

Modified:
   trunk/ChangeLog
   trunk/gio/src/file.ccg
   trunk/gio/src/file.hg
   trunk/tests/giomm_simple/main.cc

Modified: trunk/gio/src/file.ccg
==============================================================================
--- trunk/gio/src/file.ccg	(original)
+++ trunk/gio/src/file.ccg	Wed Jan 23 09:21:24 2008
@@ -1146,4 +1146,25 @@
   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

Modified: trunk/gio/src/file.hg
==============================================================================
--- trunk/gio/src/file.hg	(original)
+++ trunk/gio/src/file.hg	Wed Jan 23 09:21:24 2008
@@ -160,12 +160,25 @@
                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.

Modified: trunk/tests/giomm_simple/main.cc
==============================================================================
--- trunk/tests/giomm_simple/main.cc	(original)
+++ trunk/tests/giomm_simple/main.cc	Wed Jan 23 09:21:24 2008
@@ -5,9 +5,33 @@
 {
   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;
 }
 



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