module Bytemapper
Bytemapper
- Model arbitrary bytestrings as Ruby objects. Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects.
Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects.
Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects.
Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects. Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects. Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects. Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Bytemapper
- Model arbitrary bytestrings as Ruby objects.
Copyright (C) 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Public Class Methods
# File lib/bytemapper.rb, line 98 def get(obj) registry.get(obj) end
# File lib/bytemapper.rb, line 102 def get2(obj) obj.each_pair { |k,v| obj[k] = get(v) if registry.registered_name?(v) } registry.get(obj) end
# File lib/bytemapper.rb, line 71 def is_a_name?(obj) obj.is_a?(String) || obj.is_a?(Symbol) end
# File lib/bytemapper.rb, line 67 def is_a_shape?(obj) obj.is_a?(Hash) end
# File lib/bytemapper.rb, line 59 def is_a_type?(obj) obj.is_a?(Type) || obj.is_a?(Array) && obj.size == 2 && obj.first.is_a?(Integer) && obj.last.is_a?(String) end
# File lib/bytemapper.rb, line 75 def map(bytes, shape, name = nil) bytes = StringIO.from(bytes) if shape.is_a?(Bytemapper::Table) shape.populate(bytes) elsif shape.is_a?(Array) chunks = [] shape.each { |s| chunks << Chunk.new(bytes.read(s.size), s, name) } chunks else shape = wrap(shape, name) Chunk.new(bytes, shape, name) end end
# File lib/bytemapper.rb, line 111 def names(filter_key = nil) registry.names.keys end
# File lib/bytemapper.rb, line 115 def print registry.print end
# File lib/bytemapper.rb, line 107 def put(obj, name) registry.put(obj, name) end
# File lib/bytemapper.rb, line 32 def register(obj, name, fqname = []) return if obj.nil? name = name.downcase.to_sym unless name.nil? fqname << name unless name.nil? if is_a_type?(obj) name = fqname.size > 1 ? fqname.join('.') : fqname.first obj = Type.new(obj) put(obj, name) elsif is_a_name?(obj) register(get(obj), nil, fqname) elsif is_a_shape?(obj) if registered?(obj) obj = get(obj) put(obj, name) else shape = Shape.new obj.each do |k,v| shape[k] = register(v, k, [].concat(fqname)) end put(shape, name) end else put(obj, name) end end
# File lib/bytemapper.rb, line 94 def registered?(obj) registry.registered?(obj) end
# File lib/bytemapper.rb, line 119 def registry @@registry end
# File lib/bytemapper.rb, line 90 def repeat(obj, times = nil) Table.new(obj, times) end
# File lib/bytemapper.rb, line 123 def reset(with_basic_types = true) @@registry = Registry.new(with_basic_types) end
# File lib/bytemapper.rb, line 127 def reverse_lookup(prefix, value = nil) @@rl_cache ||= {} prefix = "#{prefix.to_s}_" lookup = @@rl_cache[prefix] if lookup.nil? labels = names.filter { |n| n.start_with?(prefix) } values = labels.map { |l| get(l) } lookup = Hash[values.zip(labels)] @@rl_cache[prefix] = lookup end value.nil? ? lookup : lookup[value] end