Class: SharedStringTable

Inherits:
Object
  • Object
show all
Defined in:
lib/surpass/biff_record.rb

Constant Summary collapse

SST_ID =
0x00FC

Instance Method Summary collapse

Constructor Details

#initializeSharedStringTable

Returns a new instance of SharedStringTable.



4
5
6
7
8
9
10
11
# File 'lib/surpass/biff_record.rb', line 4

def initialize
  @sst_record = nil
  @continues = []
  @current_piece = [0,0].pack('V2')

  @str_indexes = {} # TODO replace with array? or is hash more efficient?
  @add_calls = 0
end

Instance Method Details

#add_str(s) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/surpass/biff_record.rb', line 13

def add_str(s)
  @add_calls += 1
  index = @str_indexes[s]
  if index.nil?
    # This is a new string for the SST.
    position = @str_indexes.length
    @str_indexes[s] = position
    index = position
    add_to_sst(s)
  end
  index
end

#add_to_sst(s) ⇒ Object



38
39
40
41
42
43
# File 'lib/surpass/biff_record.rb', line 38

def add_to_sst(s)
  u_str = mock_unicode_string(s)
  raise "very long string" if u_str.length > 0xFFFF
  save_atom(u_str[0...4])
  save_splitted(u_str[4..-1], false)
end

#new_pieceObject

Store the @current_piece in @continues and initialize a new @current_piece



46
47
48
49
50
51
52
53
54
55
# File 'lib/surpass/biff_record.rb', line 46

def new_piece
  if @sst_record.nil?
    # We get here when we first run out of space, or if that never happens then we end
    # up here when everything is finished and we call to_biff for the first time.
    @sst_record = @current_piece
  else
    @continues << [BiffRecord::CONTINUE_RECORD_ID, @current_piece.length].pack('v2') + @current_piece
  end
  @current_piece = ''
end

#save_atom(atom) ⇒ Object



57
58
59
60
61
# File 'lib/surpass/biff_record.rb', line 57

def save_atom(atom)
  free_space = 0x2020 - @current_piece.length
  new_piece if free_space < atom.length
  @current_piece += atom
end

#save_splitted(s, is_unicode_str) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/surpass/biff_record.rb', line 63

def save_splitted(s, is_unicode_str)
  i = 0
  while i < s.length do
    free_space = 0x2020 - @current_piece.length
    tail_length = s.length - i
    need_more_space = free_space < tail_length
    
    if !need_more_space
      atom_length = tail_length
    else
      if is_unicode_str
        atom_length = free_space & 0xFFFE
      else
        atom_length = free_space
      end
    end
    @current_piece += s[i...(i+atom_length)]
    
    if need_more_space
      new_piece
      if is_unicode_str
        @current_piece += "\001"
      else
        @current_piece += "\000"
      end
    end
    
    i += atom_length
  end
end

#str_index(s) ⇒ Object



26
27
28
# File 'lib/surpass/biff_record.rb', line 26

def str_index(s)
  @str_indexes[s]
end

#to_biffObject



30
31
32
33
34
35
36
# File 'lib/surpass/biff_record.rb', line 30

def to_biff
  new_piece # flush the 'current' piece
  result = [SST_ID, @sst_record.length, @add_calls, @str_indexes.length].pack('v2V2')
  result += @sst_record[8..-1]
  result += @continues.join
  result
end