"Fossies" - the Fresh Open Source Software Archive

Member "php_writeexcel-0.3.0/class.writeexcel_biffwriter.inc.php" (1 Nov 2005, 6162 Bytes) of package /linux/www/old/php_writeexcel-0.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /*
    4  * Copyleft 2002 Johann Hanne
    5  *
    6  * This is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU Lesser General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2.1 of the License, or (at your option) any later version.
   10  *
   11  * This software is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Lesser General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU Lesser General Public
   17  * License along with this software; if not, write to the
   18  * Free Software Foundation, Inc., 59 Temple Place,
   19  * Suite 330, Boston, MA  02111-1307 USA
   20  */
   21 
   22 /*
   23  * This is the Spreadsheet::WriteExcel Perl package ported to PHP
   24  * Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
   25  */
   26 
   27 class writeexcel_biffwriter {
   28     var $byte_order;
   29     var $BIFF_version;
   30     var $_byte_order;
   31     var $_data;
   32     var $_datasize;
   33     var $_limit;
   34     var $_debug;
   35 
   36     /*
   37      * Constructor
   38      */
   39     function writeexcel_biffwriter() {
   40 
   41         $this->byte_order   = '';
   42         $this->BIFF_version = 0x0500;
   43         $this->_byte_order  = '';
   44         $this->_data        = false;
   45         $this->_datasize    = 0;
   46         $this->_limit       = 2080;
   47 
   48         $this->_set_byte_order();
   49     }
   50 
   51     /*
   52      * Determine the byte order and store it as class data to avoid
   53      * recalculating it for each call to new().
   54      */
   55     function _set_byte_order() {
   56         $this->byteorder=0;
   57         // Check if "pack" gives the required IEEE 64bit float
   58         $teststr = pack("d", 1.2345);
   59         $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
   60 
   61         if ($number == $teststr) {
   62             $this->byte_order = 0; // Little Endian
   63         } elseif ($number == strrev($teststr)) {
   64             $this->byte_order = 1; // Big Endian
   65         } else {
   66             // Give up
   67             trigger_error("Required floating point format not supported ".
   68                           "on this platform. See the portability section ".
   69                           "of the documentation.", E_USER_ERROR);
   70         }
   71 
   72         $this->_byte_order = $this->byte_order;
   73     }
   74 
   75     /*
   76      * General storage function
   77      */
   78     function _prepend($data) {
   79 
   80         if (func_num_args()>1) {
   81             trigger_error("writeexcel_biffwriter::_prepend() ".
   82                           "called with more than one argument", E_USER_ERROR);
   83         }
   84 
   85         if ($this->_debug) {
   86             print "*** writeexcel_biffwriter::_prepend() called:";
   87             for ($c=0;$c<strlen($data);$c++) {
   88                 if ($c%16==0) {
   89                     print "\n";
   90                 }
   91                 printf("%02X ", ord($data[$c]));
   92             }
   93             print "\n";
   94         }
   95 
   96         if (strlen($data) > $this->_limit) {
   97             $data = $this->_add_continue($data);
   98         }
   99 
  100         $this->_data      = $data . $this->_data;
  101         $this->_datasize += strlen($data);
  102     }
  103 
  104     /*
  105      * General storage function
  106      */
  107     function _append($data) {
  108 
  109         if (func_num_args()>1) {
  110             trigger_error("writeexcel_biffwriter::_append() ".
  111                           "called with more than one argument", E_USER_ERROR);
  112         }
  113 
  114         if ($this->_debug) {
  115             print "*** writeexcel_biffwriter::_append() called:";
  116             for ($c=0;$c<strlen($data);$c++) {
  117                 if ($c%16==0) {
  118                     print "\n";
  119                 }
  120                 printf("%02X ", ord($data[$c]));
  121             }
  122             print "\n";
  123         }
  124 
  125         if (strlen($data) > $this->_limit) {
  126             $data = $this->_add_continue($data);
  127         }
  128 
  129         $this->_data      = $this->_data . $data;
  130         $this->_datasize += strlen($data);
  131     }
  132 
  133     /*
  134      * Writes Excel BOF record to indicate the beginning of a stream or
  135      * sub-stream in the BIFF file.
  136      *
  137      * $type = 0x0005, Workbook
  138      * $type = 0x0010, Worksheet
  139      */
  140     function _store_bof($type) {
  141 
  142         $record  = 0x0809; // Record identifier
  143         $length  = 0x0008; // Number of bytes to follow
  144 
  145         $version = $this->BIFF_version;
  146 
  147         // According to the SDK $build and $year should be set to zero.
  148         // However, this throws a warning in Excel 5. So, use these
  149         // magic numbers.
  150         $build  = 0x096C;
  151         $year   = 0x07C9;
  152 
  153         $header = pack("vv",   $record, $length);
  154         $data   = pack("vvvv", $version, $type, $build, $year);
  155 
  156         $this->_prepend($header . $data);
  157     }
  158 
  159     /*
  160      * Writes Excel EOF record to indicate the end of a BIFF stream.
  161      */
  162     function _store_eof() {
  163 
  164         $record = 0x000A; // Record identifier
  165         $length = 0x0000; // Number of bytes to follow
  166 
  167         $header = pack("vv", $record, $length);
  168 
  169         $this->_append($header);
  170     }
  171 
  172     /*
  173      * Excel limits the size of BIFF records. In Excel 5 the limit is 2084
  174      * bytes. In Excel 97 the limit is 8228 bytes. Records that are longer
  175      * than these limits must be split up into CONTINUE blocks.
  176      *
  177      * This function take a long BIFF record and inserts CONTINUE records as
  178      * necessary.
  179      */
  180     function _add_continue($data) {
  181 
  182         $limit  = $this->_limit;
  183         $record = 0x003C; // Record identifier
  184 
  185         // The first 2080/8224 bytes remain intact. However, we have to change
  186         // the length field of the record.
  187         $tmp = substr($data, 0, $limit);
  188         $data = substr($data, $limit);
  189         $tmp = substr($tmp, 0, 2) . pack ("v", $limit-4) . substr($tmp, 4);
  190 
  191         // Strip out chunks of 2080/8224 bytes +4 for the header.
  192         while (strlen($data) > $limit) {
  193             $header  = pack("vv", $record, $limit);
  194             $tmp    .= $header;
  195             $tmp    .= substr($data, 0, $limit);
  196             $data    = substr($data, $limit);
  197         }
  198 
  199         // Mop up the last of the data
  200         $header  = pack("vv", $record, strlen($data));
  201         $tmp    .= $header;
  202         $tmp    .= $data;
  203 
  204         return $tmp;
  205     }
  206 
  207 }
  208 
  209 ?>