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