rfile 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2006, Christopher Maujean and project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of the NGSLib nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/lib/rfile.rb ADDED
@@ -0,0 +1,54 @@
1
+ # $Id: rfile.rb 44 2006-08-13 23:18:33Z cmaujean $
2
+ # See LICENSE for copyright information
3
+ # This class is a line oriented "file" object that operates
4
+ # without keeping the file in memory.
5
+ # Enumerable is mixed in, so see Enumerable for more
6
+ # information.
7
+ class RFile
8
+ include Enumerable
9
+
10
+ # indexes the file.
11
+ #--
12
+ # storing line information (offset and length)
13
+ #++
14
+ def initialize(filename)
15
+ @filename = filename
16
+ @index = []
17
+ @rndindex = []
18
+ count = 1
19
+ offset = 1
20
+
21
+ File.open(@filename).each_line do |line|
22
+ @index[count] = [line.length, offset-1]
23
+ offset += line.length
24
+ count+=1
25
+ end
26
+ @rndindex = @index.clone
27
+ end
28
+
29
+ # returns a random line from the file, deleting its index
30
+ def randomline
31
+ entry = nil
32
+ while entry.nil? and @rndindex.length > 0
33
+ entry = @rndindex.delete_at(rand(@rndindex.length))
34
+ end
35
+ entry.length == 2 and IO.read(@filename, entry[0], entry[1]).chomp unless entry.nil?
36
+ end
37
+
38
+ # returns the line at num
39
+ def line(num)
40
+ entry = @index[num]
41
+ return IO.read(@filename, entry[0], entry[1]).chomp
42
+ end
43
+
44
+ # yields each line in the file, in turn.
45
+ # currently io intensive as it will open and close the file
46
+ # for each line. but thats the price you pay.
47
+ def each
48
+ @index.each_with_index do |entry,i|
49
+ yield line(i) unless entry.nil?
50
+ end
51
+ end
52
+ end
53
+
54
+ # vi:sw=2 ts=2
@@ -0,0 +1,3 @@
1
+ Line 1
2
+ Line 2
3
+ Line 3
data/test/tc_rfile.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rfile'
2
+ require 'test/unit'
3
+
4
+ class TestRFile < Test::Unit::TestCase
5
+ def setup
6
+ @f = RFile.new("rfile/test/data/testfile")
7
+ end
8
+
9
+ def test_rfile
10
+ assert(@f.randomline.is_a?(String), "line is a string")
11
+ testline = @f.randomline
12
+ assert((testline == "Line 1" or testline == "Line 2" or testline == "Line 3"), "Line is valid: #{testline}")
13
+ assert(@f.randomline)
14
+ assert(@f.line(1) == "Line 1", "line(1) returns Line 1: #{@f.line(1)}")
15
+ end
16
+
17
+ def test_enum_mixin
18
+ count = 0
19
+ @f.each_with_index do |l,i|
20
+ case i
21
+ when 0
22
+ assert_equal(l, "Line 1")
23
+ when 1
24
+ assert_equal(l, "Line 2")
25
+ when 2
26
+ assert_equal(l, "Line 3")
27
+ end
28
+ count+=1
29
+ end
30
+ assert count == 3, "EACH TEST: count should be 3 count is #{count}"
31
+
32
+ assert( "Line 3" == @f.detect {|t| t.split(" ")[1].to_i == 3}, "Enum: testing detect { } " )
33
+ assert( @f.reject {|t| t == "Line 3"} == ["Line 1", "Line 2"], "Enum: testing reject { }" )
34
+ assert( @f.collect {|t| "This is " + t } == ["This is Line 1", "This is Line 2", "This is Line 3"], "Enum: testing collect")
35
+ end
36
+ end
37
+
38
+ # vi:sw=2 ts=2
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: rfile
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-08-13 00:00:00 -07:00
8
+ summary: a read only, line oriented, sparse file class
9
+ require_paths:
10
+ - lib
11
+ email: cmaujean@gmail.com
12
+ homepage: http://rubyforge.org/projects/ngslib
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Christopher Maujean
31
+ files:
32
+ - lib/rfile.rb
33
+ - lib/LICENSE
34
+ - test/tc_rfile.rb
35
+ - test/data
36
+ - test/data/testfile
37
+ test_files:
38
+ - test/tc_rfile.rb
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies: []
50
+