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 'bindata'
require 'xtmfile/xtmheader'
require 'xtmfile/header'
require 'xtmfile/joiner'
require 'xtmfile/splitter'
class XtmJoin
class XtmJoinArgumentError < ArgumentError ; end
@ -27,18 +28,24 @@ class XtmJoin
opts.banner = "Usage: #{File.basename $0} [options]\n"
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
end
opts.separator ""
opts.separator "General options:"
opts.on("-o", "--output FILE", "Output file (optional)") do |o|
@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|
@help = h
end
opts.on("-v", "--verbose", "Show warnings too") do |v|
@verbose = v
end
@ -56,73 +63,25 @@ class XtmJoin
@input_filename = File.expand_path @input_filename
end
def run
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
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
is_first = true
cur_size = header.filesize
print "\x1b[s"
while cur_size > 0 do
unless is_first then
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
xtmj.start do |event, info|
case event
when XtmFile::Joiner::EVENT_OPENFILE then
puts "Opening %s" % info
when XtmFile::Joiner::EVENT_PROGRESS then
print "\x1b[uProgress : %s %" % info
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
is_first = false
in_xtm.close
end
out_xtm.close
STDOUT.puts ""
# remaining files
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
STDOUT.puts ""
end
def self.main args