-
-
Save anonymous/89220c46effa04a01b95 to your computer and use it in GitHub Desktop.
Hugo Figueiredo Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# [Author] HugoLnx/HugoLinux | |
# [Version] 0.0.1.0 | |
# | |
require 'optparse' | |
require 'jcode' | |
class String | |
def is_number? | |
s = self | |
s = self[0...s.size - 1] if s[(s.size - 1)..s.size] == "\n" | |
return false if s == '' | |
s.each_char{|char| | |
return false if !('0'..'9').any?{|num| num == char} | |
} | |
return true | |
end | |
end | |
class SubTitle | |
def initialize(file) | |
@composition = [] | |
lines = file.readlines | |
i_line = 0 | |
i_block = 0 | |
while lines[i_line].to_i == 0 | |
i_line += 1 | |
end | |
while i_line < lines.size | |
block = {} | |
block[:id] = lines[i_line] | |
i_line += 1 | |
block[:time_range] = TimeRange.new(lines[i_line]) | |
i_line += 1 | |
texts = [] | |
while i_line < lines.size and !lines[i_line].is_number? | |
texts << lines[i_line] | |
i_line += 1 | |
end | |
block[:texts] = texts | |
@composition[i_block] = block | |
i_block += 1 | |
end | |
end | |
def to_file(path) | |
file = File.new(path,'w') | |
@composition.each{|block| | |
file << (block[:id]) | |
file << block[:time_range].to_s + "\n" | |
block[:texts].each{|line| file << line} | |
} | |
end | |
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@composition.each{|block| | |
block[:time_range].add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
} | |
end | |
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
warning = false | |
@composition.each{|block| | |
block[:time_range].sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
warning = true if block[:time_range].init_time.hour < 0 or | |
block[:time_range].final_time.hour < 0 | |
} | |
puts 'WARNING: Some times are negative' if warning | |
end | |
end | |
class Time | |
def initialize(time_s) | |
time_s[8] = '.' | |
@hour = time_s.slice(0..1).to_i | |
@min = time_s.slice(3..4).to_i | |
@sec = time_s.slice(6..7).to_i | |
@millisec = time_s.slice(9..11).to_i | |
end | |
def to_s | |
return "%.2d:%.2d:%.2d,%.3d" % [@hour,@min,@sec,@millisec] | |
end | |
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@hour += qnt_hour | |
@min += qnt_min | |
@sec += qnt_sec | |
@millisec += qnt_millisec | |
while @millisec >= 1000 | |
@millisec -= 1000 | |
@sec += 1 | |
end | |
while @sec >= 60 | |
@sec -= 60 | |
@min += 1 | |
end | |
while @min >= 60 | |
@min -= 60 | |
@hour += 1 | |
end | |
end | |
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@hour -= qnt_hour | |
@min -= qnt_min | |
@sec -= qnt_sec | |
@millisec -= qnt_millisec | |
while @millisec < 0 | |
@millisec += 1000 | |
@sec -= 1 | |
end | |
while @sec < 0 | |
@sec += 60 | |
@min -= 1 | |
end | |
while @min < 0 | |
@min += 60 | |
@hour -= 1 | |
end | |
end | |
end | |
class TimeRange | |
attr_accessor :init_time | |
attr_accessor :final_time | |
def initialize(time_range_s) | |
@init_time = Time.new(time_range_s.slice(0..11)) | |
@final_time = Time.new(time_range_s.slice(17..28)) | |
end | |
def to_s | |
return @init_time.to_s + ' --> ' + @final_time.to_s | |
end | |
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@init_time.add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@final_time.add(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
end | |
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@init_time.sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
@final_time.sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec) | |
end | |
end | |
class OptParser < OptionParser | |
attr :opts | |
alias ori_initialize initialize | |
def initialize | |
ori_initialize | |
@opts = {} | |
declare_options | |
end | |
def declare_options | |
def_option('--operation'){ | |
@opts[:oper] = true | |
} | |
def_option('--time'){ | |
@opts[:time] = true | |
} | |
parse! | |
end | |
end | |
class TimeArgument | |
attr :hour | |
attr :min | |
attr :sec | |
attr :millisec | |
def initialize(arg) | |
hour_s = '' | |
min_s = '' | |
sec_s = '' | |
millisec_s = '' | |
arg.reverse! | |
while arg[0].chr != ',' | |
millisec_s += arg[0].chr | |
arg[0] = '' | |
return if arg[0] == nil | |
end | |
arg[0] = '' | |
return if arg[0] == nil | |
while arg[0].chr != ',' | |
sec_s += arg[0].chr | |
arg[0] = '' | |
return if arg[0] == nil | |
end | |
arg[0] = '' | |
return if arg[0] == nil | |
while arg[0].chr != ',' | |
min_s += arg[0].chr | |
arg[0] = '' | |
return if arg[0] == nil | |
end | |
arg[0] = '' | |
return if arg[0] == nil | |
while arg[0].chr != ',' | |
hour_s += arg[0].chr | |
arg[0] = '' | |
return if arg[0] == nil | |
end | |
arg[0] = '' | |
ensure | |
hour_s.reverse! | |
min_s.reverse! | |
sec_s.reverse! | |
millisec_s.reverse! | |
@hour = hour_s.to_i | |
@min = min_s.to_i | |
@sec = sec_s.to_i | |
@millisec = millisec_s.to_i | |
end | |
end | |
begin | |
optparser = OptParser.new rescue( | |
puts 'Incorrect Option(s)' | |
sleep 1 | |
exit | |
) | |
if optparser.opts[:oper] and optparser.opts[:time] | |
puts 'Loading subtitle' | |
input_file = File.open(ARGV[2],'r') | |
subtitle = SubTitle.new(input_file) | |
puts 'Changing the times of subtitle' | |
time_arg = TimeArgument.new(ARGV[1].dup) | |
eval("subtitle.#{ARGV[0]}(#{time_arg.hour},#{time_arg.min},#{time_arg.sec},#{time_arg.millisec})") | |
puts "Creating #{ARGV[3]}" | |
subtitle.to_file(ARGV[3]) | |
puts "'#{ARGV[3]}' has been created" | |
end rescue( | |
puts 'Incorrect Argument(s)' | |
sleep 1 | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment