Permalink
Browse files

partial implementation of Stree::String

  • Loading branch information...
1 parent b75b18b commit 9c23ee83f63ad964f3015d47a167a1a5e451c9fa @tenderlove committed Dec 16, 2009
Showing with 130 additions and 10 deletions.
  1. +4 −0 Manifest.txt
  2. +49 −0 ext/stree/extconf.rb
  3. +4 −7 ext/stree/stree.c
  4. +11 −0 ext/stree/stree.h
  5. +40 −0 ext/stree/stree_string.c
  6. +8 −0 ext/stree/stree_string.h
  7. +1 −1 lib/stree.rb
  8. +2 −2 test/test_stree.rb
  9. +11 −0 test/test_string.rb
View
@@ -5,5 +5,9 @@ README.rdoc
Rakefile
ext/stree/extconf.rb
ext/stree/stree.c
+ext/stree/stree.h
+ext/stree/stree_string.c
+ext/stree/stree_string.h
lib/stree.rb
test/test_stree.rb
+test/test_string.rb
View
@@ -1,2 +1,51 @@
+ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
+
require 'mkmf'
+
+LIBDIR = Config::CONFIG['libdir']
+INCLUDEDIR = Config::CONFIG['includedir']
+
+HEADER_DIRS = [
+ # First search /opt/local for macports
+ '/opt/local/include',
+
+ # Then search /usr/local for people that installed from source
+ '/usr/local/include',
+
+ # Check the ruby install locations
+ INCLUDEDIR,
+
+ # Finally fall back to /usr
+ '/usr/include',
+]
+
+LIB_DIRS = [
+ # First search /opt/local for macports
+ '/opt/local/lib',
+
+ # Then search /usr/local for people that installed from source
+ '/usr/local/lib',
+
+ # Check the ruby install locations
+ LIBDIR,
+
+ # Finally fall back to /usr
+ '/usr/lib',
+]
+
+stree_dirs = dir_config('stree', '/opt/local/include', '/opt/local/lib')
+
+unless ["", ""] == stree_dirs
+ HEADER_DIRS.unshift stree_dirs.first
+ LIB_DIRS.unshift stree_dirs[1]
+end
+
+unless find_header('stree/lst_string.h', *HEADER_DIRS)
+ abort "libstree is missing. please install libstree"
+end
+
+unless find_library('stree', 'lst_stree_free', *LIB_DIRS)
+ abort "libstree is missing. please install libstree"
+end
+
create_makefile('stree/stree')
View
@@ -1,12 +1,9 @@
-#include <ruby.h>
+#include <stree.h>
-static VALUE hello_world(VALUE klass)
-{
- return rb_str_new2("hello world");
-}
+VALUE cStree;
void Init_stree()
{
- VALUE mStree = rb_define_module("Stree");
- rb_define_singleton_method(mStree, "hello_world", hello_world, 0);
+ cStree = rb_define_class("Stree", rb_cObject);
+ Init_stree_string();
}
View
@@ -0,0 +1,11 @@
+#ifndef RUBY_STREE
+#define RUBY_STREE
+
+#include <ruby.h>
+#include <stree_string.h>
+
+#include <stree/lst_string.h>
+
+extern VALUE cStree;
+
+#endif
View
@@ -0,0 +1,40 @@
+#include <stree_string.h>
+
+VALUE cStreeString;
+
+static void dealloc(LST_String * ptr)
+{
+}
+
+static VALUE allocate(VALUE klass)
+{
+ LST_String * ptr;
+ return Data_Make_Struct(klass, LST_String, NULL, dealloc, ptr);
+}
+
+static VALUE initialize(VALUE self, VALUE string)
+{
+ LST_String * ptr;
+
+ Data_Get_Struct(self, LST_String, ptr);
+ lst_string_init(ptr, StringValuePtr(string), 1, RSTRING_LEN(string));
+
+ return self;
+}
+
+static VALUE bytesize(VALUE self)
+{
+ LST_String * ptr;
+
+ Data_Get_Struct(self, LST_String, ptr);
+ return INT2NUM(lst_string_get_length(ptr));
+}
+
+void Init_stree_string()
+{
+ cStreeString = rb_define_class_under(cStree, "String", rb_cObject);
+ rb_define_alloc_func(cStreeString, allocate);
+
+ rb_define_method(cStreeString, "initialize", initialize, 1);
+ rb_define_method(cStreeString, "bytesize", bytesize, 0);
+}
View
@@ -0,0 +1,8 @@
+#ifndef RUBY_STREE_STRING
+#define RUBY_STREE_STRING
+
+#include <stree.h>
+
+void Init_stree_string();
+
+#endif
View
@@ -1,5 +1,5 @@
require 'stree/stree'
-module Stree
+class Stree
VERSION = '1.0.0'
end
View
@@ -2,7 +2,7 @@
require "stree"
class TestStree < Test::Unit::TestCase
- def test_hello_world
- assert_equal 'hello world', Stree.hello_world
+ def test_new
+ assert Stree.new
end
end
View
@@ -0,0 +1,11 @@
+require "test/unit"
+require "stree"
+
+class Stree
+ class TestString < Test::Unit::TestCase
+ def test_bytesize
+ str = "foo"
+ assert_equal str.bytesize, Stree::String.new(str).bytesize
+ end
+ end
+end

0 comments on commit 9c23ee8

Please sign in to comment.