Code cleanup

This commit is contained in:
Alexander Simonov 2011-11-17 22:53:04 +02:00
parent 7cb60f5a37
commit 3418d31b44
10 changed files with 87 additions and 144 deletions

View File

@ -1,5 +1,3 @@
require 'zip/constants'
module Zip
class Compressor #:nodoc:all
def finish

View File

@ -17,7 +17,7 @@ module Zip
def finish
until @zlibDeflater.finished?
@outputStream << @zlibDeflater.finish
@outputStream << @zlibDeflater.finish
end
end

View File

@ -6,23 +6,23 @@ module Zip
@outputBuffer=""
@hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST
end
def sysread(numberOfBytes = nil, buf = nil)
readEverything = (numberOfBytes == nil)
while (readEverything || @outputBuffer.length < numberOfBytes)
break if internal_input_finished?
@outputBuffer << internal_produce_input(buf)
readEverything = numberOfBytes.nil?
while (readEverything || @outputBuffer.bytesize < numberOfBytes)
break if internal_input_finished?
@outputBuffer << internal_produce_input(buf)
end
return value_when_finished if @outputBuffer.length==0 && input_finished?
endIndex= numberOfBytes==nil ? @outputBuffer.length : numberOfBytes
return value_when_finished if @outputBuffer.bytesize == 0 && input_finished?
endIndex = numberOfBytes.nil? ? @outputBuffer.bytesize : numberOfBytes
return @outputBuffer.slice!(0...endIndex)
end
def produce_input
if (@outputBuffer.empty?)
return internal_produce_input
return internal_produce_input
else
return @outputBuffer.slice!(0...(@outputBuffer.length))
return @outputBuffer.slice!(0...(@outputBuffer.length))
end
end
@ -53,8 +53,8 @@ module Zip
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
def value_when_finished # mimic behaviour of ruby File object.
return nil if @hasReturnedEmptyString
@hasReturnedEmptyString=true
return if @hasReturnedEmptyString
@hasReturnedEmptyString = true
return ""
end
end

View File

@ -45,11 +45,11 @@ module IOExtras #:nodoc:
def read(numberOfBytes = nil, buf = nil)
tbuf = nil
if @outputBuffer.length > 0
if numberOfBytes <= @outputBuffer.length
if @outputBuffer.bytesize > 0
if numberOfBytes <= @outputBuffer.bytesize
tbuf = @outputBuffer.slice!(0, numberOfBytes)
else
numberOfBytes -= @outputBuffer.length if (numberOfBytes)
numberOfBytes -= @outputBuffer.bytesize if (numberOfBytes)
rbuf = sysread(numberOfBytes, buf)
tbuf = @outputBuffer
tbuf << rbuf if (rbuf)
@ -73,45 +73,45 @@ module IOExtras #:nodoc:
def readlines(aSepString = $/)
retVal = []
each_line(aSepString) { |line| retVal << line }
return retVal
retVal
end
def gets(aSepString=$/)
def gets(aSepString = $/)
@lineno = @lineno.next
return read if aSepString == nil
aSepString="#{$/}#{$/}" if aSepString == ""
bufferIndex=0
return read if aSepString.nil?
aSepString = "#{$/}#{$/}" if aSepString.empty?
bufferIndex = 0
while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil)
bufferIndex=@outputBuffer.length
bufferIndex = @outputBuffer.bytesize
if input_finished?
return @outputBuffer.empty? ? nil : flush
end
@outputBuffer << produce_input
end
sepIndex=matchIndex + aSepString.length
sepIndex = matchIndex + aSepString.bytesize
return @outputBuffer.slice!(0...sepIndex)
end
def flush
retVal=@outputBuffer
retVal = @outputBuffer
@outputBuffer=""
return retVal
end
def readline(aSepString = $/)
retVal = gets(aSepString)
raise EOFError if retVal == nil
return retVal
retVal
end
def each_line(aSepString = $/)
while true
yield readline(aSepString)
end
rescue EOFError
end
alias_method :each, :each_line
end
@ -123,7 +123,7 @@ module IOExtras #:nodoc:
def write(data)
self << data
data.to_s.length
data.to_s.bytesize
end
@ -137,21 +137,20 @@ module IOExtras #:nodoc:
def putc(anObject)
self << case anObject
when Fixnum then anObject.chr
when String then anObject
else raise TypeError, "putc: Only Fixnum and String supported"
end
when Fixnum then anObject.chr
when String then anObject
else raise TypeError, "putc: Only Fixnum and String supported"
end
anObject
end
def puts(*params)
params << "\n" if params.empty?
params.flatten.each {
|element|
params.flatten.each do |element|
val = element.to_s
self << val
self << "\n" unless val[-1,1] == "\n"
}
end
end
end

View File

@ -6,27 +6,27 @@ module Zip
@readSoFar = 0
@hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST
end
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
def sysread(numberOfBytes = nil, buf = nil)
if input_finished?
hasReturnedEmptyStringVal=@hasReturnedEmptyString
@hasReturnedEmptyString=true
return "" unless hasReturnedEmptyStringVal
return nil
hasReturnedEmptyStringVal = @hasReturnedEmptyString
@hasReturnedEmptyString = true
return "" unless hasReturnedEmptyStringVal
return
end
if (numberOfBytes == nil || @readSoFar+numberOfBytes > @charsToRead)
numberOfBytes = @charsToRead-@readSoFar
if (numberOfBytes == nil || @readSoFar + numberOfBytes > @charsToRead)
numberOfBytes = @charsToRead - @readSoFar
end
@readSoFar += numberOfBytes
@inputStream.read(numberOfBytes, buf)
end
def produce_input
sysread(Decompressor::CHUNK_SIZE)
end
def input_finished?
(@readSoFar >= @charsToRead)
end

View File

@ -1,12 +1,3 @@
unless Enumerable.method_defined?(:inject)
module Enumerable #:nodoc:all
def inject(n = 0)
each { |value| n = yield(n, value) }
n
end
end
end
module Enumerable #:nodoc:all
# returns a new array of all the return values not equal to nil
# This implementation could be faster
@ -15,34 +6,10 @@ module Enumerable #:nodoc:all
end
end
unless Object.method_defined?(:object_id)
class Object #:nodoc:all
# Using object_id which is the new thing, so we need
# to make that work in versions prior to 1.8.0
alias object_id id
end
end
unless File.respond_to?(:read)
class File # :nodoc:all
# singleton method read does not exist in 1.6.x
def self.read(fileName)
open(fileName) { |f| f.read }
end
end
end
class String #:nodoc:all
def starts_with(aString)
rindex(aString, 0) == 0
end
def ends_with(aString)
index(aString, -aString.size)
end
def ensure_end(aString)
ends_with(aString) ? self : self + aString
end_with?(aString) ? self : self + aString
end
def lchop
@ -51,7 +18,7 @@ class String #:nodoc:all
end
class Time #:nodoc:all
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
#
# Register CX, the Time:
@ -63,18 +30,18 @@ class Time #:nodoc:all
# Bits 0-4 day (1-31)
# bits 5-8 month (1-12)
# bits 9-15 year (four digit year minus 1980)
def to_binary_dos_time
(sec/2) +
(min << 5) +
(hour << 11)
(min << 5) +
(hour << 11)
end
def to_binary_dos_date
(day) +
(month << 5) +
((year - 1980) << 9)
(month << 5) +
((year - 1980) << 9)
end
# Dos time is only stored with two seconds accuracy
@ -97,10 +64,9 @@ end
class Module #:nodoc:all
def forward_message(forwarder, *messagesToForward)
methodDefs = messagesToForward.map {
|msg|
methodDefs = messagesToForward.map do |msg|
"def #{msg}; #{forwarder}(:#{msg}); end"
}
end
module_eval(methodDefs.join("\n"))
end
end

View File

