lib/writeexcel/biffwriter.rb in writeexcel-0.3.5 vs lib/writeexcel/biffwriter.rb in writeexcel-0.4.0
- old
+ new
@@ -1,5 +1,6 @@
+# -*- coding: utf-8 -*-
#
# BIFFwriter - An abstract base class for Excel workbooks and worksheets.
#
#
# Used in conjunction with WriteExcel
@@ -33,11 +34,11 @@
@ignore_continue = 0
# Open a tmp file to store the majority of the Worksheet data. If this fails,
# for example due to write permissions, store the data in memory. This can be
# slow for large files.
- @filehandle = Tempfile.new('spreadsheetwriteexcel')
+ @filehandle = Tempfile.new('writeexcel')
@filehandle.binmode
# failed. store temporary data in memory.
@using_tmpfile = @filehandle ? true : false
@@ -75,41 +76,41 @@
#
# General storage function
#
def prepend(*args)
d = args.join
- d = add_continue(d) if d.length > @limit
+ d = add_continue(d) if d.bytesize > @limit
- @datasize += d.length
+ @datasize += d.bytesize
@data = d + @data
print "prepend\n" if defined?($debug)
print d.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ') + "\n\n" if defined?($debug)
- return d
+ d
end
###############################################################################
#
# _append($data)
#
# General storage function
#
def append(*args)
- d = args.join
+ d = args.collect{ |a| a.dup.force_encoding('ASCII-8BIT') }.join
# Add CONTINUE records if necessary
- d = add_continue(d) if d.length > @limit
+ d = add_continue(d) if d.bytesize > @limit
if @using_tmpfile
@filehandle.write d
- @datasize += d.length
+ @datasize += d.bytesize
else
- @datasize += d.length
+ @datasize += d.bytesize
@data = @data + d
end
print "append\n" if defined?($debug)
print d.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ') + "\n\n" if defined?($debug)
- return d
+ d
end
###############################################################################
#
# get_data().
@@ -135,11 +136,11 @@
if @using_tmpfile
return @filehandle.read(buflen)
end
# No data to return
- return nil
+ nil
end
###############################################################################
#
# _store_bof($type)
@@ -210,32 +211,30 @@
# the length field of the record.
#
# in perl
# $tmp = substr($data, 0, $limit, "");
- if data.length > @limit
+ if data.bytesize > @limit
tmp = data[0, @limit]
data[0, @limit] = ''
else
tmp = data.dup
data = ''
end
tmp[2, 2] = [@limit-4].pack('v')
# Strip out chunks of 2080/8224 bytes +4 for the header.
- while (data.length > @limit)
+ while (data.bytesize > @limit)
header = [record, @limit].pack("vv")
- tmp = tmp + header + data[0, @limit]
+ tmp += header + data[0, @limit]
data[0, @limit] = ''
end
# Mop up the last of the data
- header = [record, data.length].pack("vv")
- tmp = tmp + header + data
-
- return tmp
+ header = [record, data.bytesize].pack("vv")
+ tmp += header + data
end
###############################################################################
#
# _add_mso_generic()
@@ -249,25 +248,27 @@
# other methods to create specific mso records.
#
# Returns the packed record.
#
def add_mso_generic(type, version, instance, data, length = nil)
- length = length.nil? ? data.length : length
+ length = length.nil? ? data.bytesize : length
# The header contains version and instance info packed into 2 bytes.
header = version | (instance << 4)
record = [header, type, length].pack('vvV') + data
-
- return record
end
def not_using_tmpfile
@filehandle.close(true) if @filehandle
@filehandle = nil
@using_tmpfile = nil
end
def clear_data_for_test # :nodoc:
@data = ''
+ end
+
+ def cleanup # :nodoc:
+ @filehandle.close(true) if @filehandle
end
end