Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created May 4, 2012 14:15
Show Gist options
  • Save lomereiter/2595037 to your computer and use it in GitHub Desktop.
Save lomereiter/2595037 to your computer and use it in GitHub Desktop.
D & ruby: std.stdio
import std.stdio;
import core.runtime;
extern(C) void lib_init() {
Runtime.initialize();
}
import std.conv;
import std.string;
import core.memory;
import std.c.stdlib;
extern(C) void* file_open(const char* filename) {
File* f = cast(File*)malloc(File.sizeof);
emplace(f, to!string(filename));
GC.addRange(f, File.sizeof);
return f;
}
extern(C) void file_close(File* f) {
f.close();
GC.removeRange(f);
free(cast(void*)f);
}
extern(C) immutable(char)* file_readln(File* f) {
return toStringz(f.readln());
}
extern(C) bool file_eof(File* f) {
return f.eof();
}
void main() {}
require 'ffi'
module ReadFileLib
extend FFI::Library
ffi_lib './readfile.so'
attach_function :lib_init, [], :void
attach_function :file_open, [:string], :pointer
attach_function :file_close, [:pointer], :void
attach_function :file_eof, [:pointer], :bool
attach_function :file_readln, [:pointer], :string
end
ReadFileLib.lib_init
ptr = ReadFileLib.file_open 'readfile.d'
while not ReadFileLib.file_eof(ptr) do
puts ReadFileLib.file_readln(ptr)
end
ReadFileLib.file_close ptr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment