Class: OLEWriter
- Inherits:
-
IO
- Object
- IO
- OLEWriter
- Defined in:
- lib/spreadsheet/olewriter.rb
Constant Summary collapse
- MaxSize =
Not meant for public consumption
7087104- BlockSize =
4096- BlockDiv =
512- ListBlocks =
127
Instance Attribute Summary collapse
-
#biff_size ⇒ Object
readonly
Returns the value of attribute biff_size.
-
#big_blocks ⇒ Object
readonly
Returns the value of attribute big_blocks.
-
#book_size ⇒ Object
readonly
Returns the value of attribute book_size.
-
#list_blocks ⇒ Object
readonly
Returns the value of attribute list_blocks.
-
#root_start ⇒ Object
readonly
Returns the value of attribute root_start.
-
#size_allowed ⇒ Object
readonly
Returns the value of attribute size_allowed.
Instance Method Summary collapse
-
#calculate_sizes ⇒ Object
Calculate various sizes needed for the OLE stream.
-
#close ⇒ Object
Write root entry, big block list and close the filehandle.
-
#initialize(arg, &block) ⇒ OLEWriter
constructor
Accept an IO object, a fileno, or a String.
-
#set_size(size = BlockSize) ⇒ Object
Set the size of the data to be written to the OLE stream.
-
#write_big_block_depot ⇒ Object
Write a big block depot.
-
#write_header ⇒ Object
Write the OLE header block.
-
#write_padding ⇒ Object
Pad the end of the file.
-
#write_pps(name, type, dir, start, size) ⇒ Object
Write property sheet in property storage.
-
#write_property_storage ⇒ Object
Write property storage.
Constructor Details
#initialize(arg, &block) ⇒ OLEWriter
Accept an IO object, a fileno, or a String
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/spreadsheet/olewriter.rb', line 20 def initialize(arg, &block) if arg.kind_of?(String) super(File.open(arg, "w+").fileno, "w+", &block) elsif arg.respond_to?(:fileno) super(arg.fileno, "w+", &block) else super(arg, "w+", &block) end binmode @biff_only = false @size_allowed = true @biff_size = 0 @book_size = 0 @big_blocks = 0 @list_blocks = 0 @root_start = 0 @block_count = 4 end |
Instance Attribute Details
#biff_size ⇒ Object (readonly)
Returns the value of attribute biff_size.
16 17 18 |
# File 'lib/spreadsheet/olewriter.rb', line 16 def biff_size @biff_size end |
#big_blocks ⇒ Object (readonly)
Returns the value of attribute big_blocks.
16 17 18 |
# File 'lib/spreadsheet/olewriter.rb', line 16 def big_blocks @big_blocks end |
#book_size ⇒ Object (readonly)
Returns the value of attribute book_size.
16 17 18 |
# File 'lib/spreadsheet/olewriter.rb', line 16 def book_size @book_size end |
#list_blocks ⇒ Object (readonly)
Returns the value of attribute list_blocks.
16 17 18 |
# File 'lib/spreadsheet/olewriter.rb', line 16 def list_blocks @list_blocks end |
#root_start ⇒ Object (readonly)
Returns the value of attribute root_start.
17 18 19 |
# File 'lib/spreadsheet/olewriter.rb', line 17 def root_start @root_start end |
#size_allowed ⇒ Object (readonly)
Returns the value of attribute size_allowed.
17 18 19 |
# File 'lib/spreadsheet/olewriter.rb', line 17 def size_allowed @size_allowed end |
Instance Method Details
#calculate_sizes ⇒ Object
Calculate various sizes needed for the OLE stream
61 62 63 64 65 |
# File 'lib/spreadsheet/olewriter.rb', line 61 def calculate_sizes @big_blocks = (@book_size.to_f/BlockDiv.to_f).ceil @list_blocks = (@big_blocks / ListBlocks) + 1 @root_start = @big_blocks end |
#close ⇒ Object
Write root entry, big block list and close the filehandle.
68 69 70 71 72 73 74 75 |
# File 'lib/spreadsheet/olewriter.rb', line 68 def close if @size_allowed == true write_padding write_property_storage write_big_block_depot end super end |
#set_size(size = BlockSize) ⇒ Object
Set the size of the data to be written to the OLE stream
-
(1 x end words)) = 13842
MaxSize = @big_blocks * 512 bytes = 7087104
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/spreadsheet/olewriter.rb', line 46 def set_size(size = BlockSize) raise MaxSizeError if size > MaxSize @biff_size = size if biff_size > BlockSize @book_size = size else @book_size = BlockSize end @size_allowed = true end |
#write_big_block_depot ⇒ Object
Write a big block depot
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/spreadsheet/olewriter.rb', line 104 def write_big_block_depot total_blocks = @list_blocks * 128 used_blocks = @big_blocks + @list_blocks + 2 marker = [-3].pack("V") eoc = [-2].pack("V") unused = [-1].pack("V") num_blocks = @big_blocks - 1 1.upto(num_blocks){|n| write([n].pack("V")) } write eoc write eoc 1.upto(@list_blocks){ write(marker) } used_blocks.upto(total_blocks){ write(unused) } end |
#write_header ⇒ Object
Write the OLE header block
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/spreadsheet/olewriter.rb', line 78 def write_header return if @biff_only == true calculate_sizes root_start = @root_start write([0xD0CF11E0, 0xA1B11AE1].pack("NN")) write([0x00, 0x00, 0x00, 0x00].pack("VVVV")) write([0x3E, 0x03, -2, 0x09].pack("vvvv")) write([0x06, 0x00, 0x00].pack("VVV")) write([@list_blocks, root_start].pack("VV")) write([0x00, 0x1000,-2].pack("VVV")) write([0x00, -2 ,0x00].pack("VVV")) unused = [-1].pack("V") 1.upto(@list_blocks){ root_start += 1 write([root_start].pack("V")) } @list_blocks.upto(108){ write(unused) } end |
#write_padding ⇒ Object
Pad the end of the file
169 170 171 172 173 174 175 176 177 |
# File 'lib/spreadsheet/olewriter.rb', line 169 def write_padding min_size = 512 min_size = BlockSize if @biff_size < BlockSize if @biff_size % min_size != 0 padding = min_size - (@biff_size % min_size) write("\0" * padding) end end |
#write_pps(name, type, dir, start, size) ⇒ Object
Write property sheet in property storage
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/spreadsheet/olewriter.rb', line 136 def write_pps(name, type, dir, start, size) length = 0 ord_name = [] unless name.empty? name += "\0" ord_name = name.unpack("c*") length = name.length * 2 end zero = [0].pack("C") unknown = [0].pack("V") write(ord_name.pack("v*")) for n in 1..64-length write(zero) end write([length,type,-1,-1,dir].pack("vvVVV")) for n in 1..5 write(unknown) end for n in 1..4 write([0].pack("V")) end write([start,size].pack("VV")) write(unknown) end |
#write_property_storage ⇒ Object
Write property storage
128 129 130 131 132 133 |
# File 'lib/spreadsheet/olewriter.rb', line 128 def write_property_storage write_pps('Root Entry', 0x05, 1, -2, 0x00) write_pps('Book', 0x02, -1, 0x00, @book_size) write_pps("", 0x00, -1, 0x00, 0x0000) write_pps("", 0x00, -1, 0x00, 0x0000) end |