From 65867866a870be8fbef90d67bd0fef8776aee09e Mon Sep 17 00:00:00 2001 From: shugo Date: Sat, 15 Oct 2005 14:54:03 +0000 Subject: * lib/net/ftp.rb: (getbinaryfile): allow nil for localfile, and returns retrieved data if localfile is nil. * lib/net/ftp.rb: (gettextfile): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9394 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/ftp.rb | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index 20dc4eeec1..fe244f0775 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -507,43 +507,60 @@ module Net # # Retrieves +remotefile+ in binary mode, storing the result in +localfile+. + # If +localfile+ is nil, returns retrieved data. # If a block is supplied, it is passed the retrieved data in +blocksize+ # chunks. # def getbinaryfile(remotefile, localfile = File.basename(remotefile), - blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data - if @resume - rest_offset = File.size?(localfile) - f = open(localfile, "a") - else - rest_offset = nil - f = open(localfile, "w") + blocksize = DEFAULT_BLOCKSIZE) # :yield: data + result = nil + if localfile + if @resume + rest_offset = File.size?(localfile) + f = open(localfile, "a") + else + rest_offset = nil + f = open(localfile, "w") + end + elsif !block_given? + result = "" end begin - f.binmode + f.binmode if localfile retrbinary("RETR " + remotefile, blocksize, rest_offset) do |data| - f.write(data) - yield(data) if block + f.write(data) if localfile + yield(data) if block_given? + result.concat(data) if result end + return result ensure - f.close + f.close if localfile end end # # Retrieves +remotefile+ in ASCII (text) mode, storing the result in - # +localfile+. If a block is supplied, it is passed the retrieved data one + # +localfile+. + # If +localfile+ is nil, returns retrieved data. + # If a block is supplied, it is passed the retrieved data one # line at a time. # - def gettextfile(remotefile, localfile = File.basename(remotefile), &block) # :yield: line - f = open(localfile, "w") + def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line + result = nil + if localfile + f = open(localfile, "w") + elsif !block_given? + result = "" + end begin retrlines("RETR " + remotefile) do |line| - f.puts(line) - yield(line) if block + f.puts(line) if localfile + yield(line) if block_given? + result.concat(line + "\n") if result end + return result ensure - f.close + f.close if localfile end end -- cgit v1.2.3