@ -1,7 +1,7 @@
module Zip
class ZipCentralDirectory
include Enumerable
END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50
MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE = 65536 + 18
STATIC_EOCD_SIZE = 22
@ -27,22 +27,24 @@ module Zip
def write_e_o_c_d(io, offset) #:nodoc:
io <<
[END_OF_CENTRAL_DIRECTORY_SIGNATURE,
[END_OF_CENTRAL_DIRECTORY_SIGNATURE,
0 , # @numberOfThisDisk
0 , # @numberOfDiskWithStartOfCDir
@entrySet? @entrySet.size : 0 ,
@entrySet? @entrySet.size : 0 ,
cdir_size ,
offset ,
@comment ? @comment.length : 0 ].pack('VvvvvVVv')
io << @comment
0 , # @numberOfDiskWithStartOfCDir
@entrySet? @entrySet.size : 0 ,
@entrySet? @entrySet.size : 0 ,
cdir_size ,
offset ,
@comment ? @comment.length : 0 ].pack('VvvvvVVv')
io << @comment
end
private :write_e_o_c_d
def cdir_size #:nodoc:
# does not include eocd
@entrySet.inject(0) { |value, entry| entry.cdir_header_size + value }
end
private :cdir_size
def read_e_o_c_d(io) #:nodoc:
@ -57,33 +59,33 @@ module Zip
@comment = buf.read(commentLength)
raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0
end
def read_central_directory_entries(io) #:nodoc:
begin
io.seek(@cdirOffset, IO::SEEK_SET)
io.seek(@cdirOffset, IO::SEEK_SET)
rescue Errno::EINVAL
raise ZipError, "Zip consistency problem while reading central directory entry"
raise ZipError, "Zip consistency problem while reading central directory entry"
end
@entrySet = ZipEntrySet.new
@size.times {
@entrySet << ZipEntry.read_c_dir_entry(io)
@entrySet << ZipEntry.read_c_dir_entry(io)
}
end
def read_from_stream(io) #:nodoc:
read_e_o_c_d(io)
read_central_directory_entries(io)
end
def get_e_o_c_d(io) #:nodoc:
begin
io.seek(-MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, IO::SEEK_END)
io.seek(-MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, IO::SEEK_END)
rescue Errno::EINVAL
io.seek(0, IO::SEEK_SET)
io.seek(0, IO::SEEK_SET)
rescue Errno::EFBIG # FreeBSD 4.9 raise Errno::EFBIG instead of Errno::EINVAL
io.seek(0, IO::SEEK_SET)
io.seek(0, IO::SEEK_SET)
end
# 'buf = io.read' substituted with lump of code to work around FreeBSD 4.5 issue
retried = false
buf = nil
@ -92,7 +94,7 @@ module Zip
rescue Errno::EFBIG # FreeBSD 4.5 may raise Errno::EFBIG
raise if (retried)
retried = true
io.seek(0, IO::SEEK_SET)
retry
end
@ -101,7 +103,7 @@ module Zip
raise ZipError, "Zip end of central directory signature not found" unless sigIndex
buf=buf.slice!((sigIndex+4)...(buf.size))
def buf.read(count)
slice!(0, count)
slice!(0, count)
end
return buf
end
@ -132,6 +134,6 @@ module Zip
end
end
# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.
# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.

View File

@ -84,7 +84,7 @@ module Zip
compression_method = ZipEntry::DEFLATED, size = 0,
time = Time.now)
super()
if name.starts_with("/")
if name.start_with?("/")
raise ZipEntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
end
@localHeaderOffset = 0

View File

@ -588,7 +588,7 @@ module Zip
end
def expand_path(aPath)
expanded = aPath.starts_with("/") ? aPath : @pwd.ensure_end("/") + aPath
expanded = aPath.start_with?("/") ? aPath : @pwd.ensure_end("/") + aPath
expanded.gsub!(/\/\.(\/|$)/, "")
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, "")
expanded.empty? ? "/" : expanded

View File

@ -17,28 +17,6 @@ end
class StringExtensionsTest < Test::Unit::TestCase
def test_starts_with
assert("hello".starts_with(""))
assert("hello".starts_with("h"))
assert("hello".starts_with("he"))
assert(! "hello".starts_with("hello there"))
assert(! "hello".starts_with(" he"))
assert_raise(TypeError, "type mismatch: NilClass given") {
"hello".starts_with(nil)
}
end
def test_ends_with
assert("hello".ends_with("o"))
assert("hello".ends_with("lo"))
assert("hello".ends_with("hello"))
assert(!"howdy".ends_with("o"))
assert(!"howdy".ends_with("oy"))
assert(!"howdy".ends_with("howdy doody"))
assert(!"howdy".ends_with("doody howdy"))
end
def test_ensure_end
assert_equal("hello!", "hello!".ensure_end("!"))
assert_equal("hello!", "hello!".ensure_end("o!"))