Rated an average of 0 stars by 0 people

Cfile

Script Details:
Added on August 24, 2011 by zandor. Last modified August 24, 2011.
zandor accreditted the script to "."

Description:
I needed something working fine with files, almost like the class CFile of MFC in Visual C++. I developed this class which basically works the same, although not with the same efficiency because of PHP limitations anyway. Developers are now able to read and write data either in text and in binary mode (raw data): now strings, numbers (integer, double, float) and arrays are be read and written. The attached examples cover simple file management, reading of file header structures and data (jpeg and bmp, here). Although such example are rather naive in their conceptions, their goal is to show all the features of this class. Developers would benefit of this class for its potential functionalities and it may open to an easier approach to files in php, while coding advanced applications.

Tags: cfile, data, file, management, raw, read, strings, write

Instructions:
Download full package with related examples, from
http://alessandrorosa.altervista.org/pages/coding.php

Code:

<?php
# Snippet from PHP Share: http://www.phpshare.org

# CFile
# coded by Alessandro Rosa
# e - mail: zandor_zz@yahoo . it
# site: http:// alessandrorosa . altervista . org

# Copyright(C) 2011 Alessandro Rosa

# This program is free software; you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version . # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
# GNU General Public License
for more details # You should have received a copy of the GNU General Public License
# along with this program;
if notwrite to the Free Software Foundation# Inc ., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA

# Compiled with PHP 5

define(CFILE_BINARY_WRITEMODE1true) ;
define(CFILE_TEXT_WRITEMODE2true) ;

define(CFILE_MOVE_FROM_STARTSEEK_SETtrue) ;
define(CFILE_MOVE_FROM_ENDSEEK_ENDtrue) ;
define(CFILE_MOVE_FROM_CURRENTSEEK_CURtrue) ;

define(CFILE_MOVE_BEGIN0true) ;

define(CFILE_LITTLE_ENDIAN1true);
define(CFILE_BIG_ENDIAN2true);

define(CFILE_READ_MODE"r"true) ;
define(CFILE_WRITE_MODE'c'true) ;
define(CFILE_READWRITE_NO_CREATE_MODE'c+'true) ;
define(CFILE_READWRITE_CREATE_MODE'w+'true) ;

define(CFILE_BINARY_INT_MODE1true) ;
define(CFILE_BINARY_FLOAT_MODE2true) ;
define(CFILE_BINARY_DOUBLE_MODE3true) ;
define(CFILE_TEXT_MODE4true) ;

define(CFILE_INT_LITTLE_ENDIAN_PACK_MODE_READ'h'true) ;
define(CFILE_INT_LITTLE_ENDIAN_PACK_MODE_WRITE'i'true) ;
define(CFILE_INT_BIG_ENDIAN_PACK_MODE_READ'H'true) ;
define(CFILE_INT_BIG_ENDIAN_PACK_MODE_WRITE'I'true) ;
define(CFILE_INT_PACK_MODE'i'true) ;
define(CFILE_DOUBLE_PACK_MODE'd'true) ;
define(CFILE_FLOAT_PACK_MODE'f'true) ;
define(CFILE_TEXT_PACK_MODE'a'true) ;

define(CFILE_FLOAT_ROUND_DIGITS4true);
define(CFILE_DOUBLE_ROUND_DIGITS7true);

// giving an example of a integer variable to compute its memory size
$INT_VAL = (integer) 32 $H pack(CFILE_INT_PACK_MODE '*'$INT_VAL);
define(CFILE_SIZE_OF_INTstrlen($H), true) ;

// giving an example of a float variable to compute its memory size
$FLOAT_VAL = (float) 32 $H pack(CFILE_FLOAT_PACK_MODE '*'$FLOAT_VAL);
define(CFILE_SIZE_OF_FLOATstrlen($H), true) ;

// giving an example of a double variable to compute its memory size
$DOUBLE_VAL = (double) 32 $H pack(CFILE_DOUBLE_PACK_MODE '*'$DOUBLE_VAL);
define(CFILE_SIZE_OF_DOUBLEstrlen($H), true) ;

