Class: MppReader::Blocks::Var2Data

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

Overview

Variable-length field values, indexed by a VarMeta block. Each item is a u32 size followed by the payload (ported from MPXJ Var2Data).

Instance Method Summary collapse

Constructor Details

#initialize(var_meta, data) ⇒ Var2Data

Returns a new instance of Var2Data.



6
7
8
9
# File 'lib/mpp_reader/blocks/var2_data.rb', line 6

def initialize(var_meta, data)
  @meta = var_meta
  @data = data
end

Instance Method Details

#byte_string(unique_id, type) ⇒ Object

Single-byte NUL-terminated string (used by notes/RTF fields).



22
23
24
25
26
27
# File 'lib/mpp_reader/blocks/var2_data.rb', line 22

def byte_string(unique_id, type)
  value = bytes(unique_id, type)
  return nil unless value

  value.sub(/\0.*\z/m, "")
end

#bytes(unique_id, type) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/mpp_reader/blocks/var2_data.rb', line 11

def bytes(unique_id, type)
  offset = @meta.offset(unique_id, type)
  return nil if offset.nil? || offset + 4 > @data.bytesize

  size = @data.byteslice(offset, 4).unpack1("V")
  return nil if size.negative? || offset + 4 + size > @data.bytesize

  @data.byteslice(offset + 4, size)
end

#string(unique_id, type) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/mpp_reader/blocks/var2_data.rb', line 29

def string(unique_id, type)
  value = bytes(unique_id, type)
  return nil unless value

  value.force_encoding(Encoding::UTF_16LE)
       .encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
       .sub(/\0.*\z/m, "")
end