Re: [gtkmm] On Glib::IOChannel wrappers [PATCH]



On 12 Sep 2002, Murray Cumming wrote:

> On Thu, 2002-09-12 at 22:27, Tassos Bassoukos wrote:

[snip]

> >  * IOChannels have the potential for lots of errors, as evidenced by the
> > ever-present GError**. Should these be thrown as C++ exceptions by the
> > wrappers, or should I use a trailing Glib::Error*& parameter?
>
> Doesn't glibmm/gtkmm wrap GErrors as exceptions? Surely you can find
> something similar in glibmm.

Yes, in glib/src/convert.ccg. I'll try to patch the perl & m4 code to
generate the requisite source for methods.

[snip]
> The only thing that ever really makes sense sometime is patches. Patches
> can be criticised and improved.

Ok, but remember: you asked for it! ;-)

Attached is a preliminary patch for the Glib::IOChannel wrappers. It does
not yet throw() errors, does not produce Sources or asynchronous watches.

Tassos

-- 
Bassoukos Tassos   +30 31 996011 / +30 93 7109954       IT Generalist

Unix:        Your gun, Your bullet, Your foot, Your choice.
M$-CE/ME/NT: Same as Unix, BUT: No choice, and We Aim Higher.
                         -- From the Linux S/390 mailing list
