#!/usr/bin/env ruby require "json" def read_block(file) result = "" 4.times do break if file.eof? c = file.readchar result += sprintf("%c", c) end end_index = result.index(0) 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