forked from msgpack/msgpack-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosslang.rb
88 lines (72 loc) · 1.31 KB
/
crosslang.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# MessagePack cross-language test tool
#
# $ gem install msgpack
# or
# $ port install rb_msgpack # MacPorts
#
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 <<EOF
Usage: #{$0} [in-file] [out-file]
This tool is for testing of MessagePack implementation.
This does following behavior:
1. Reads objects serialized by MessagePack from <in-file> (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 <out-file> (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