--- gtkmm-1.3.orig/glib/src/iochannel.ccg	1970-01-01 02:00:00.000000000 +0200
+++ gtkmm-1.3/glib/src/iochannel.ccg	2002-09-08 09:10:28.000000000 +0300
@@ -0,0 +1,108 @@
+// -*- c++ -*-
+/* $Id: iochannel.ccg,v 1.3 2002/06/19 17:04:05 daniel Exp $ */
+
+/* Copyright (C) 2002 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <glibmm/refptr.h>
+#include <glibmm/iochannel.h>
+
+#include <glib/giochannel.h>
+
+namespace Glib {
+
+  Glib::RefPtr<Glib::IOChannel> IOChannel::create(int fd){
+    return Glib::RefPtr<Glib::IOChannel>(wrap(g_io_channel_unix_new(fd)));
+  }
+
+  Glib::RefPtr<Glib::IOChannel> IOChannel::create(const Glib::ustring& name,
+						  const Glib::ustring& mode,
+						  GError*& error){
+    return Glib::RefPtr<Glib::IOChannel>(wrap(g_io_channel_new_file(name.c_str(),mode.c_str(),&(error)),false));
+  }
+
+  /* FIXME: should we return the number of bytes written? */
+  IOStatus IOChannel::write(const Glib::ustring& string,GError*&error){
+    gsize bytes_written;
+    gsize bytes_count=string.bytes();
+    return write(string.c_str(),bytes_count,bytes_written,error);
+  }
+
+  IOStatus IOChannel::read_line(Glib::ustring& line,GError*&error){
+    gchar *str;
+    gsize len;
+    IOStatus st=(IOStatus)g_io_channel_read_line(gobj(),&str,
+						 &len,NULL,&(error));
+
+    if(len==0 || str==0){
+      line="";
+      return st;
+    }
+    line.assign(str,len);
+    g_free(str);
+    return st;
+  }
+
+  IOStatus IOChannel::read_to_end(Glib::ustring& line,GError*&error){
+    gchar *str;
+    gsize len;
+    IOStatus st=(IOStatus)g_io_channel_read_to_end(gobj(),&str,&len,&(error));
+    
+    line.assign(str,len);
+    g_free(str);
+    return st;
+  }
+  
+  IOStatus IOChannel::read(Glib::ustring& str,int maxchars,GError*& error){
+    str.resize(maxchars);
+    gsize len;
+    IOStatus st=(IOStatus)
+      g_io_channel_read_chars(gobj(),const_cast<gchar*>(str.c_str()),maxchars,
+			      &len,&(error));
+    str.resize(len);
+    return st;
+  }
+  
+  /* FIXME: currently breaks on NULL, e.g. autodetection */
+  Glib::ustring IOChannel::get_line_term() {
+    const gchar *str;
+    int len;
+    str=g_io_channel_get_line_term(gobj(),&len);
+    return Glib::ustring(str,len);
+  }
+
+  void IOChannel::set_line_term(const Glib::ustring& term) {
+    if(term==Glib::ustring(0) || term==""){
+      g_io_channel_set_line_term(gobj(),0,0);
+    } else {
+      g_io_channel_set_line_term(gobj(),term.c_str(),term.length());
+    }
+  }
+
+  IOStatus IOChannel::set_encoding(const Glib::ustring& str,GError*& err){
+    if(str==Glib::ustring(0) || str==""){
+      return (IOStatus)g_io_channel_set_encoding(gobj(),0,&(err));
+    } else {
+      return (IOStatus)g_io_channel_set_encoding(gobj(),str.c_str(),&(err));
+    }
+  }
+
+  Glib::ustring IOChannel::get_encoding(){
+    return Glib::ustring(g_io_channel_get_encoding(gobj()));
+  }
+
+};
--- gtkmm-1.3.orig/glib/src/iochannel.hg	1970-01-01 02:00:00.000000000 +0200
+++ gtkmm-1.3/glib/src/iochannel.hg	2002-09-08 09:08:35.000000000 +0300
@@ -0,0 +1,159 @@
+// -*- c++ -*-
+/* $Id: iochannel.hg,v 1.3 2002/06/19 17:04:05 daniel Exp $ */
+
+/* Copyright (C) 2002 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+_DEFS(glibmm,glib)
+
+#include <glibmm/error.h>
+#include <glibmm/refptr.h>
+#include <glibmm/main.h>
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+extern "C" { 
+  typedef struct _GIOChannel GIOChannel; 
+  typedef struct _GSource GSource;
+}
+#endif
+
+
+namespace Glib
+{
+
+_WRAP_ENUM(SeekType, GSeekType, NO_GTYPE,
+	s#SEEK_##,
+	s#^CUR$#CURRENT#)
+_WRAP_ENUM(IOStatus, GIOStatus, NO_GTYPE,
+	s#IO_STATUS_##,
+        s#^EOF$#END_OF_FILE#,
+	s#^AGAIN$#TRYAGAIN#)
+
+enum IOFlags {
+  APPEND = 1 << 0,
+  NONBLOCK = 1 << 1,
+  IS_READABLE = 1 << 2,	 /* Read only flag */
+  IS_WRITEABLE = 1 << 3, /* Read only flag */
+  IS_SEEKABLE = 1 << 4,	 /* Read only flag */
+  GET_MASK = (1 << 5) - 1,
+  SET_MASK = (1 << 0) | (1 << 1)
+};
+
+/** @ingroup glibmmEnums */
+inline IOFlags operator|(IOFlags lhs, IOFlags rhs)
+  { return static_cast<IOFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); }
+
+/** @ingroup glibmmEnums */
+inline IOFlags operator&(IOFlags lhs, IOFlags rhs)
+  { return static_cast<IOFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); }
+
+/** @ingroup glibmmEnums */
+inline IOFlags operator~(IOFlags flags)
+  { return static_cast<IOFlags>(~static_cast<unsigned>(flags)); }
+
+
+
+/** Exception class for file operation errors.
+ */	
+_WRAP_GERROR(IOError, GIOChannelError, G_IO_CHANNEL_ERROR, NO_GTYPE,
+	s#^ISDIR$#IS_DIRECTORY#,
+	s#^PIPE$#BROKEN_PIPE#,
+	s#^NOSPC$#NO_SPACE_LEFT#,
+	s#^NXIO$#NO_SUCH_DEVICE#,
+	s#^ACCES$#ACCESS_DENIED#,
+	s#^FBIG$#FILE_TOO_BIG#,
+	s#^IO$#IO_ERROR#)
+
+class IOChannel
+{
+  _CLASS_OPAQUE_REFCOUNTED(IOChannel, GIOChannel,NONE, 
+                           g_io_channel_ref,g_io_channel_unref)
+
+  _IGNORE(g_io_channel_ref,g_io_channel_unref,g_io_channel_init)
+  _IGNORE(g_io_channel_win32_get_fd,g_io_channel_win32_make_pollfd)
+
+  _IGNORE(g_io_channel_unix_new)
+
+  _IGNORE(g_io_channel_read_line,g_io_channel_read_to_end)
+  _IGNORE(g_io_channel_read_line_string)
+
+  /* deprecated */
+  _IGNORE(g_io_channel_read,g_io_channel_write)
+  _IGNORE(g_io_channel_seek,g_io_channel_close)
+
+public:
+
+  static Glib::RefPtr<Glib::IOChannel> create(int fd);
+
+  static Glib::RefPtr<Glib::IOChannel> create(const Glib::ustring& filename,
+					      const Glib::ustring& mode,
+					      GError*& error);
+
+  _WRAP_METHOD(int get_fd(),g_io_channel_unix_get_fd)
+
+  _WRAP_METHOD(IOStatus read(gunichar& unichar,GError*&error),
+	       g_io_channel_read_unichar)
+  _WRAP_METHOD(IOStatus read(gchar *str,gsize count, 
+			     gsize& bytes_read,GError*& error),
+	       g_io_channel_read_chars)
+  IOStatus read(Glib::ustring& str, int maxchars, GError*&error);
+  IOStatus read_line(Glib::ustring& line, GError*&error);
+  IOStatus read_to_end(Glib::ustring& line, GError*&error);
+
+  IOStatus write(const Glib::ustring& str,GError*&error);
+  _WRAP_METHOD(IOStatus write(const gchar *str,gssize len,
+			      gsize& bytes_written,GError*&error),
+	       g_io_channel_write_chars)
+  _WRAP_METHOD(IOStatus write(gunichar unichar,GError*&error),
+	       g_io_channel_write_unichar)
+
+    /* FIXME: int should be guint64 */
+  _WRAP_METHOD(IOStatus seek_position(int pos,SeekType where,GError*&error),
+               g_io_channel_seek_position)
+  _WRAP_METHOD(IOStatus flush(GError*&error), g_io_channel_flush)
+  _WRAP_METHOD(IOStatus shutdown(bool flush, GError*&error), 
+               g_io_channel_shutdown)
+
+  _WRAP_METHOD(void set_buffer_size(gsize size), g_io_channel_set_buffer_size)
+  _WRAP_METHOD(gsize get_buffer_size(),g_io_channel_get_buffer_size)
+
+  _WRAP_METHOD(IOFlags get_flags(),g_io_channel_get_flags)
+  _WRAP_METHOD(IOStatus set_flags(IOFlags f,GError*& error),
+	       g_io_channel_set_flags)
+
+  _WRAP_METHOD(bool get_buffered(),g_io_channel_get_buffered)
+  _WRAP_METHOD(void set_buffered(bool buffered),g_io_channel_set_buffered)
+  _WRAP_METHOD(IOCondition get_buffer_condition(),
+               g_io_channel_get_buffer_condition)
+
+  _WRAP_METHOD(bool get_close_on_unref(),g_io_channel_get_close_on_unref)
+  _WRAP_METHOD(void set_close_on_unref(bool buffered),
+               g_io_channel_set_close_on_unref)
+  
+  _IGNORE(g_io_channel_get_encoding)
+  Glib::ustring get_encoding();
+  _IGNORE(g_io_channel_set_encoding)
+  IOStatus set_encoding(const Glib::ustring& encoding,GError*& error);
+
+  _IGNORE(g_io_channel_get_line_term)
+  Glib::ustring get_line_term();
+  _IGNORE(g_io_channel_set_line_term)
+  void set_line_term(const Glib::ustring& term);
+
+};
+
+} // Namespace Glib
--- gtkmm-1.3.orig/glib/src/Makefile_list_of_hg.am_fragment	2002-06-23 18:28:22.000000000 +0300
+++ gtkmm-1.3/glib/src/Makefile_list_of_hg.am_fragment	2002-08-24 16:01:24.000000000 +0300
@@ -5,4 +5,4 @@
 
 files_posix_hg =
 files_win32_hg =
