Class: DBF::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/dbf/header.rb

Constant Summary collapse

HEADER_SIZE =
32

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Header

Returns a new instance of Header.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dbf/header.rb', line 9

def initialize(data)
  # Pad a nil or truncated header read so unpacking a short file yields
  # empty values instead of raising.
  data = data.to_s.b.ljust(HEADER_SIZE, "\x00")
  @version = data.unpack1('H2')
  @encoding_key = nil
  @encoding = nil

  case @version
  when '02'
    @record_count, @record_length = data.unpack('x v x3 v')
    @header_length = 521
  else
    @record_count, @header_length, @record_length, @encoding_key = data.unpack('x x3 V v2 x17 H2')
    @encoding = DBF::ENCODINGS[@encoding_key]
  end
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



7
8
9
# File 'lib/dbf/header.rb', line 7

def encoding
  @encoding
end

#encoding_keyObject (readonly)

Returns the value of attribute encoding_key.



7
8
9
# File 'lib/dbf/header.rb', line 7

def encoding_key
  @encoding_key
end

#header_lengthObject (readonly)

Returns the value of attribute header_length.



7
8
9
# File 'lib/dbf/header.rb', line 7

def header_length
  @header_length
end

#record_countObject (readonly)

Returns the value of attribute record_count.



7
8
9
# File 'lib/dbf/header.rb', line 7

def record_count
  @record_count
end

#record_lengthObject (readonly)

Returns the value of attribute record_length.



7
8
9
# File 'lib/dbf/header.rb', line 7

def record_length
  @record_length
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/dbf/header.rb', line 7

def version
  @version
end