Skip to content
Browse files

basic implementation of writev

  • Loading branch information...
0 parents commit 7a5392756ce0e981e4f042c82bdf8fc092d16984 @tenderlove committed Aug 19, 2011
Showing with 155 additions and 0 deletions.
  1. +8 −0 .autotest
  2. +6 −0 CHANGELOG.rdoc
  3. +7 −0 Manifest.txt
  4. +48 −0 README.rdoc
  5. +33 −0 Rakefile
  6. +3 −0 ext/writev/extconf.rb
  7. +30 −0 ext/writev/writev.c
  8. +5 −0 lib/writev.rb
  9. +15 −0 test/test_writev.rb
8 .autotest
@@ -0,0 +1,8 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+Autotest.add_hook :initialize do |at|
+ at.testlib = 'minitest/autorun'
+ at.find_directories = ARGV unless ARGV.empty?
+end
6 CHANGELOG.rdoc
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2011-08-19
+
+* 1 major enhancement
+
+ * Birthday!
+
7 Manifest.txt
@@ -0,0 +1,7 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+bin/writev
+lib/writev.rb
+test/test_writev.rb
48 README.rdoc
@@ -0,0 +1,48 @@
+= writev
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2011 Aaron Patterson
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 Rakefile
@@ -0,0 +1,33 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+gem 'rake-compiler', '>= 0.4.1'
+require "rake/extensiontask"
+
+Hoe.plugins.delete :rubyforge
+Hoe.plugin :minitest
+Hoe.plugin :gemspec # `gem install hoe-gemspec`
+Hoe.plugin :git # `gem install hoe-git`
+Hoe.plugin :debugging # `gem install hoe-debugging`
+
+Hoe.spec 'writev' do
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
+ self.readme_file = 'README.rdoc'
+ self.history_file = 'CHANGELOG.rdoc'
+ self.extra_rdoc_files = FileList['*.rdoc']
+
+ extra_dev_deps << ['rake-compiler', '>= 0.4.1']
+
+ self.spec_extras = {
+ :extensions => ["ext/writev/extconf.rb"],
+ :required_ruby_version => '>= 1.9.2'
+ }
+
+ Rake::ExtensionTask.new "writev", spec do |ext|
+ ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
+ end
+end
+
+# vim: syntax=ruby
3 ext/writev/extconf.rb
@@ -0,0 +1,3 @@
+require 'mkmf'
+
+create_makefile 'writev'
30 ext/writev/writev.c
@@ -0,0 +1,30 @@
+#include <ruby.h>
+#include <ruby/io.h>
+#include <sys/uio.h>
+
+static VALUE rb_writev(VALUE io, VALUE list)
+{
+ rb_io_t * fptr;
+ struct iovec * iov;
+ long i;
+
+ GetOpenFile(io, fptr);
+ iov = xcalloc(RARRAY_LEN(list), sizeof(struct iovec));
+
+ for(i = 0; i < RARRAY_LEN(list); i++) {
+ VALUE string = rb_ary_entry(list, i);
+ iov[i].iov_base = StringValuePtr(string);
+ iov[i].iov_len = RSTRING_LEN(string);
+ }
+
+ writev(fptr->fd, iov, RARRAY_LEN(list));
+
+ return Qnil;
+}
+
+void Init_writev()
+{
+ rb_define_method(rb_cIO, "writev", rb_writev, 1);
+}
+
+/* vim: set noet sws=4 sw=4: */
5 lib/writev.rb
@@ -0,0 +1,5 @@
+require 'writev.so'
+
+module WriteV
+ VERSION = '1.0.0'
+end
15 test/test_writev.rb
@@ -0,0 +1,15 @@
+require 'minitest/autorun'
+require 'writev'
+require 'tempfile'
+
+class TestWritev < MiniTest::Unit::TestCase
+ def test_writev
+ file = Tempfile.new('foo')
+
+ file.writev %w{ hello world }
+ assert_equal 'helloworld', File.read(file.path)
+ file.unlink
+ end
+end
+
+# /* vim: set et sws=2 sw=2: */

0 comments on commit 7a53927

Please sign in to comment.
Something went wrong with that request. Please try again.