Skip to content
Browse files

Starting to boost IO/File support. Better stream handling.

  • Loading branch information...
1 parent b8ae2d6 commit b4d51a95e0b75e4be751f5b9425d260072e54ae7 @rifraf committed Jun 27, 2010
View
43 IREmbeddedApp/EmbeddedRuby/FileSupport.rb
@@ -14,7 +14,11 @@ def read_embedded_stream(path)
next if load_path_invalid?(loc)
serf_name = get_serf_name(loc, path)
stream = self.open_read(serf_name)
- return stream if stream
+ if stream
+ # Mix in the IO capabilities
+ stream.extend(EmbeddedIOStream) unless stream.respond_to?(:select_binary_reader)
+ return stream
+ end
end
false
end
@@ -24,18 +28,33 @@ def read_embedded_stream(path)
# Very limited support at the moment
#---------------------
class SerfStat
- attr_reader :mtime, :size
- def initialize(filename, content)
+ attr_reader :mtime, :size, :mode, :blksize
+ def initialize(filename, content, dirname)
+ @dirname = @dirname
@mtime = Time.now
@size = content.size
+ @mode = 0444 # Read only
+ @blksize = nil
end
+
+ def symlink?; false; end
+ def file?; !@dirname; end
+ def directory?; !!@dirname; end
+ def chardev?; true; end
+ def blockdev?; true; end
+ def socket?; false; end
+ def pipe?; false; end
end
# Provide File::stat equivalent
def stat(filename)
+ dirname = find_embedded_path(filename)
+ return SerfStat.new(filename, '', dirname) if dirname
str = read_embedded_file(filename)
- SerfStat.new(filename, str) if str
+ return SerfStat.new(filename, str, nil) if str
+ nil
end
+ alias lstat stat
#---------------------
# Provide File::open equivalent
@@ -56,7 +75,7 @@ def open(filename, *args, &blk)
stream = read_embedded_stream(filename)
return nil unless stream
- open_mode.include?('b') ? stream.select_binarymode : stream.select_textmode
+ open_mode.include?('b') ? stream.select_binary_reader : stream.select_text_reader
stream
end
@@ -65,7 +84,7 @@ def open(filename, *args, &blk)
class File
class << self
- alias irembedded_old_stat stat
+ alias irembedded_old_stat stat
def stat(filename)
irembedded_old_stat(filename)
rescue Errno::ENOENT => error
@@ -90,15 +109,21 @@ def expand_path(path, aDirString = nil)
irembedded_old_expand_path(path, aDirString)
end
- # TODO: service blk
alias irembedded_old_open open
def open(filename, *args, &blk)
irembedded_old_open(filename, *args, &blk)
rescue => error
puts "FILE:OPEN(#{filename}, #{args.join(', ')})" if SerfsInstance.debug
stream = SerfsInstance.open(filename, *args, &blk)
- return stream if stream
- raise error
+ raise error unless stream
+ if blk
+ begin
+ return blk.call(stream)
+ ensure
+ stream.close
+ end
+ end
+ return stream
end
alias irembedded_old_new new
View
100 IREmbeddedApp/EmbeddedRuby/IOSupport.rb
@@ -1,104 +1,27 @@
# --------------------------------------------------
-# Patch System::IO::UnmanagedMemoryStream to look
-# more like an open file
+# Module to provide standard IO methods on a stream
+# These are used to extend actual instances when they
+# are created, because we need to override methods
+# such as read() with the Ruby-style version
# --------------------------------------------------
-class System::IO::UnmanagedMemoryStream
+module EmbeddedIOStream
def rewind
- self.position = 0
- @reader = @textmode ? System::IO::StreamReader.new(self) : System::IO::BinaryReader.new(self)
- end
-
- def select_textmode
- @textmode = true
- rewind
- end
-
- def select_binarymode
- @textmode = false
- rewind
- end
-
- def read(num)
- return nil if eof?
- if @textmode
- str = ""
- last_ch = nil
- until num == 0
- ch = reader.Read
- return str if ch < 0
- if (ch == 13) && (reader.Peek == 10)
- ch = reader.Read
- end
- str << ch
- num -= 1
- end
- return str
- else
- buffer = String.CreateBinary(reader.ReadBytes(num))
- (buffer.length == 0) ? nil : buffer
- end
- end
-
- def gets(aSepString = $/)
- # TODO: Reads the next 'line' from the I/O stream; lines are separated by aSepString.
- # A separator of nil reads the entire contents, and a zero-length separator
- # reads the input a paragraph at a time (two successive newlines in the input
- # separate paragraphs). The stream must be opened for reading or an IOerror will
- # be raised. The line read in will be returned and also assigned to $_.
- # Returns nil if called at end of file.
- $_ = ''
- val = read(1)
- return nil unless val
- while val
- $_ << val
- return $_ if (val == aSepString)
- return $_ if (val.length == 0) && $_[-2,2] == "\n\n"
- val = read(1)
+ if self.can_seek
+ self.position = 0
end
- return $_
- end
-
- def readline(aSepString = $/)
- raise EOFError if eof?
- gets aSepString
- end
-
- def eof?
- eof = @textmode ? @reader.end_of_stream : (@reader.peek_char < 0)
end
- def reader
- unless @reader
- rewind
- @reader = System::IO::StreamReader.new(self)
- end
- @reader
- end
-
- private :reader
-end
-
-# --------------------------------------------------
-# Patch System::IO::MemoryStream to look
-# more like an open file
-# TODO: remove duplication with above!!!
-# --------------------------------------------------
-class System::IO::MemoryStream
-
- def rewind
- self.position = 0
- @reader = @textmode ? System::IO::StreamReader.new(self) : System::IO::BinaryReader.new(self)
- end
-
- def select_textmode
+ def select_text_reader
@textmode = true
rewind
+ @reader = System::IO::StreamReader.new(self)
end
- def select_binarymode
+ def select_binary_reader
@textmode = false
rewind
+ @reader = System::IO::BinaryReader.new(self, System::Text::Encoding.ASCII)
end
def read(num)
@@ -160,4 +83,3 @@ def reader
private :reader
end
-
View
13 IREmbeddedApp/EmbeddedRuby/LoadSupport.rb
@@ -1,25 +1,14 @@
# --------------------------------------------------
# Patch load to try Serfs if disk file is not found
# --------------------------------------------------
-def load_embedded_file(path, wrap = false)
- # TODO: If the optional wrap parameter is true, the loaded script will be executed under an anonymous module,
- # TODO: protecting the calling program's global namespace.
- # TODO: In no circumstance will any local variables in the loaded file be propagated to the loading environment.
- str = SerfsInstance.read_embedded_file(path)
- if (str)
- eval(str, nil, '/' + path, 0)
- return true
- end
- false
-end
alias irembedded_old_load load
def load(filename, wrap = false)
filename.gsub!(/^\//,'')
irembedded_old_load(filename, wrap)
rescue LoadError => load_error
$! = nil
- if (load_error.message =~ /#{Regexp.escape filename}\z/) and load_embedded_file(filename, wrap)
+ if (load_error.message =~ /#{Regexp.escape filename}\z/) and load_embedded_string(SerfsInstance.read_embedded_file(filename), filename, wrap)
return true
end
raise load_error
View
28 IREmbeddedApp/EmbeddedRuby/RequireSupport.rb
@@ -19,7 +19,8 @@ def load_path_invalid?(loc)
def get_serf_name(loc, path)
return path[1..-1] if path =~ /^[\/]/
- "#{loc}/#{path}".gsub(/\\/,'/').gsub(/^\/\//, './')
+ return path[2..-1] if path =~ /^\.[\/]/
+ "#{loc}/#{path}".gsub(/\\/,'/').gsub(/^\/\//, './')
end
# Try to read complete file to a string
@@ -34,6 +35,17 @@ def read_embedded_file(path)
false
end
+ # Look for a match in the path
+ def find_embedded_path(path)
+ str = nil
+ $LOAD_PATH.each do |loc|
+ next if load_path_invalid?(loc)
+ serf_name = get_serf_name(loc, path)
+ return serf_name if FolderExists(serf_name)
+ end
+ nil
+ end
+
end
# --------------------------------------------------
@@ -65,7 +77,7 @@ def require(path)
$" << path
puts("Found #{filename}") if SerfsInstance.debug
begin
- eval(str, nil, '/' + filename, 0)
+ load_embedded_string(str, filename, false)
rescue Exception => e
puts("Caught (#{filename}):" + e.message) if SerfsInstance.debug
raise e
@@ -78,3 +90,15 @@ def require(path)
private :irembedded_old_require
private :require
+def load_embedded_string(str, path, wrap = false)
+ # TODO: If the optional wrap parameter is true, the loaded script will be executed under an anonymous module,
+ # TODO: protecting the calling program's global namespace.
+ # TODO: In no circumstance will any local variables in the loaded file be propagated to the loading environment.
+ if (str)
+ eval(str, TOPLEVEL_BINDING, '/' + path, 0)
+ return true
+ end
+ false
+end
+
+#SerfsInstance.debug = true
View
BIN IREmbeddedApp/Serfs.dll
Binary file not shown.
View
BIN IRSinatra/bin/IREmbeddedApp.dll
Binary file not shown.
View
BIN IRSinatra/bin/IRSinatra.exe
Binary file not shown.
View
BIN IRSinatra/bin/merge/IRSinatra.exe
Binary file not shown.

0 comments on commit b4d51a9

Please sign in to comment.
Something went wrong with that request. Please try again.