Guest User

Untitled

a guest
Feb 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. # $Id: rfile.rb 61 2006-08-19 22:34:59Z cmaujean $
  2. #
  3.  
  4. require 'rfile/randomstack'
  5. require 'rfile/indexelement'
  6.  
  7. # This class is a line oriented file object that operates
  8. # without keeping the file in memory.
  9. #
  10. # Enumerable is mixed in, see Enumerable for more
  11. # information.
  12. #
  13. class RFile
  14. include Enumerable
  15. attr_accessor :recycle, :filename
  16.  
  17. # parses and indexes <i>filename</i>.
  18. #
  19. # if recycle == true, the randomline
  20. # method will reload the index (fast)
  21. # when it runs out of unique lines to produce
  22. #
  23. # if sep_string is passed, "lines" will be determined
  24. # by sep_string instead of $/
  25. #--
  26. # storing line information (length, offset, )
  27. #++
  28. def initialize(filename, recycle=false, sep_string=$/)
  29. @filename = filename
  30. @recycle = recycle
  31. @sep_string = sep_string
  32. @index = Array.new
  33. @rndindex = []
  34. count = 0
  35. offset = 1
  36.  
  37. File.open(@filename).each_line(@sep_string) do |line|
  38. @index[count] = IndexElement.new([line.length, offset-1, count+1])
  39. offset += line.length
  40. count+=1
  41. end
  42. @rndindex = RandomStack.new(@index.clone)
  43. end
  44.  
  45. # returns a random line from the file. will not repeat lines.
  46. # returns nil when the file is exausted. note: does not modify file.
  47. def randomline
  48. entry = nil
  49. if @recycle and @rndindex.length == 0
  50. @rndindex = RandomStack.new(@index)
  51. end
  52. while(entry.nil? and @rndindex.length > 0)
  53. entry = @rndindex.pop
  54. end
  55. entry.nil? and return nil
  56. return line(entry.linum)
  57. end
  58.  
  59. # return true if there are no lines left for randomline(s) ( only useful if
  60. # recycle=true )
  61. def r_eof?
  62. return true if @rndindex.length == 0
  63. false
  64. end
  65.  
  66. # yields num random lines or returns them as an array. see randomline for details
  67. def randomlines(num) #:yields:line
  68. arr = Array.new
  69. doyield = block_given?
  70. num.times do |i|
  71. rline = randomline()
  72. yield rline if doyield
  73. arr.push rline
  74. end
  75. arr if not doyield
  76. end
  77.  
  78. # returns the number of lines available to randomline based methods
  79. # in the current cycle. useful if you want to know how close you
  80. # are to recycling the file, or how close to r_eof? == true
  81. def length
  82. @rndindex.length
  83. end
  84.  
  85. # returns the line at num (provided num is greater than or equal to 1)
  86. # returns nil if num is larger than the lines available
  87. def line(num)
  88. if (num < 1) or (num > @index.length)
  89. raise "line number: #{num} is out of bounds"
  90. end
  91. entry = @index[num-1]
  92. IO.read(@filename, entry.length, entry.offset).chomp(@sep_string)
  93. end
  94.  
  95. # yields each line in the file, in turn.
  96. #
  97. # <i>note: currently IO intensive as it will open and close the file
  98. # for each line.</i>
  99. #
  100. def each # :yields:line
  101. @index.each do |entry|
  102. yield line(entry.linum) unless entry.nil?
  103. end
  104. end
  105. end
  106.  
  107. require 'rfile/version'
Add Comment
Please, Sign In to add comment