Class: RBase::MemoFile::DBase3MemoFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rbase/memo_file.rb

Constant Summary collapse

HEADER_SIZE =
512
BLOCK_SIZE =
512
BLOCK_TERMINATOR =
"\x1a\x1a"

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DBase3MemoFile

Returns a new instance of DBase3MemoFile.



19
20
21
22
23
24
25
# File 'lib/rbase/memo_file.rb', line 19

def initialize(name)
  @file = File.open(name)
  
  @header = @file.read(HEADER_SIZE)
  @next_block = header.unpack('@0L')
  @version = header.unpack('@16c')
end

Instance Method Details

#read(index) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbase/memo_file.rb', line 27

def read(index)
  @file.pos = index*BLOCK_SIZE + HEADER_SIZE

  result = ''
  loop do
    data = @file.read(BLOCK_SIZE)
    terminator_pos = data.index(BLOCK_TERMINATOR)
    if terminator_pos
      break result + data[0, terminator_pos]
    end
    result += data
  end
end

#write(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbase/memo_file.rb', line 41

def write(value)
  @file.pos = @next_block*BLOCK_SIZE + HEADER_SIZE
  value += BLOCK_TERMINATOR
  blocks_num = (value.length+511)/512
  @file.write [value].pack("a#{512*blocks_num}")
  
  position = @next_block
  @next_block += blocks_num
  update_header
  
  position
end