Hi ruby-gnome2,
For my personnal needs with Ruby GStreamer, I needed to access the
g_string_* stuff in GLib.
Here is a first try to implement the g_string_* stuff in a ruby class.
Please tell me how to improve/add/remove things if necessary, so it
could be integrated as soon as possible.
–
[PATCH] Added GString
Signed-off-by: Lionel L. [email protected]
glib/src/rbglib.c | 10 ±
glib/src/rbglib_string.c | 329
++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 335 insertions(+), 4 deletions(-)
create mode 100644 glib/src/rbglib_string.c
diff --git a/glib/src/rbglib.c b/glib/src/rbglib.c
index f163e89…e68cbab 100644
— a/glib/src/rbglib.c
+++ b/glib/src/rbglib.c
@@ -42,6 +42,7 @@ extern void Init_glib_timer();
extern void Init_glib_unicode();
extern void Init_glib_keyfile();
extern void Init_glib_bookmark_file();
+extern void Init_glib_string();
const gchar *
rbg_rval_inspect(VALUE object)
@@ -97,14 +98,14 @@ So they occur “cross-thread violation”.
static gpointer
my_malloc(gsize n_bytes)
{
- /* Should we rescue NoMemoryError? */
- /* Should we rescue NoMemoryError? */
return ruby_xmalloc(n_bytes);
}
static gpointer
my_realloc(gpointer mem, gsize n_bytes)
{
- /* Should we rescue NoMemoryError? */
- /* Should we rescue NoMemoryError? */
return ruby_xrealloc(mem, n_bytes);
}
@@ -162,7 +163,7 @@ rbg_s_os_unix(self)
#endif
}
-void
+void
Init_glib2()
{
id_inspect = rb_intern(“inspect”);
@@ -202,7 +203,7 @@ Init_glib2()
rb_define_const(mGLib, “MININT”, INT2FIX(G_MININT));
rb_define_const(mGLib, “MAXINT”, INT2FIX(G_MAXINT));
rb_define_const(mGLib, “MAXUINT”, INT2FIX(G_MAXUINT));
-
rb_define_const(mGLib, “MINSHORT”, INT2FIX(G_MINSHORT));
rb_define_const(mGLib, “MAXSHORT”, INT2FIX(G_MAXSHORT));
rb_define_const(mGLib, “MAXUSHORT”, INT2FIX(G_MAXUSHORT));
@@ -314,6 +315,7 @@ union GDoubleIEEE754;
Init_glib_unicode();
Init_glib_keyfile();
Init_glib_bookmark_file(); -
Init_glib_string();
/* This is called here once. /
G_DEF_SETTERS(mGLib);
diff --git a/glib/src/rbglib_string.c b/glib/src/rbglib_string.c
new file mode 100644
index 0000000…374c369
— /dev/null
+++ b/glib/src/rbglib_string.c
@@ -0,0 +1,329 @@
+/ -- c-file-style: “ruby”; indent-tabs-mode: nil -- /
+/*********************************************** -
rbglib_string.c -
-
$Author: $
-
$Date: $
-
Copyright (C) 2009 Lionel L.
+************************************************/
+#include “rbgprivate.h”
+
+/***********************************************/
+
+#define _SELF(self) ((GString)(RVAL2BOXED(self, G_TYPE_GSTRING)))
+
+static VALUE
+string_initialize(argc, argv, self)
- int argc;
- VALUE *argv;
- VALUE self;
+{ -
VALUE arg1;
- if (argc == 0) {
-
G_INITIALIZE(self, g_string_new(NULL));
- } else {
-
rb_scan_args(argc, argv, "1", &arg1);
-
if (TYPE(arg1) != T_STRING)
-
rb_raise(rb_eArgError, "argument should be a String");
-
G_INITIALIZE(self, g_string_new(STR2CSTR(arg1)));
- }
- return Qnil;
+}
+static VALUE
+string_equal(self, value)
- VALUE self;
- VALUE value;
+{ - return CBOOL2RVAL(g_string_equal(_SELF(self), _SELF(value)));
+}
+static VALUE
+string_assign(self, value)
- VALUE self;
- VALUE value;
+{ - if (TYPE(value) != T_STRING)
-
rb_raise(rb_eArgError, "argument should be a String");
- g_string_assign(_SELF(self), STR2CSTR(value));
- return self;
+}
+static VALUE
+string_truncate(self, len)
- VALUE self;
- VALUE len;
+{ - if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argment should be a Fixnum");
- g_string_truncate(_SELF(self), FIX2INT(len));
- return self;
+}
+static VALUE
+string_get_size(self)
- VALUE self;
+{ - return INT2FIX(_SELF(self)->len);
+}
+static VALUE
+string_set_size(self, size)
- VALUE self;
- VALUE size;
+{ - if (!FIXNUM_P(size))
-
rb_raise(rb_eArgError, "argment should be a Fixnum");
- g_string_set_size(_SELF(self), FIX2INT(size));
- return self;
+}
+static VALUE
+string_get_char(self, pos)
- VALUE self;
- VALUE pos;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argment should be a Fixnum");
- if (FIX2INT(pos) >= _SELF(self)->len)
-
return Qnil;
- return INT2FIX(_SELF(self)->str[FIX2INT(pos)]);
+}
+static VALUE
+string_set_char(self, pos, value)
- VALUE self;
- VALUE pos;
- VALUE value;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argment 1 should be a Fixnum");
- if (!FIXNUM_P(value) && (FIX2INT(value) >= 256))
-
rb_raise(rb_eArgError, "argment 2 should be a Fixnum < 256");
- _SELF(self)->str[FIX2INT(pos)] = (gchar) FIX2INT(value);
- return self;
+}
+static VALUE
+string_append(self, data)
- VALUE self;
- VALUE data;
+{ - if (FIXNUM_P(data) && (FIX2INT(data) < 256))
-
g_string_append_c(_SELF(self), (gchar) FIX2INT(data));
- else if (TYPE(data) == T_STRING)
-
g_string_append(_SELF(self), STR2CSTR(data));
- else
-
rb_raise(rb_eArgError, "argument should be a Fixnum < 256 or
String");
+
- return self;
+}
+static VALUE
+string_append_len(self, str, len)
- VALUE self;
- VALUE str;
- VALUE len;
+{ - if (TYPE(str) != T_STRING)
-
rb_raise(rb_eArgError, "argument 1 should be a String");
- if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argument 2 should be a Fixnum");
- g_string_append_len(_SELF(self), STR2CSTR(str), FIX2INT(len));
- return self;
+}
+static VALUE
+string_prepend(self, data)
- VALUE self;
- VALUE data;
+{ - if (FIXNUM_P(data) && (FIX2INT(data) < 256))
-
g_string_prepend_c(_SELF(self), (gchar) FIX2INT(data));
- else if (TYPE(data) == T_STRING)
-
g_string_prepend(_SELF(self), STR2CSTR(data));
- else
-
rb_raise(rb_eArgError, "argument should be a Fixnum or
String");
+
- return self;
+}
+static VALUE
+string_prepend_len(self, str, len)
- VALUE self;
- VALUE str;
- VALUE len;
+{ - if (TYPE(str) != T_STRING)
-
rb_raise(rb_eArgError, "argument 1 should be a String");
- if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argument 2 should be a Fixnum");
- g_string_prepend_len(_SELF(self), STR2CSTR(str), FIX2INT(len));
- return self;
+}
+static VALUE
+string_insert(self, pos, data)
- VALUE self;
- VALUE pos;
- VALUE data;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argument 1 should be a Fixnum");
- if (FIXNUM_P(data) && (FIX2INT(data) < 256))
-
g_string_insert_c(_SELF(self), FIX2INT(pos), (gchar)
FIX2INT(data));
- else if (TYPE(data) == T_STRING)
-
g_string_insert(_SELF(self), FIX2INT(pos), STR2CSTR(data));
- else
-
rb_raise(rb_eArgError, "argument 2 should be a Fixnum or
String");
+
- return self;
+}
+static VALUE
+string_insert_len(self, pos, str, len)
- VALUE self;
- VALUE pos;
- VALUE str;
- VALUE len;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argument 1 should be a Fixnum");
- if (TYPE(str) != T_STRING)
-
rb_raise(rb_eArgError, "argument 2 should be a String");
- if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argument 3 should be a Fixnum");
- g_string_insert_len(_SELF(self), FIX2INT(pos), STR2CSTR(str),
FIX2INT(len)); - return self;
+}
+static VALUE
+string_overwrite(self, pos, data)
- VALUE self;
- VALUE pos;
- VALUE data;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argument 1 should be a Fixnum");
- if (FIXNUM_P(data) && (FIX2INT(data) < 256))
-
_SELF(self)->str[FIX2INT(pos)] = (gchar) FIX2INT(data);
- else if (TYPE(data) == T_STRING)
-
g_string_overwrite(_SELF(self), FIX2INT(pos), STR2CSTR(data));
- else
-
rb_raise(rb_eArgError, "argument 2 should be a Fixnum or
String");
+
- return self;
+}
+static VALUE
+string_overwrite_len(self, pos, str, len)
- VALUE self;
- VALUE pos;
- VALUE str;
- VALUE len;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argument 1 should be a Fixnum");
- if (TYPE(str) != T_STRING)
-
rb_raise(rb_eArgError, "argument 2 should be a String");
- if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argument 3 should be a Fixnum");
- g_string_overwrite_len(_SELF(self), FIX2INT(pos), STR2CSTR(str),
FIX2INT(len)); - return self;
+}
+static VALUE
+string_erase(self, pos, len)
- VALUE self;
- VALUE pos;
- VALUE len;
+{ - if (!FIXNUM_P(pos))
-
rb_raise(rb_eArgError, "argument 1 should be a Fixnum");
- if (!FIXNUM_P(len))
-
rb_raise(rb_eArgError, "argument 2 should be a String");
- g_string_erase(_SELF(self), FIX2INT(pos), FIX2INT(len));
- return self;
+}
+static VALUE
+string_ascii_down(self)
- VALUE self;
+{ - g_string_ascii_down(_SELF(self));
- return self;
+}
+static VALUE
+string_ascii_up(self)
- VALUE self;
+{ - g_string_ascii_up(_SELF(self));
- return self;
+}
+void
+Init_glib_string()
+{
- VALUE str = G_DEF_CLASS(G_TYPE_GSTRING, “String”, mGLib);
- rb_define_method(str, “initialize”, string_initialize, -1);
- rb_define_method(str, “==”, string_equal, 1);
- rb_define_method(str, “assign”, string_assign, -1);
- rb_define_method(str, “truncate”, string_truncate, -1);
- rb_define_method(str, “size”, string_get_size, 0);
- rb_define_method(str, “size=”, string_set_size, 1);
- rb_define_method(str, “[]”, string_get_char, 1);
- rb_define_method(str, “[]=”, string_set_char, 2);
- rb_define_method(str, “append”, string_append, 1);
- rb_define_method(str, “append_len”, string_append_len, 2);
- rb_define_method(str, “insert”, string_insert, 2);
- rb_define_method(str, “insert_len”, string_insert_len, 3);
- rb_define_method(str, “prepend”, string_prepend, 1);
- rb_define_method(str, “prepend_len”, string_prepend_len, 1);
- rb_define_method(str, “overwrite”, string_overwrite, 2);
- rb_define_method(str, “overwrite_len”, string_overwrite_len, 3);
- rb_define_method(str, “erase”, string_erase, 2);
- rb_define_method(str, “ascii_down”, string_ascii_down, 0);
- rb_define_method(str, “ascii_up”, string_ascii_up, 0);
+}
–
1.6.3.1
–
Lionel L. [email protected]