15b562819da927b7066c9033b92e602fcd1a60ae
[rubygit.git] / lib / git / raw / internal / mmap.rb
blob15b562819da927b7066c9033b92e602fcd1a60ae
1 begin
2   require 'mmap'
3 rescue LoadError
5 module Git 
6   module Raw 
7     module Internal
8       class Mmap
9         def initialize(file)
10           @file = file
11           @offset = nil
12         end
14         def unmap
15           @file = nil
16         end
18         def [](*idx)
19           idx = idx[0] if idx.length == 1
20           case idx
21           when Range
22             offset = idx.first
23             len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
24           when Fixnum
25             offset = idx
26             len = nil
27           when Array
28             offset, len = idx
29           else
30             raise RuntimeError, "invalid index param: #{idx.class}"
31           end
32           if @offset != offset
33             @file.seek(offset)
34           end
35           @offset = offset + len ? len : 1
36           if not len
37             @file.read(1)[0]
38           else
39             @file.read(len)
40           end
41         end
42       end
43     end
44   end 
45 end
47 end     # rescue LoadError