bin/xtmjoin: Moved core code to library.

This commit is contained in:
Glenn Y. Rolland 2010-11-15 21:47:31 +01:00
parent d0cbc1a376
commit 9a4c6b0e94

View file

@ -5,8 +5,9 @@ require 'optparse'
require 'rubygems' require 'rubygems'
require 'bindata' require 'bindata'
require 'xtmfile/xtmheader' require 'xtmfile/header'
require 'xtmfile/joiner'
require 'xtmfile/splitter'
class XtmJoin class XtmJoin
class XtmJoinArgumentError < ArgumentError ; end class XtmJoinArgumentError < ArgumentError ; end
@ -27,18 +28,24 @@ class XtmJoin
opts.banner = "Usage: #{File.basename $0} [options]\n" opts.banner = "Usage: #{File.basename $0} [options]\n"
opts.separator "" opts.separator ""
opts.separator "Mandatory options" opts.separator "Options"
opts.on("-i", "--input FILE", "Input XTM file") do |r| opts.on("-i", "--input FILE", "Input XTM file (mandatory)") do |r|
@input_filename = r @input_filename = r
end end
opts.separator "" opts.on("-o", "--output FILE", "Output file (optional)") do |o|
opts.separator "General options:" @output_filename = o
end
opts.on("-m", "--md5", "Verify MD5 sums") do |m|
@verify_md5 = m
end
opts.on("-h", "--help", "Show this help") do |h| opts.on("-h", "--help", "Show this help") do |h|
@help = h @help = h
end end
opts.on("-v", "--verbose", "Show warnings too") do |v| opts.on("-v", "--verbose", "Show warnings too") do |v|
@verbose = v @verbose = v
end end
@ -56,73 +63,25 @@ class XtmJoin
@input_filename = File.expand_path @input_filename @input_filename = File.expand_path @input_filename
end end
def run def run
validate! validate!
output_file = nil
# initial file
in_xtm = File.open @input_filename, "rb"
header = XtmHeader::read in_xtm
output_file = header.filename_str
puts "Writing data to %s" % output_file
# FIXME: prevent overwriting # FIXME: prevent overwriting
out_xtm = File.open output_file, "wb"
xtmj = XtmFile::Joiner.new @input_filename, @output_filename
puts "Writing data to %s" % xtmj.output_filename
cur_xtm = @input_filename xtmj.start do |event, info|
is_first = true case event
cur_size = header.filesize when XtmFile::Joiner::EVENT_OPENFILE then
print "\x1b[s" puts "Opening %s" % info
while cur_size > 0 do when XtmFile::Joiner::EVENT_PROGRESS then
unless is_first then print "\x1b[uProgress : %s %" % info
cur_xtm = _nextfile cur_xtm
puts "Opening %s" % cur_xtm
in_xtm = File.open cur_xtm, "rb"
end
while cur_size > 0 and (not in_xtm.eof?) do
pcent = ((header.filesize - cur_size) * 100 / header.filesize)
STDOUT.print "\x1b[uProgress : %s %" % pcent
STDOUT.flush STDOUT.flush
read_size = if cur_size > BUFFER_MAX_SIZE then BUFFER_MAX_SIZE
else cur_size
end
buffer = in_xtm.read read_size
cur_size = cur_size - buffer.length
out_xtm.write buffer.slice(0, buffer.length)
end end
is_first = false
in_xtm.close
end end
out_xtm.close
STDOUT.puts ""
# remaining files STDOUT.puts ""
end
def _nextfile curfile
result = nil
cur_idx = 0
cur_len = 0
case curfile
when /\.([0-9]+)\.xtm$/ then
cur_idx = $1.to_i
cur_len = $1.length
next_idx = cur_idx + 1
result = curfile.gsub(/\.([0-9]+)\.xtm$/, ".%0#{cur_len}d.xtm" % next_idx)
when /\.xtm\.([0-9]+)$/ then
cur_idx = $1.to_i
cur_len = $1.length
next_idx = cur_idx + 1
result = curfile.gsub(/\.xtm\.([0-9]+)$/, ".xtm.%0#{cur_len}d" % next_idx)
else
raise "Unable to detect a naming patterng for file sequence!"
end
return result
end end
def self.main args def self.main args