Class: Zim::ZimFile

Inherits:
Object
  • Object
show all
Defined in:
lib/zim/file.rb

Overview

Main class of the zim-ruby library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ ZimFile

load data from the given file

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zim/file.rb', line 27

def initialize(filename)
  @file = File.new(filename)

  @header = FileHeader.new(self)
  raise InvalidZimMagic if(@header.magic != ZIM_MAGIC)

  seek(@header.mime_list_pos)
  @mime_types = StringList.new(self)

  seek(@header.url_pos)
  @urls = Directory.new(self, @header.article_count, Url)

  seek(@header.title_pos)
  @titles = TitleList.new(self, @header.article_count)

  seek(@header.cluster_pos)
  @clusters = Directory.new(self, @header.cluster_count, ClusterEntry)
end

Instance Attribute Details

#clustersObject (readonly)

cluster list (used internally)



21
22
23
# File 'lib/zim/file.rb', line 21

def clusters
  @clusters
end

#fileObject (readonly)

IO associated with this ZimFile



24
25
26
# File 'lib/zim/file.rb', line 24

def file
  @file
end

#headerObject (readonly)

header informations (used internally)



9
10
11
# File 'lib/zim/file.rb', line 9

def header
  @header
end

#mime_typesObject (readonly)

list of included mime types (used internally)



12
13
14
# File 'lib/zim/file.rb', line 12

def mime_types
  @mime_types
end

#titlesObject (readonly)

mapping of titles to urls (used internally)



18
19
20
# File 'lib/zim/file.rb', line 18

def titles
  @titles
end

#urlsObject (readonly)

Directory of urls (used internally)



15
16
17
# File 'lib/zim/file.rb', line 15

def urls
  @urls
end

Instance Method Details

#[](idx) ⇒ Object

access a url by full url (including namespace) e.g.: zim[‘/A/Table of Contents’]



48
49
50
# File 'lib/zim/file.rb', line 48

def [](idx)
  @urls.detect { |x| x.to_s == idx }
end

#read_cstr(pos = nil, io = nil) ⇒ Object

:nodoc:



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zim/file.rb', line 108

def read_cstr(pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?

  str = ''
  begin
    c = io.read(1)
    str += c unless c[0] == 0
  end while(c[0] != 0)

  str
end

#read_int16(pos = nil, io = nil) ⇒ Object

:nodoc:



68
69
70
71
72
73
# File 'lib/zim/file.rb', line 68

def read_int16(pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?
  s = io.read(2)
  (s[1] << 8) | s[0]
end

#read_int32(pos = nil, io = nil) ⇒ Object

:nodoc:



75
76
77
78
79
80
81
82
83
# File 'lib/zim/file.rb', line 75

def read_int32(pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?
  s = io.read(4)
  (s[3] << 24) |
    (s[2] << 16) |
    (s[1] << 8) |
    s[0]
end

#read_int64(pos = nil, io = nil) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zim/file.rb', line 85

def read_int64(pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?
  s = io.read(8)
  (s[7] << 56) |
    (s[6] << 48) |
    (s[5] << 40) |
    (s[4] << 32) |
    (s[3] << 24) |
    (s[2] << 16) |
    (s[1] << 8) |
    s[0]
end

#read_int8(pos = nil, io = nil) ⇒ Object

:nodoc:



62
63
64
65
66
# File 'lib/zim/file.rb', line 62

def read_int8(pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?
  io.read(1)[0]
end

#read_str(size, pos = nil, io = nil) ⇒ Object

:nodoc:



99
100
101
102
103
104
105
106
# File 'lib/zim/file.rb', line 99

def read_str(size, pos = nil, io = nil) # :nodoc:
  io ||= @file
  seek(pos, io) unless pos.nil?

  return '' if size == 0

  io.read(size)
end

#seek(pos, io = nil) ⇒ Object

:nodoc:



52
53
54
55
# File 'lib/zim/file.rb', line 52

def seek(pos, io = nil) # :nodoc:
  io ||= @file
  io.seek(pos)
end

#tell(io = nil) ⇒ Object

:nodoc:



57
58
59
60
# File 'lib/zim/file.rb', line 57

def tell(io = nil) # :nodoc:
  io ||= @file
  io.tell
end