Class: Sequence::File

Inherits:
Sequence show all
Includes:
StringLike
Defined in:
lib/sequence/file.rb

Overview

delete/insert at arbitrary location is not supported.

Constant Summary

Constants included from StringLike

StringLike::FFS_4BITTABLE

Constants inherited from Sequence

SubSequence, VERSION

Instance Attribute Summary

Attributes inherited from Sequence

#last_match, #maxmatchlen

Instance Method Summary collapse

Methods included from StringLike

#_anchor, #data_class, #ffs, #fixup_match_result, #fns, #group_anchors, #index, #like, #match, #match_fast, #matchback, #push, #read_til_charset, #rindex, #scan, #scan_until, #scanback, #scanback_until, #unshift

Methods inherited from Sequence

#+, #-, #<<, [], #[], #[]=, #_adjust_pos_on_change, #_default_maxmatchlen, #_delete_position, #_normalize_pos, #_parse_slice_args, #all_data, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #closed?, #delete, #each, #empty?, #end, #end!, #exist?, #existback?, #first, #goto, #holding, #holding!, #holding?, #holding_position, #holding_position!, #holding_position?, #insert, #last, #length, #match?, #matchback?, #more_data?, #move, #move!, #nearbegin, #nearend, #new, #notify_change, #on_change_notify, #original__new, #overwrite, #pop, #pos=, #pos?, #position, #position=, #position?, #pred, #prepend, #prop, #readback1, #readbehind1, #rest_size, #reversed, #shift, #skip, #skip_literal, #skip_until, #skip_until_literal, #skipback, #skipback_until, #slice, #slice!, #slice1, #slice1!, #subseq, #succ, #to_sequence, #was_data?, #write, #writeahead, #writeback, #writebehind

Constructor Details

#initialize(file, mode = "r") ⇒ File

Returns a new instance of File.



17
18
19
20
21
22
23
24
25
26
# File 'lib/sequence/file.rb', line 17

def initialize(file,mode="r")

  case file
  when Integer;  file=IO.new(file,mode)
  when String;   file=File.new(file,mode)
  else #do nothing, file is of a right type (we hope) already
  end
  
  @io = file
end

Instance Method Details

#_pos=(p) ⇒ Object



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

def _pos=(p)
    @io.seek(p,::IO::SEEK_SET)
    p
end

#append(str) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sequence/file.rb', line 138

def append(str)
    Integer===str and str=str.chr
    first=nil
    holding{
      end!
      first=pos
      @io.write str
    }
    notify_change(self,first,0,str.size)      
    self
end

#closeObject



150
151
152
153
# File 'lib/sequence/file.rb', line 150

def close
    @io.close
    super
end

#dataObject



31
# File 'lib/sequence/file.rb', line 31

def data; @io end

#eof?Boolean

Returns:

  • (Boolean)


107
# File 'lib/sequence/file.rb', line 107

def eof?; @io.eof? end

#modify(*args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sequence/file.rb', line 115

def modify(*args)
  data=args.pop
  first,len,only1=_parse_slice_args(*args)
  if first+len==size #working at end of data?
    holding{
      @io.truncate first if len.nonzero?
      goto first
      @io.write data
    }
  elsif len==data.size  #inserted data is same size?
    holding{
      goto first
      @io.write data
    }
  else
    raise ArgumentError,"replace data must be same size or modification must be at very end" 
  end
  notify_change(self,first,len,data.size)
  data
end

#new_dataObject

:stopdoc:



28
29
30
# File 'lib/sequence/file.rb', line 28

def new_data
    ''
end

#posObject



155
# File 'lib/sequence/file.rb', line 155

def pos;     @io.tell end

#read(len) ⇒ Object



75
76
77
# File 'lib/sequence/file.rb', line 75

def read(len)
       @io.read(len) or ""
end

#read!(reverse = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sequence/file.rb', line 94

def read!(reverse=false)
    if reverse                   
        len = @io.pos.nonzero? or return ""
        @io.seek(0, ::IO::SEEK_SET)
        #@io.pos = 0 # BUGGY in v1.8.2
        buffer1 = @io.read(len) || ""
        @io.seek(0, ::IO::SEEK_SET)
    else
        buffer1 = @io.read(nil)
    end
    buffer1
end

#read1Object



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

def read1
    @io.getc
end

#readahead(len) ⇒ Object



78
79
80
81
82
# File 'lib/sequence/file.rb', line 78

def readahead(len)
        buffer1 = read(len)
            @io.seek(-buffer1.size,::IO::SEEK_CUR)
    buffer1
end

#readahead1Object

def write1next(v)

    @io.eof? ? nil : (@io.putc(v);true)
end
def write1next!(v)
    @io.putc(v)
    true
end
def skip1prev
    @io.pos.nonzero? && (@io.seek(-1,::IO::SEEK_CUR);true)
end


47
48
49
50
51
# File 'lib/sequence/file.rb', line 47

def readahead1
    v0 = @io.getc
    v0 && @io.ungetc(v0)
    v0
end

#readback(len) ⇒ Object



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

def readback(len)
  result=readbehind(len)
            @io.seek(-result.size,::IO::SEEK_CUR)
  result
end

#readbehind(len) ⇒ Object



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

def readbehind(len)
        p = @io.pos
        len>p and len=p
        @io.seek(-len,::IO::SEEK_CUR)
        @io.read(len)
end

#sizeObject

def write1after!(v)

    @io.putc(v)
    @io.seek(-1,::IO::SEEK_CUR)
    true
end
def skip1next
    @io.getc ? true : nil
end
def skip1after
    @io.eof? ? nil : true
end
def skip1before
    @io.pos.zero? ? nil : true
end
def scan1next(v)
    v0 = @io.getc
    (v0.nil? || v==v0) ? v0 : (@io.ungetc(v0);nil)
end


72
73
74
# File 'lib/sequence/file.rb', line 72

def size
  @io.stat.size
end