2010-02-06 20:37:25 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
def read_block(file)
|
|
|
|
result = ""
|
|
|
|
|
|
|
|
4.times do
|
|
|
|
break if file.eof?
|
2017-03-31 20:03:18 +00:00
|
|
|
c = file.readbyte
|
2010-02-06 20:37:25 +00:00
|
|
|
result += sprintf("%c", c)
|
|
|
|
end
|
|
|
|
|
2017-03-31 20:03:18 +00:00
|
|
|
end_index = result.index("\0")
|
2010-02-06 20:37:25 +00:00
|
|
|
if end_index
|
|
|
|
result = result[0, end_index]
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_block?(block)
|
|
|
|
for i in 0...block.length
|
|
|
|
c = block[i, 1]
|
|
|
|
|
|
|
|
if c !~ /[a-zA-Z0-9_= :\\\'\"\-\,\.\(\)\[\]\%\!\n\?\<\>\%\#\@\/\&]/
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
File.open(ARGV[0]) do |file|
|
|
|
|
current_str = ""
|
|
|
|
|
|
|
|
until file.eof?
|
|
|
|
block = read_block(file)
|
|
|
|
|
|
|
|
if valid_block?(block)
|
|
|
|
current_str += block
|
|
|
|
|
|
|
|
# End of string?
|
|
|
|
|
|
|
|
if block.length < 4
|
|
|
|
if current_str.length >= 4
|
|
|
|
puts current_str.to_json
|
|
|
|
end
|
|
|
|
current_str = ""
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Invalid string; start again
|
|
|
|
|
|
|
|
# if current_str.length >= 4
|
|
|
|
# puts "INVALID: #{current_str + block}"
|
|
|
|
# end
|
|
|
|
|
|
|
|
current_str = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|