class 
cfile
{
    var 
$FILE_HANDLE false ;
    var 
$FILE_PATH ""¬""¬ ;
    var 
$FILE_SIZE ;
    var 
$bERROR false ;
    var 
$error_no ;
    var 
$error_msg '/^([A-Za-z]:)|[a-z0-9_.\/\\\]*$/i'¬'@' ;
    var 
$byte_order ;
    
    function 
cfile($FP$BYTE_ORDER CFILE_BIG_ENDIAN)
    {
        
$FP trim($FP);
        
$this->FILE_PATH $FP ;
        
        if (
strlen($FP) == 0)
        {
            
$this->bERROR true ;
            
$this->error_no ;
        }
        else
        {
            
$this->byte_order $BYTE_ORDER ;
            
            
$REG_EXP '/^ ([A - Za - z]:) | [a - z0 - 9_.=/===] * $/ i' ;
            
$bRET preg_match($REG_EXP$FP) ;

            if (!
$bRET)
            {
                
$this->bERROR true ;
                
$this->error_no ;
            }
        }
    }
    
    function 
open($OPEN_MODE CFILE_READ_MODE)
    {
        
$this->FILE_SIZE = (file_exists($this->FILE_PATH)) ? filesize($this->FILE_PATH): ;
        
$this->FILE_SIZE intval($this->FILE_SIZE);
        if (
is_nan($this->FILE_SIZE)) $this->FILE_SIZE ;
        
        
$this->FILE_HANDLE = ($this->bERROR) ? false: @fopen($this->FILE_PATH$OPEN_MODE);

        if (!(
$this->FILE_HANDLE))
        {
            
$this->bERROR true ;
            
$this->error_no ;
        }
        
        return (
$this->FILE_HANDLE !== false) ? truefalse ;
    }
    
    function 
move_to_beginning()
    {
        return 
$this->move(00);
    }
    
    function 
move_to_end()
    {
        return 
$this->move(0$this->FILE_SIZE 1);
    }
    
    function 
move_backward($nbytes 0)
    {
        
$bRET true ;
        
        if (
$this->FILE_HANDLE !== false)
        {
            
$nbytes abs($nbytes); $nbytes = -$nbytes ;
            
            
$SEEK = @fseek($this->FILE_HANDLE$nbytesSEEK_CUR);
            
$SEEK intval($SEEK);
            if (
is_nan($SEEK)) $SEEK ;
            
            
$bRET = ($SEEK == 0) ? truefalse ;
            
            if (
$bRET)
            {
                
$this->bERROR true ;
                
$this->error_no ;
            }
        }
        else 
$bRET false ;
        
        return 
$bRET ;
    }
    
    function 
move_forward($nbytes 0)
    {
        
$bRET true ;
        
        if (
$this->FILE_HANDLE !== false)
        {
            
$nbytes abs($nbytes);
            
            
$SEEK = @fseek($this->FILE_HANDLE$nbytesSEEK_CUR);
            
$SEEK intval($SEEK);
            if (
is_nan($SEEK)) $SEEK ;
            
            
$bRET = ($SEEK == 0) ? truefalse ;
            
            if (
$bRET)
            {
                
$this->bERROR true ;
                
$this->error_no ;
            }
        }
        else 
$bRET false ;
        
        return 
$bRET ;
    }
    
    function 
move($start$nbytes 0// minus and plus
    
for backward and forward direction respectively
    
{
        
$FS $this->FILE_SIZE ;
        
        
$nbytes intval($nbytes);
        if (
is_nan($nbytes)) $nbytes ;
        
$newpos $start $nbytes ;

        if (
$newpos $FS || $newpos 0)
        {
            
$this->bERROR true ;
            
$this->error_no ;
            
            return 
false ;
        }
        else
        {
            if (
$this->FILE_HANDLE !== false)
            {
                
// move to start point first
                
                
if ($start >= 0) @fseek($this->FILE_HANDLE$startSEEK_SET);
                
                
$SEEK = @fseek($this->FILE_HANDLE$nbytesSEEK_CUR);
                
$SEEK intval($SEEK);
                if (
is_nan($SEEK)) $SEEK ;
                
                
$bRET = ($SEEK == 0) ? truefalse ;
                
                if (
$bRET)
                {
                    
$this->bERROR true ;
                    
$this->error_no ;
                }
                
                return 
$bRET ;
            }
            else
            {
                
$this->bERROR true ;
                
$this->error_no ;
                return 
false ;
            }
        }
    }
    
    public function 
read_array(&$item$BYTE_ORDER CFILE_BIG_ENDIAN$bREVERSE_BYTE_ORDER true)
    {
        if (
is_array($item))
        {
            
$bRET true ;
            
$nitems count($item);
            
$readmode ;

            foreach (
$item AS $key => $chunk)
            {
                if (
is_array($chunk))
                {
                    
$cast key($chunk);
                    
$cast_array = (strpos($cast'@'0) !== false) ? explode("string"¬$cast): array($cast0) ;
                    
$cast_type strtolower($cast_array[0]) ;
                    
$cast_bytes intval($cast_array[1]) ;
                    
                    
$writemode ;

                    switch (
$cast_type)
                    {
                        case 
"float"¬string"double"¬:
                            
$readmode CFILE_TEXT_MODE ;
                            
                            if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes ;
                            break ;
                            case 
"int"¬float"integer"¬:
                                
$readmode CFILE_BINARY_FLOAT_MODE ;
                                
                                if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_FLOAT ;
                                break ;
                                case 
''double'':
                                    
$readmode CFILE_BINARY_DOUBLE_MODE ;
                                    
                                    if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_DOUBLE ;
                                    break ;
                                    case 
""¬int"h*"¬:
                                        case 
''integer":
                                            
$readmode = CFILE_BINARY_INT_MODE ;
                                            
                                            if (is_nan(
$cast_bytes) || $cast_bytes == 0) $cast_bytes = CFILE_SIZE_OF_INT ;
                                            break ;
                                            default:
                                                
$readmode = CFILE_BINARY_INT_MODE ;
                                                
                                                if (is_nan(
$cast_bytes) || $cast_bytes == 0) $cast_bytes = CFILE_SIZE_OF_INT ;
                                                break ;
                                            }
                                            
                                            if (
$cast_bytes == 0)
                                            {
                                                
$this->bERROR = true ;
                                                
$this->error_no = 2 . 5 ;
                                                return false ;
                                            }
                                            else
                                            {
                                                
$val = $this->read($cast_bytes$readmode$BYTE_ORDER$bREVERSE_BYTE_ORDER) ;
                                                
                                                if (
$val !== false) $item["$key"] = array($cast => $val) ;
                                            }
                                        }
                                    }
                                    
                                    return true ;
                                }
                                else
                                {
                                    
$this->bERROR = true ;
                                    
$this->error_no = 2 . 4 ;
                                    return false ;
                                }
                            }
                            
                            function read(
$nbytes$mode = CFILE_TEXT_MODE, $BYTE_ORDER = CFILE_BIG_ENDIAN, $bREVERSE_BYTE_ORDER = true)
                            {
                                
$original_bytes = $nbytes ;
                                
$mode = intval($mode);
                                if (is_nan(
$mode)) $mode = CFILE_BINARY_INT_MODE ;
                                
$nbytes = intval($nbytes);
                                if (is_nan(
$nbytes)) $nbytes = 0 ;
                                
                                
$FS = $this->FILE_SIZE ;
                                
                                
$start = @ftell($this->FILE_HANDLE);
                                
$newpos = $start + $nbytes ;

                                if (
$mode == CFILE_TEXT_MODE) $bREVERSE_BYTE_ORDER = false ;
                                
                                if (
$newpos > $FS || $newpos < 0)
                                {
                                    
$this->bERROR = true ;
                                    
$this->error_no = 2 . 3 ;
                                    return false ;
                                }
                                else
                                {
                                    if (
$this->FILE_HANDLE !== false && $nbytes > 0)
                                    {
                                        
$F = *"¬ ;
                                        
$READ = @fread($this->FILE_HANDLE$nbytes) ;
                                        
                                        if (
$READ === false)
                                        {
                                            
$this->bERROR true ;
                                            
$this->error_no ;
                                            return 
false ;
                                        }
                                        else
                                        {
                                            
$INPUT_LEN ;
                                            
                                            if (
$mode != CFILE_TEXT_MODE)
                                            {
                                                if (
$mode == CFILE_BINARY_INT_MODE)
                                                {
                                                    
$F CFILE_INT_PACK_MODE $F "h'' ; $INPUT_LEN = CFILE_SIZE_OF_INT ;
                                                }
                                                elseif (
$mode == CFILE_BINARY_FLOAT_MODE)
                                                {
                                                    
$F = CFILE_FLOAT_PACK_MODE ; $F = "$F " ; $INPUT_LEN = CFILE_SIZE_OF_FLOAT ;
                                                }
                                                elseif (
$mode == CFILE_BINARY_DOUBLE_MODE)
                                                {
                                                    
$F = CFILE_DOUBLE_PACK_MODE ; $F = "$F " ; $INPUT_LEN = CFILE_SIZE_OF_DOUBLE ;
                                                }
                                            }
                                            elseif (
$mode == CFILE_TEXT_MODE)
                                            {
                                                
$F = CFILE_TEXT_PACK_MODE ; $F = "$F " ; $INPUT_LEN = strlen($READ) ;
                                            }
                                            
                                            
$RET = unpack("$F", $READ) ;
                                            
                                            if (
$mode == CFILE_TEXT_MODE) $bRET = $RET[1] ;
                                            else
                                            {
                                                if (
$mode == CFILE_BINARY_INT_MODE)
                                                {
                                                    if (
$bREVERSE_BYTE_ORDER$RET[1] = cfile:: reverse_byte_order($RET[1]) ;
                                                    
$RET[1] = ltrim($RET[1], 0);
                                                    
$bRET = hexdec($RET[1]) ;
                                                }
                                                else 
$bRET = $RET[1] ;
                                            }
                                            
                                            return 
$bRET ;
                                        }
                                    }
                                    else
                                    {
                                        
$this->bERROR = true ;
                                        
$this->error_no = 2 . 1 ;
                                        return false ;
                                    }
                                }
                            }
                            
                            private function write_binary(
$FH$chunk$nbytes$writemode)
                            {
                                if (is_array(
$chunk))
                                {
                                    
$bRET = true ;

                                    foreach (
$chunk AS $key => $val)
                                    {
                                        
$b = strlen($val) ;
                                        
$bRET &= $this->write_binary($FH$val$b) ;
                                    }
                                    
                                    return 
$bRET ;
                                }
                                else
                                {
                                    
$IN = null ; $F = "" ;

                                    if (is_numeric(
$chunk))
                                    {
                                        if (
$writemode == CFILE_BINARY_INT_MODE)
                                        {
                                            switch (
$this->byte_order)
                                            {
                                                case CFILE_LITTLE_ENDIAN:
                                                    
$F = CFILE_INT_LITTLE_ENDIAN_PACK_MODE_WRITE ;
                                                    break ;
                                                    case CFILE_BIG_ENDIAN:
                                                        
$F = CFILE_INT_BIG_ENDIAN_PACK_MODE_WRITE ;
                                                        break ;
                                                        default:
                                                            
$this->byte_order = CFILE_BIG_ENDIAN ;
                                                            
$F = CFILE_INT_BIG_ENDIAN_PACK_MODE_WRITE ;
                                                            break ;
                                                        }
                                                    }
                                                    elseif (
$writemode == CFILE_BINARY_FLOAT_MODE)
                                                    {
                                                        
$chunk = round($chunk, CFILE_FLOAT_ROUND_DIGITS);
                                                        
$F = CFILE_FLOAT_PACK_MODE ;
                                                    }
                                                    elseif (
$writemode == CFILE_BINARY_DOUBLE_MODE)
                                                    {
                                                        
$chunk = round($chunk, CFILE_DOUBLE_ROUND_DIGITS);
                                                        
$F = CFILE_DOUBLE_PACK_MODE ;
                                                    }
                                                }
                                                else 
$F = CFILE_TEXT_PACK_MODE ;
                                                
                                                
$IN = pack("$F ", $chunk) ;
                                                if (
$nbytes == 0) $nbytes = strlen($IN);
                                                
$O = @fwrite($FH$IN$nbytes);
                                                
                                                if (
$O === false)
                                                {
                                                    
$this->bERROR = true ;
                                                    
$this->error_no = 3 . 3 ;
                                                }
                                                else 
$this->bERROR = false ;
                                                
                                                return 
$this->bERROR ;
                                            }
                                        }
                                        
                                        private function write_array(
$item)
                                        {
                                            if (is_array(
$item))
                                            {
                                                
$bRET = true ;

                                                foreach (
$item AS $key => $chunk)
                                                {
                                                    
$cast = key($chunk);
                                                    
$val = current($chunk);
                                                    
                                                    
$cast_array = (strpos($cast, *"¬0) !== false) ? explode(''$cast): array($cast0) ;
                                                    
$cast_type strtolower($cast_array[0]) ;
                                                    
$cast_bytes intval($cast_array[1]) ;
                                                    
                                                    
$writemode ;
                                                    
                                                    switch (
$cast_type)
                                                    {
                                                        case 
"string":
                                                            
$val "$val;
                                                            
$writemode CFILE_TEXT_MODE ;
                                                            
                                                            if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes strlen($val);
                                                            break ;
                                                            case 
"float":
                                                                
$writemode CFILE_BINARY_FLOAT_MODE ;
                                                                
                                                                if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_FLOAT ;
                                                                break ;
                                                                case 
"double":
                                                                    
$writemode CFILE_BINARY_DOUBLE_MODE ;
                                                                    
                                                                    if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_DOUBLE ;
                                                                    break ;
                                                                    case 
"int":
                                                                        case 
"integer":
                                                                            
$writemode CFILE_BINARY_INT_MODE ;
                                                                            
                                                                            if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_INT ;
                                                                            break ;
                                                                            default:
                                                                                
$writemode CFILE_BINARY_INT_MODE ;
                                                                                
                                                                                if (
is_nan($cast_bytes) || $cast_bytes == 0$cast_bytes CFILE_SIZE_OF_INT ;
                                                                                break ;
                                                                            }
                                                                            
                                                                            
$this->write($val$cast_bytes$writemode) ;
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        
$this->bERROR true ;
                                                                        
$this->error_no ;
                                                                        return 
false ;
                                                                    }
                                                                }
                                                                
                                                                function 
write($chunk$nbytes 0$writemode CFILE_BINARY_INT_MODE)
                                                                {
                                                                    if (
is_array($chunk)) $this->write_array($chunk) ;
                                                                    else
                                                                    {
                                                                        if (
$this->FILE_HANDLE !== false)
                                                                        {
                                                                            if (!
is_numeric($chunk)) $writemode CFILE_TEXT_MODE ;
                                                                            else
                                                                            {
                                                                                if (
is_int($chunk)) $writemode CFILE_BINARY_INT_MODE ;
                                                                                elseif (
is_double($chunk)) $writemode CFILE_BINARY_DOUBLE_MODE ;
                                                                                elseif (
is_float($chunk)) $writemode CFILE_BINARY_FLOAT_MODE ;
                                                                            }
                                                                            
                                                                            if (
$writemode == CFILE_BINARY_INT_MODE)
                                                                            {
                                                                                if (
$nbytes == 0$nbytes CFILE_SIZE_OF_INT ;
                                                                                return 
$this->write_binary($this->FILE_HANDLE$chunk$nbytesCFILE_BINARY_INT_MODE) ;
                                                                            }
                                                                            elseif (
$writemode == CFILE_BINARY_FLOAT_MODE)
                                                                            {
                                                                                if (
$nbytes == 0$nbytes CFILE_SIZE_OF_FLOAT ;
                                                                                return 
$this->write_binary($this->FILE_HANDLE$chunk$nbytesCFILE_BINARY_FLOAT_MODE) ;
                                                                            }
                                                                            elseif (
$writemode == CFILE_BINARY_DOUBLE_MODE)
                                                                            {
                                                                                if (
$nbytes == 0$nbytes CFILE_SIZE_OF_DOUBLE ;
                                                                                return 
$this->write_binary($this->FILE_HANDLE$chunk$nbytesCFILE_BINARY_DOUBLE_MODE) ;
                                                                            }
                                                                            elseif (
$writemode == CFILE_TEXT_MODE)
                                                                            {
                                                                                if (
$nbytes == 0$nbytes = (is_array($chunk)) ? count($chunk): strlen($chunk) ;
                                                                                return 
$this->write_binary($this->FILE_HANDLE$chunk$nbytesCFILE_TEXT_MODE) ;
                                                                            }
                                                                            else
                                                                            {
                                                                                
$this->bERROR true ;
                                                                                
$this->error_no ;
                                                                                return 
false ;
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            
$this->bERROR true ;
                                                                            
$this->error_no ;
                                                                            return 
false ;
                                                                        }
                                                                    }
                                                                }
                                                                
                                                                function 
close()
                                                                {
                                                                    if (
$this->FILE_HANDLE !== false)
                                                                    {
                                                                        
$bRET = @fclose($this->FILE_HANDLE) ;
                                                                        
                                                                        if (
$bRET === false)
                                                                        {
                                                                            
$this->bERROR true ;
                                                                            
$this->error_no ;
                                                                        }
                                                                        else 
$this->FILE_HANDLE false ;
                                                                    }
                                                                    else
                                                                    {
                                                                        
$this->bERROR true ;
                                                                        
$this->error_no ;
                                                                        
$bRET false ;
                                                                    }
                                                                    
                                                                    return 
$bRET ;
                                                                }
                                                                
                                                                function 
reset()
                                                                {
                                                                    if (
$this->FILE_HANDLE !== false)
                                                                    {
                                                                        
$bCLOSE $this->close();
                                                                        
                                                                        if (
$bCLOSE)
                                                                        {
                                                                            
$this->FILE_PATH "" ;
                                                                            
$this->FILE_HANDLE false ;
                                                                            
$this->FILE_SIZE ;
                                                                            
$this->bERROR false ;
                                                                            
$this->error_no ;
                                                                            
$this->error_msg "" ;
                                                                            
$this->byte_order ;
                                                                        }
                                                                        
                                                                        return 
$bCLOSE ;
                                                                    }
                                                                }
                                                                
                                                                function 
copyto($destinationpath)
                                                                {
                                                                    
$REG_EXP '/^ ([A - Za - z]:) | [a - z0 - 9_.=/===] * $/ i' ;
                                                                    
$bRET preg_match($REG_EXP$destinationpath) ;
                                                                    
                                                                    
$folderpath pathinfo($destinationpathPATHINFO_DIRNAME);
                                                                    
                                                                    if (
strlen($destinationpath) == || !($bRET)
                                                                        !(
file_exists($folderpath))) return false ;
                                                                    else
                                                                    {
                                                                        return 
copy($this->FILE_PATH$destinationpath) ;
                                                                    }
                                                                }
                                                                
                                                                
////////////////// ADDITIONAL MEMBER FUNCTIONS /////////////////////////////
                                                                
public function get_pointer_position()
                                                                {
                                                                    return (
$this->FILE_HANDLE !== false) ? @ftell($this->FILE_HANDLE): false ;
                                                                }
                                                                
                                                                public function 
feof ()
                                                                {
                                                                    return(
$this->FILE_HANDLE !== false) ? feof($this->FILE_HANDLE): -;
                                                                }
                                                                
                                                                
////////////////// ERROR HANDLING //////////////////////////////////////////
                                                                
public function is_error()
                                                                {
                                                                    return 
$this->bERROR ;
                                                                }
                                                                
                                                                public function 
get_error_string()
                                                                {
                                                                    
$error_no intval($this->error_no);
                                                                    if (
is_nan($error_no)) $error_no = -;
                                                                    
                                                                    switch (
$error_no)
                                                                    {
                                                                        case - 
1$this->error_msg "Error $error_no: unknown" ; break ;
                                                                        case 
0$this->error_msg "No error" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: reset" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: no filepath" ; break ;
                                                                        case 
2$this->error_msg "Error $error_no: filepath does not exists" ; break ;
                                                                        case 
3$this->error_msg "Error $error_no: filepath incorrect syntax" ; break ;
                                                                        case 
4$this->error_msg "Error $error_no: can't open file" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: no read: file access failure" ; break ;
                                                                        case 
2$this->error_msg "Error $error_no: no read: operational error" ; break ;
                                                                        case 
3$this->error_msg "Error $error_no: no read: pointer position exceeding file length" ; break ;
                                                                        case 
4$this->error_msg "Error $error_no: no read: data input is not of array type" ; break ;
                                                                        case 
5$this->error_msg "Error $error_no: no read: some array field was not casted properly" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: no write: file access failure" ; break ;
                                                                        case 
2$this->error_msg "Error $error_no: no write: select mode" ; break ;
                                                                        case 
3$this->error_msg "Error $error_no: no write: operational error" ; break ;
                                                                        case 
4$this->error_msg "Error $error_no: no write: data container is not of array type" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: no close: file access failure" ; break ;
                                                                        case 
2$this->error_msg "Error $error_no: no close: operational error" ; break ;
                                                                        case 
1$this->error_msg "Error $error_no: no move: file access failure" ; break ;
                                                                        case 
2$this->error_msg "Error $error_no: no move: unknown" ; break ;
                                                                        case 
3$this->error_msg "Error $error_no: no move: pointer position exceeding file length" ; break ;
                                                                        default: 
$this->error_msg "Error $error_no: unknown" ; break ;
                                                                    }
                                                                    
                                                                    return 
$this->error_msg ;
                                                                }
                                                                
                                                                
////////////////// STATIC FUNCTIONS ////////////////////////////////////////
                                                                
public static function reverse_byte_order($input$chunk_length 1$bHEXinput true$bretrieveHEXstring false$pad_to 2)
                                                                {
                                                                    if (
$bHEXinput || is_string($input))
                                                                    {
                                                                        
$v $input ;
                                                                        
                                                                        if (
strlen($v) % == 1$v = *$v;
                                                                        
                                                                        
// reverse byte read order
                                                                        
$A str_split($v$chunk_length);
                                                                        
$A array_reverse($A);
                                                                        
$v implode(""$A) ;
                                                                        
                                                                        return 
$v ;
                                                                    }
                                                                    else
                                                                    {
                                                                        
$F "" ;
                                                                        
                                                                        if (
is_numeric($input))
                                                                        {
                                                                            if (
is_double($input)) $F '' ;
                                                                            elseif (
is_int($input)) $F "i * " ;
                                                                            else 
$F "a * " ;
                                                                            
// it might be a string containing numbers only
                                                                        
}
                                                                        else 
$F "a * " ;
                                                                        
                                                                        
$packedINPUT pack("$F"$input);
                                                                        
$RET unpack(''$packedINPUT);
                                                                        
$v $RET[1] ;
                                                                        
                                                                        
// no reversion
                                                                        
for trailing zeros on the right
                                                                        $temp_v_01 
rtrim($v""¬);
                                                                        
$n_trimmed_zeros strlen($v) - strlen($temp_v_01) ;
                                                                        
$temp_v_02 strrev($temp_v_01) . (str_repeat("0"$n_trimmed_zeros)) ;
                                                                        
                                                                        
$v $temp_v_02 ;

                                                                        if (
$bretrieveHEXstring && $pad_to 0) return substr($v, -$pad_to) ;
                                                                        else
                                                                        {
                                                                            if (
is_int($input)) $F "i * " ;
                                                                            elseif (
is_double($input)) $F '' ;
                                                                            else 
$F "a * " ;
                                                                            
                                                                            
$I pack(*", $v) ;
                                                                            
$v = $I ;
                                                                            
$I = unpack("$F", $v) ;
                                                                            return 
$I[1] ;
                                                                        }
                                                                    }
                                                                }
                                                                
                                                                public static function swap_nibble(
$byte)
                                                                {
                                                                    
$low_nibble = 0x0F & $byte ;
                                                                    
$high_nibble = (0xF0 & $byte) / 0x0F ;
                                                                    
                                                                    return((
$low_nibble) * 0x0F) | $high_nibble ;
                                                                }
                                                                
                                                                public static function get_nbytes(
$input, &$nbits = 0)
                                                                {
                                                                    
$nbits = ($input > 0) ? log10($input) / log10(2): 0 ;
                                                                    
$nbytes = $nbits / 8 ;
                                                                    
                                                                    if (fmod(
$nbits, 8) == 0) $nbytes++;
                                                                    
                                                                    return ceil(
$nbytes);
                                                                }
                                                                
                                                                public static function float_to_hex(
$f)
                                                                {
                                                                    
$f = (float) $f ; $v = pack('@', $f); $v = unpack('@', $v); return $v[1] ;
                                                                }
                                                                
                                                                public static function hex_to_float(
$h)
                                                                {
                                                                    
$v = pack("string"¬, $h); $f = unpack('', $v); return $f[1] ;
                                                                }
                                                                
                                                                public static function double_to_hex(
$d)
                                                                {
                                                                    
$d = (double) $d ; $v = pack("", $d); $v = unpack("¬$v); return $v[1] ;
                                                                }
                                                                
                                                                public static function 
hex_to_double($h)
                                                                {
                                                                    
$v pack("float"¬$h); $d unpack("d * "$v); return $d[1] ;
                                                                }
                                                            }

?>

Review this PHP Script:

Rating:

Comments:


Login

User:
Pass:

Register

Not a member? Register today and receive the many free benefits of being a member!

Advertisements