Александър обнови решението на 10.11.2014 15:41 (преди около 10 години)
+module RBFS
+ class StringToDir
+ @files
+ @file
+ @dirs
+ @dir
+ @dir_str
+ def initialize str
+ @files = Hash.new
+ @file = File.new
+ @dir = Directory.new
+ @dirs = Hash.new
+ @dir_str = str
+ string_to_files
+ string_to_dirs
+ end
+ def string_to_dirs
+ num,@dir_str = @dir_str.split(":",2)
+ while num.to_i > @dirs.size
+ name,size,rest = @dir.split(":",3)
+ one_dir_str = rest[0..size.to_i]
+ @dir_str = rest[size.to_i + 1..rest.length]
+ @dirs.join({name => @dir.sanitize})
+ end
+ end
+ def string_to_files
+ num,@dir_str = @dir_str.split(":",2)
+ while num.to_i > files.size
+ name,size,rest = @dir_str.split(":",3)
+ file_str = rest[0..size.to_i]
+ @dir_str = rest[size_to_i+1..rest.length]
+ @files.join({name => @file.sanitize(file_str)})
+ end
+ end
+ def get_hash
+ @dirs.join(@files)
+ end
+ end
+ class DirToString
+ def files_to_string(files)
+ files_text = String.new
+ files.each do |key,value|
+ value_str = value.serialize
+ char_num = value_str.length
+ files_text << key << ":" << char_num.to_s << ":" << value_str
+ end
+ files_text = nil if files.nil?
+ files_text
+ end
+ def dir_to_string(dir)
+ dir_text = ""
+ dir.each do |key,value|
+ value_str = value.serialize
+ char_num = value_str.length.to_s
+ dir_text << dir_text << key << ":" << char_num << ":" + value_str << ":"
+ end
+ dir_text = nil if dir.nil?
+ dir_text
+ end
+ end
+ class File
+ attr_accessor :data
+ attr_reader:data_type
+ def initialize object = nil
+ @data = object
+ change_data_type
+ end
+ def data_type
+ @data_type
+ end
+ def change_data_type
+ if(@data.is_a?(TrueClass) || @data.is_a?(FalseClass)) then @data_type = :boolean
+ elsif (@data.kind_of?(Integer) || @data.kind_of?(Float)) then @data_type = :number
+ elsif (@data.is_a? Symbol) then @data_type = :symbol
+ elsif (@data.is_a? String) then @data_type = :string
+ elsif (@data.nil?) then @data_type = :nil
+ end
+ end
+ def data=(object = nil)
+ @data = object if !object.nil?
+ change_data_type
+ end
+ def data
+ @data
+ end
+ def serialize
+ serialized_string = data_type.to_s + ":" + data.to_s
+ end
+ def parse string_data
+ array = string_data.split(":")
+ type,@data = array[0],array[1]
+ find_object_type(type,@data)
+ end
+ def find_object_type (type, string_obj)
+ if(type == "nil") then @data = nil
+ elsif (type == "boolean") then @data = (string_obj == "true")
+ elsif (type == "number" and string_obj.include? ".") then @data = string_obj.to_f
+ elsif (type == "number") then @data = string_obj.to_i
+ elsif (type == "symbol") then @data = string_obj.to_sym
+ end
+ change_data_type
+ end
+ end
+ class Directory
+ @hash
+ def initialize
+ @hash = Hash.new
+ end
+ def add_file(name,file)
+ if(name.include? ":")
+ nil
+ else
+ @hash.store(name,file)
+ end
+ end
+ def add_directory(name,directory=nil)
+ directory = Directory.new if directory.nil?
+ @hash.store(name,directory)
+ end
+ def [](name)
+ @hash[name]
+ end
+ def serialize
+ file_num = files.length.to_s
+ dir_num = directories.length.to_s
+ serializer=DirToString.new
+ files_str = serializer.files_to_string(files)
+ dir_str = serializer.dir_to_string(directories).chop
+ file_num + ":" + files_str + dir_num + ":" + dir_str
+ end
+ def parse string_data
+ parser = StringToDir.new string_data
+ @hash = parser.get_hash
+ end
+ def files
+ @hash.select {|key,item| item.instance_of? File}
+ end
+ def directories
+ @hash.select{|key,item| item.instance_of? Directory}
+ end
+ end
+end