added Matthias and Simon to credits for the gitrb code
[rubygit.git] / lib / git / raw / internal / mmap.rb
blob78de164879238d84f0cf9d558bfb13f1af001cb4
2 # converted from the gitrb project
4 # authors: 
5 #    Matthias Lederhofer <matled@gmx.net>
6 #    Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
8 # provides native ruby access to git objects and pack files
11 begin
12   require 'mmap'
13 rescue LoadError
15 module Git 
16   module Raw 
17     module Internal
18       class Mmap
19         def initialize(file)
20           @file = file
21           @offset = nil
22         end
24         def unmap
25           @file = nil
26         end
28         def [](*idx)
29           idx = idx[0] if idx.length == 1
30           case idx
31           when Range
32             offset = idx.first
33             len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
34           when Fixnum
35             offset = idx
36             len = nil
37           when Array
38             offset, len = idx
39           else
40             raise RuntimeError, "invalid index param: #{idx.class}"
41           end
42           if @offset != offset
43             @file.seek(offset)
44           end
45           @offset = offset + len ? len : 1
46           if not len
47             @file.read(1)[0]
48           else
49             @file.read(len)
50           end
51         end
52       end
53     end
54   end 
55 end
57 end     # rescue LoadError