Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/excel/BIFFwriter.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003 *  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
00004 *
00005 *  The majority of this is _NOT_ my code.  I simply ported it from the
00006 *  PERL Spreadsheet::WriteExcel module.
00007 *
00008 *  The author of the Spreadsheet::WriteExcel module is John McNamara 
00009 *  <jmcnamara@cpan.org>
00010 *
00011 *  I _DO_ maintain this code, and John McNamara has nothing to do with the
00012 *  porting of this code to PHP.  Any questions directly related to this
00013 *  class library should be directed to me.
00014 *
00015 *  License Information:
00016 *
00017 *    Spreadsheet::WriteExcel:  A library for generating Excel Spreadsheets
00018 *    Copyright (C) 2002 Xavier Noguer xnoguer@rezebra.com
00019 *
00020 *    This library is free software; you can redistribute it and/or
00021 *    modify it under the terms of the GNU Lesser General Public
00022 *    License as published by the Free Software Foundation; either
00023 *    version 2.1 of the License, or (at your option) any later version.
00024 *
00025 *    This library is distributed in the hope that it will be useful,
00026 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028 *    Lesser General Public License for more details.
00029 *
00030 *    You should have received a copy of the GNU Lesser General Public
00031 *    License along with this library; if not, write to the Free Software
00032 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00033 */
00034 
00051 class BIFFWriter
00052 {
00053     var $_BIFF_version = 0x0500;
00054 
00060     function BIFFwriter()
00061     {
00062         // The byte order of this architecture. 0 => little endian, 1 => big endian
00063         $this->_byte_order = '';
00064         // The string containing the data of the BIFF stream
00065         $this->_data       = '';
00066         // Should be the same as strlen($this->_data)
00067         $this->_datasize   = 0;
00068         // The maximun length for a BIFF record. See _add_continue()
00069         $this->_limit      = 2080;   
00070         // Set the byte order
00071         $this->_set_byte_order();
00072     }
00073 
00080     function _set_byte_order()
00081     {
00082         if ($this->_byte_order == '')
00083         {
00084             // Check if "pack" gives the required IEEE 64bit float
00085             $teststr = pack("d", 1.2345);
00086             $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
00087             if ($number == $teststr) {
00088                 $byte_order = 0;    // Little Endian
00089             }
00090             elseif ($number == strrev($teststr)){
00091                 $byte_order = 1;    // Big Endian
00092             }
00093             else {
00094                 // Give up. I'll fix this in a later version.
00095                 die("Required floating point format not supported ".
00096                     "on this platform. See the portability section ".
00097                     "of the documentation."
00098                    );
00099             }
00100         }
00101         $this->_byte_order = $byte_order;
00102     }
00103 
00110     function _prepend($data)
00111     {
00112         if (strlen($data) > $this->_limit) {
00113             $data = $this->_add_continue($data);
00114         }
00115         $this->_data      = $data.$this->_data;
00116         $this->_datasize += strlen($data);
00117     }
00118 
00125     function _append($data)
00126     {
00127         if (strlen($data) > $this->_limit) {
00128             $data = $this->_add_continue($data);
00129         }
00130         $this->_data      = $this->_data.$data;
00131         $this->_datasize += strlen($data);
00132     }
00133 
00141     function _store_bof($type)
00142     {
00143         $record  = 0x0809;        // Record identifier
00144         $length  = 0x0008;        // Number of bytes to follow
00145         $version = $this->_BIFF_version;
00146    
00147         // According to the SDK $build and $year should be set to zero.
00148         // However, this throws a warning in Excel 5. So, use these
00149         // magic numbers.
00150         $build   = 0x096C;
00151         $year    = 0x07C9;
00152    
00153         $header  = pack("vv",   $record, $length);
00154         $data    = pack("vvvv", $version, $type, $build, $year);
00155         $this->_prepend($header.$data);
00156     }
00157 
00163     function _store_eof() 
00164     {
00165         $record    = 0x000A;   // Record identifier
00166         $length    = 0x0000;   // Number of bytes to follow
00167         $header    = pack("vv", $record, $length);
00168         $this->_append($header);
00169     }
00170 
00183     function _add_continue($data)
00184     {
00185         $limit      = $this->_limit;
00186         $record     = 0x003C;         // Record identifier
00187  
00188         // The first 2080/8224 bytes remain intact. However, we have to change
00189         // the length field of the record.
00190         $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
00191         
00192         $header = pack("vv", $record, $limit);  // Headers for continue records
00193  
00194         // Retrieve chunks of 2080/8224 bytes +4 for the header.
00195         for($i = $limit; $i < strlen($data) - $limit; $i += $limit)
00196         {
00197             $tmp .= $header;
00198             $tmp .= substr($data, $i, $limit);
00199         }
00200 
00201         // Retrieve the last chunk of data
00202         $header  = pack("vv", $record, strlen($data) - $i);
00203         $tmp    .= $header;
00204         $tmp    .= substr($data,$i,strlen($data) - $i);
00205  
00206         return($tmp);
00207     }
00208 }
00209 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations