From a0071c2f9f1760b6eb1a40c1b6179f02c8ce7cb6 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Mon, 31 May 2010 17:27:51 +0900 Subject: [PATCH] add crosslang.rb --- crosslang.cc | 2 +- crosslang.rb | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 crosslang.rb diff --git a/crosslang.cc b/crosslang.cc index 3b22b74c..9ec7c0f6 100644 --- a/crosslang.cc +++ b/crosslang.cc @@ -78,7 +78,7 @@ static void usage(const char* prog) " 3. Writes the re-serialized objects into (default: stdout)\n" "\n" , prog); - exit(0); + exit(1); } int main(int argc, char* argv[]) diff --git a/crosslang.rb b/crosslang.rb new file mode 100644 index 00000000..af36dd11 --- /dev/null +++ b/crosslang.rb @@ -0,0 +1,81 @@ +begin +require 'rubygems' +rescue LoadError +end +require 'msgpack' + +def run(inio, outio) + pac = MessagePack::Unpacker.new(inio) + + begin + pac.each {|obj| + outio.write MessagePack.pack(obj) + outio.flush + } + rescue EOFError + return 0 + rescue + $stderr.puts $! + return 1 + end + + return 0 +end + +def usage + puts < (default: stdin) + 2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid) + 3. Writes the re-serialized objects into (default: stdout) + +EOF + exit 1 +end + +inio = $stdin +outio = $stdout + +if ARGV.length > 2 + usage +end + +ARGV.each {|str| + if str.size > 1 && str[0] == ?- + usage + end +} + +if fname = ARGV[0] + unless fname == "-" + begin + inio = File.open(fname) + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +if fname = ARGV[1] + unless fname == "-" + begin + outio = File.open(fname, "w") + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +code = run(inio, outio) + +inio.close +outio.close + +exit code +