Class: Cabriolet::HLP::QuickHelp::OffsetCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/hlp/quickhelp/offset_calculator.rb

Overview

Calculates file offsets for QuickHelp structure

Class Method Summary collapse

Class Method Details

.calculate(topics:, contexts:, context_map:) ⇒ Hash

Calculate all offsets in the file

Parameters:

  • topics (Array<Hash>)

    Compressed topics

  • contexts (Array<String>)

    Context strings

  • context_map (Array<Integer>)

    Context topic indices

Returns:

  • (Hash)

    Calculated offsets



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cabriolet/hlp/quickhelp/offset_calculator.rb', line 14

def self.calculate(topics:, contexts:, context_map:)
  offsets = {}

  # Start after file header (70 bytes = 2 signature + 68 header)
  current_offset = 70

  # Topic index: (topic_count + 1) * 4 bytes
  offsets[:topic_index] = current_offset
  topic_count = topics.size
  current_offset += (topic_count + 1) * 4

  # Context strings: sum of string lengths + null terminators
  offsets[:context_strings] = current_offset
  contexts.each do |ctx|
    current_offset += ctx.bytesize + 1 # +1 for null terminator
  end

  # Context map: context_count * 2 bytes
  offsets[:context_map] = current_offset
  current_offset += context_map.size * 2

  # Keywords: not implemented yet, set to 0
  offsets[:keywords] = 0

  # Huffman tree: not implemented yet, set to 0
  offsets[:huffman_tree] = 0

  # Topic text: starts after context map
  offsets[:topic_text] = current_offset

  # Calculate topic text offsets
  offsets[:topic_offsets] = []
  topics.each do |topic|
    offsets[:topic_offsets] << (current_offset - offsets[:topic_text])
    current_offset += topic[:compressed_length]
  end
  # Add end marker
  offsets[:topic_offsets] << (current_offset - offsets[:topic_text])

  # Total database size
  offsets[:database_size] = current_offset

  offsets
end