-files_general_hg = convert.hg date.hg fileutils.hg module.hg shell.hg spawn.hg thread.hg unicode.hg
+files_general_hg = convert.hg date.hg fileutils.hg iochannel.hg module.hg shell.hg spawn.hg thread.hg unicode.hg
--- gtkmm-1.3.orig/tools/m4/convert_glib.m4	1970-01-01 02:00:00.000000000 +0200
+++ gtkmm-1.3/tools/m4/convert_glib.m4	2002-09-03 17:35:22.000000000 +0300
@@ -0,0 +1,24 @@
+dnl 
+dnl Glib C names have prefix 'G' but C++ namespace Glib
+dnl 
+define(`__ARG2__',`$`'3')
+define(`_CONV_GLIB_ENUM',`dnl
+_CONVERSION(`G$1', `$1', (($1)(__ARG2__)))
+_CONVERSION(`G$1', `Glib::$1', ((Glib::$1)(__ARG2__)))
+_CONVERSION(`$1', `G$1', ((G$1)(__ARG2__)))
+_CONVERSION(`Glib::$1', `G$1', ((G$1)(__ARG2__)))
+')dnl
+
+_CONV_GLIB_ENUM(IOStatus)
+_CONV_GLIB_ENUM(IOFlags)
+_CONV_GLIB_ENUM(IOCondition)
+_CONV_GLIB_ENUM(SeekType)
+
+_CONVERSION(`gunichar*&',`gunichar**',`&($3)')
+_CONVERSION(`gunichar&',`gunichar*',`&($3)')
+_CONVERSION(`gsize&',`gsize*',`&($3)')
+
+/* FIXME: precision loss -  perhaps should be long int? */
+_CONVERSION(`int',`gint64',`$3')
+_CONVERSION(`gint64',`int',`$3')
+
--- gtkmm-1.3.orig/tools/m4/convert_gtkmm.m4	2001-12-04 00:19:56.000000000 +0200
+++ gtkmm-1.3/tools/m4/convert_gtkmm.m4	2002-09-02 16:28:27.000000000 +0300
@@ -4,4 +4,5 @@
 include(convert_gtk.m4)
 include(convert_pango.m4)
 include(convert_gdk.m4)
-include(convert_atk.m4)
\ No newline at end of file
+include(convert_atk.m4)
+include(convert_glib.m4)
--- gtkmm-1.3.orig/tools/m4/convert_base.m4	2002-07-31 11:40:44.000000000 +0300
+++ gtkmm-1.3/tools/m4/convert_base.m4	2002-09-02 16:55:01.000000000 +0300
@@ -65,4 +65,5 @@
 include(convert_pango.m4)
 include(convert_gdk.m4)
 include(convert_atk.m4)
+include(convert_glib.m4)
 
--- gtkmm-1.3.orig/glib/glibmm/Makefile.am	2002-08-06 13:05:34.000000000 +0300
+++ gtkmm-1.3/glib/glibmm/Makefile.am	2002-08-24 16:02:16.000000000 +0300
@@ -57,6 +57,7 @@
 	exceptionhandler.h		\
 	helperlist.h			\
 	interface.h			\
+	iochannel.h			\
 	listhandle.h			\
 	main.h				\
 	miscutils.h			\


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