Class: HMap::HMapFileReader
- Inherits:
-
Object
- Object
- HMap::HMapFileReader
- Defined in:
- lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb
Instance Attribute Summary collapse
-
#bucktes ⇒ Hash<HMap::HMapBucket => HMap::HMapBucketStr>
readonly
An array of the file’s bucktes.
-
#filename ⇒ String?
readonly
The filename loaded from, or nil if loaded from a binary string.
- #header ⇒ HMap::HMapHeader readonly
-
#swapped ⇒ Object
readonly
True/false the swapped of the mapfile.
Instance Method Summary collapse
- #description ⇒ Object
-
#initialize(path) ⇒ HMapFileReader
constructor
A new instance of HMapFileReader.
-
#populate_and_check_magic ⇒ Integer
private
Read just the file’s magic number and check its validity.
-
#populate_buckets ⇒ Array<HMap::HMapBucket>
private
All buckets in the file.
-
#populate_fields ⇒ void
Populate the instance’s fields with the raw HMap data.
-
#populate_hmap_header ⇒ HMap::HMapHeader
private
The file’s HMapheader structure.
Constructor Details
#initialize(path) ⇒ HMapFileReader
Returns a new instance of HMapFileReader.
20 21 22 23 24 25 26 27 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 20 def initialize(path) raise ArgumentError, "#{path}: no such file" unless File.file?(path) @filename = path @raw_data = File.open(@filename, 'rb', &:read) populate_fields puts description end |
Instance Attribute Details
#bucktes ⇒ Hash<HMap::HMapBucket => HMap::HMapBucketStr> (readonly)
bucktes are provided in order of ascending offset.
Returns an array of the file’s bucktes.
18 19 20 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 18 def bucktes @bucktes end |
#filename ⇒ String? (readonly)
6 7 8 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 6 def filename @filename end |
#header ⇒ HMap::HMapHeader (readonly)
14 15 16 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 14 def header @header end |
#swapped ⇒ Object (readonly)
11 12 13 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 11 def swapped @swapped end |
Instance Method Details
#description ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 82 def description sum = " Header map: #{filename}\n" + header.description bucktes.each_with_index do |buckte_h, index| sum += "\t- Bucket: #{index}" + Utils.safe_encode(buckte_h.values[0].description, 'UTF-8') unless buckte_h.nil? sum end sum end |
#populate_and_check_magic ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Read just the file’s magic number and check its validity.
58 59 60 61 62 63 64 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 58 def populate_and_check_magic magic = @raw_data[0..3].unpack1('N') raise MagicError, magic unless Utils.magic?(magic) version = @raw_data[4..5].unpack1('n') @swapped = Utils.swapped_magic?(magic, version) end |
#populate_buckets ⇒ Array<HMap::HMapBucket>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All buckets in the file.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 69 def populate_buckets bucket_offset = header.class.bytesize bucktes = [] header.num_buckets.times do |i| bucket = HMapBucket.new_from_bin(swapped, @raw_data[bucket_offset, HMapBucket.bytesize]) bucket_offset += HMapBucket.bytesize next if bucket.key == HEADER_CONST[:HMAP_EMPTY_BUCKT_KEY] bucktes[i] = { bucket => yield(bucket) } end bucktes end |
#populate_fields ⇒ void
This method is public, but should (almost) never need to be called.
This method returns an undefined value.
Populate the instance’s fields with the raw HMap data.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 32 def populate_fields @header = populate_hmap_header string_t = @raw_data[header.strings_offset..-1] @bucktes = populate_buckets do |bucket| bucket_s = bucket.to_a.map do |key| string_t[key..-1].match(/[^\0]+/)[0] end HMapBucketStr.new(*bucket_s) end end |
#populate_hmap_header ⇒ HMap::HMapHeader
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The file’s HMapheader structure.
47 48 49 50 51 52 |
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 47 def populate_hmap_header raise TruncatedFileError if @raw_data.size < HMapHeader.bytesize + 8 * HMapBucket.bytesize populate_and_check_magic HMapHeader.new_from_bin(swapped, @raw_data[0, HMapHeader.bytesize]) end |