Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/file.rb

Constant Summary collapse

@@vfile =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/file.rb', line 7

def initialize(path)
  if @@vfile.include? path
    vfile = @@vfile[path]
    @io = vfile.io
    @hash = @io.string.hash
  else
    @io = StringIO.new ''
    @hash = ''.hash
    @@vfile[path] = self
  end
  @path = path
  @buf = []
  @old_hash = @hash - 1
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



4
5
6
# File 'lib/file.rb', line 4

def io
  @io
end

Class Method Details

.foreach(path, rs = '', &block) ⇒ Object



121
122
123
# File 'lib/file.rb', line 121

def self.foreach(path, rs = '',&block)
  self.open(path).each_line(block)
end

.open(path, &block) ⇒ Object



125
126
127
128
129
# File 'lib/file.rb', line 125

def self.open(path,&block)
  f = self.new(path)
  block.call f if block
  f
end

Instance Method Details

#<<(str) ⇒ Object



22
23
24
# File 'lib/file.rb', line 22

def <<(str)
  self.write(str)
end

#clearObject



108
109
110
111
112
# File 'lib/file.rb', line 108

def clear
  @io.close
  @io = StringIO.new ''
  self
end

#closeObject



114
115
116
117
118
119
# File 'lib/file.rb', line 114

def close
  @buf = nil
  @hash = nil
  @old_hash = nil
  @path = nil
end

#each_line(rs = "", limit = 0) ⇒ Object



36
37
38
39
40
# File 'lib/file.rb', line 36

def each_line(rs = "",limit = 0)
  @io.string.split("\n").each do |x|
    yield x
  end
end

#getcObject



42
43
44
45
46
47
48
# File 'lib/file.rb', line 42

def getc
  unless @old_hash == @hash    
    @old_hash = @hash
    @buf = self.to_s.split('')
  end    
  @buf.shift
end

#getsObject



50
51
52
53
54
55
56
57
# File 'lib/file.rb', line 50

def gets
  buf = ''
  ch = ''
  while (ch = self.getc) != "\n" && ch 
    buf << ch
  end
  buf << ch.to_s
end

#read(length = nil, outbuf = '') ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/file.rb', line 63

def read(length = nil,outbuf = '')
  buf = ''
  if length
    (0..length).each do
      buf << self.getc
    end
    outbuf.sub!(/^.#{length}/,buf)
  else
    outbuf.gsub!(/./,'')
    outbuf << self.readlines.join('')
  end
  buf
end

#read_nonblock(maxlen, outbuf = '') ⇒ Object



77
78
79
# File 'lib/file.rb', line 77

def read_nonblock(maxlen, outbuf = '')
  read(maxlen,outbuf)
end

#readcharObject

Raises:

  • (EOFError)


89
90
91
92
# File 'lib/file.rb', line 89

def readchar
  ch = self.getc
  raise EOFError unless ch
end

#readlineObject

Raises:

  • (EOFError)


94
95
96
97
98
# File 'lib/file.rb', line 94

def readline
  line = self.gets
  raise EOFError unless line
  line
end

#readlines(rs = '', limit = 0) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/file.rb', line 100

def readlines(rs = '',limit = 0)
  lines = []
  self.each_line(rs,limit) do |l|
    lines << l
  end
  lines
end

#readpartial(maxlen, outbuf = '') ⇒ Object



81
82
83
# File 'lib/file.rb', line 81

def readpartial(maxlen, outbuf = '')
  read(maxlen,outbuf)
end

#sysread(maxlen, outbuf = '') ⇒ Object



85
86
87
# File 'lib/file.rb', line 85

def sysread(maxlen, outbuf = '')
  read(maxlen,outbuf)
end

#to_sObject



26
27
28
# File 'lib/file.rb', line 26

def to_s
  @io.string
end

#ungetc(ch) ⇒ Object



59
60
61
# File 'lib/file.rb', line 59

def ungetc(ch)
  @buf.unshift(ch)
end

#write(str) ⇒ Object



30
31
32
33
34
# File 'lib/file.rb', line 30

def write(str)
  c = @io.write(str)
  @hash = self.to_s.hash
  c
end