Class: MppReader::Blocks::FixedData

Inherits:
Object
  • Object
show all
Defined in:
lib/mpp_reader/blocks/fixed_data.rb

Overview

Fixed-size records (tasks, resources, assignments), located by the offsets in a FixedMeta block: item i spans from its offset to the next item’s offset (the last item runs to the end of the block). Ported from MPXJ FixedData; sizes are clamped to the available bytes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixed_meta, data, max_expected_size: 0) ⇒ FixedData

Returns a new instance of FixedData.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 27

def initialize(fixed_meta, data, max_expected_size: 0)
  @item_count = fixed_meta.item_count
  @items = Array.new(@item_count)

  @item_count.times do |i|
    offset = fixed_meta.item_offset(i)
    next if offset.nil? || offset.negative? || offset > data.bytesize

    size = if i + 1 == @item_count
             data.bytesize - offset
           else
             fixed_meta.item_offset(i + 1).to_i - offset
           end

    available = data.bytesize - offset
    if size.negative? || size > available
      size = max_expected_size.zero? ? available : [max_expected_size, available].min
    end
    size = max_expected_size if !max_expected_size.zero? && size > max_expected_size

    @items[i] = data.byteslice(offset, size) if size.positive?
  end
end

Instance Attribute Details

#item_countObject (readonly)

Returns the value of attribute item_count.



8
9
10
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 8

def item_count
  @item_count
end

Class Method Details

.fixed_size(data, item_size) ⇒ Object

For blocks whose meta offsets are unreliable but whose record size is fixed and known (e.g. assignment data, 110 bytes per record).



12
13
14
15
16
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 12

def self.fixed_size(data, item_size)
  instance = allocate
  instance.send(:init_fixed_size, data, item_size)
  instance
end

.meta_offsets_with_size(fixed_meta, data, item_size) ⇒ Object

Items located by meta offsets but with a known fixed size, overriding the size implied by consecutive offsets (e.g. link records, 20 bytes each).



21
22
23
24
25
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 21

def self.meta_offsets_with_size(fixed_meta, data, item_size)
  instance = allocate
  instance.send(:init_meta_offsets_with_size, fixed_meta, data, item_size)
  instance
end

Instance Method Details

#[](index) ⇒ Object



51
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 51

def [](index) = @items[index]

#index_from_offset(offset) ⇒ Object

Index of the record starting at the given byte offset, or nil.



54
55
56
# File 'lib/mpp_reader/blocks/fixed_data.rb', line 54

def index_from_offset(offset)
  @offsets&.index(offset)
end