Refactoring, bug fixes

git-svn-id: http://php-reader.googlecode.com/svn/trunk@5 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2008-03-03 11:49:01 +00:00
parent 906e2dc59d
commit 1c99955fe1
3 changed files with 27 additions and 22 deletions

View File

@@ -34,7 +34,7 @@
*/ */
/**#@+ @ignore */ /**#@+ @ignore */
require_once("ReaderException.php"); require_once("Reader/Exception.php");
require_once("Transform.php"); require_once("Transform.php");
/**#@-*/ /**#@-*/
@@ -47,6 +47,7 @@ require_once("Transform.php");
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2006, 2007 The Bearpaw Project Work Group
* @copyright 2007, 2008 BEHR Software Systems * @copyright 2007, 2008 BEHR Software Systems
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version $Rev$ * @version $Rev$
*/ */
class Reader class Reader
@@ -65,12 +66,12 @@ class Reader
* Opens the file given as a parameter. * Opens the file given as a parameter.
* *
* @param string $filename The absolute or relative path to the file. * @param string $filename The absolute or relative path to the file.
* @throws ReaderException if the file cannot be read. * @throws Reader_Exception if the file cannot be read.
*/ */
public function __construct($filename) public function __construct($filename)
{ {
if (($this->_fd = fopen($filename, "rb")) === false) if (($this->_fd = fopen($filename, "rb")) === false)
throw new ReaderException("Unable to open file:" . $filename); throw new Reader_Exception("Unable to open file:" . $filename);
fseek($this->_fd, 0, SEEK_END); fseek($this->_fd, 0, SEEK_END);
$this->_size = ftell($this->_fd); $this->_size = ftell($this->_fd);
@@ -100,13 +101,14 @@ class Reader
* Jumps <var>size</var> amount of bytes in the file stream. * Jumps <var>size</var> amount of bytes in the file stream.
* *
* @return void * @return void
* @throws ReaderException if <var>size</var> attribute is not greater or * @throws Reader_Exception if <var>size</var> attribute is negative.
* equal than zero.
*/ */
public function skip($size) public function skip($size)
{ {
if ($size < 0) if ($size < 0)
throw new ReaderException("Invalid argument"); throw new Reader_Exception("Invalid argument");
if ($size == 0)
return;
fseek($this->_fd, $size, SEEK_CUR); fseek($this->_fd, $size, SEEK_CUR);
} }
@@ -114,13 +116,14 @@ class Reader
* Reads <var>length</var> amount of bytes from the file stream. * Reads <var>length</var> amount of bytes from the file stream.
* *
* @return string Returns read bytes as a string * @return string Returns read bytes as a string
* @throws ReaderException if <var>length</var> attribute is not greater than * @throws Reader_Exception if <var>length</var> attribute is negative.
* zero.
*/ */
public function read($length) public function read($length)
{ {
if ($length <= 0) if ($length < 0)
throw new ReaderException("Invalid argument"); throw new Reader_Exception("Invalid argument");
if ($length == 0)
return "";
return fread($this->_fd, $length); return fread($this->_fd, $length);
} }
@@ -165,7 +168,7 @@ class Reader
* @param string $method The method to be called. * @param string $method The method to be called.
* @param string $params The parameters should the function accept them. * @param string $params The parameters should the function accept them.
* @return mixed * @return mixed
* @throws ReaderException if no such transformer is implemented * @throws Reader_Exception if no such transformer is implemented
*/ */
public function __call($method, $params) { public function __call($method, $params) {
$chunks = array(); $chunks = array();
@@ -177,6 +180,6 @@ class Reader
$this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ? $this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ?
(isset($params[0]) ? $params[0] : 1) : (isset($params[0]) ? $params[0] : 1) :
($chunks[1] == "GUID" ? 16 : $chunks[2] / 8))); ($chunks[1] == "GUID" ? 16 : $chunks[2] / 8)));
} else throw new ReaderException("Unknown method: " . $method); } else throw new Reader_Exception("Unknown method: " . $method);
} }
} }

View File

@@ -39,8 +39,9 @@
* @package php-reader * @package php-reader
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2008 BEHR Software Systems * @copyright 2008 BEHR Software Systems
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version $Rev$ * @version $Rev$
*/ */
class ReaderException extends Exception class Reader_Exception extends Exception
{ {
} }

View File

@@ -40,6 +40,7 @@
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2006, 2007 The Bearpaw Project Work Group
* @copyright 2007, 2008 BEHR Software Systems * @copyright 2007, 2008 BEHR Software Systems
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version $Rev$ * @version $Rev$
* @static * @static
*/ */
@@ -79,7 +80,7 @@ final class Transform
*/ */
public static function getInt64LE($raw) public static function getInt64LE($raw)
{ {
return ReaderUtils::getInt64($raw, self::LITTLE_ENDIAN_ORDER); return self::getInt64($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
@@ -92,7 +93,7 @@ final class Transform
*/ */
public static function getInt64BE($raw) public static function getInt64BE($raw)
{ {
return ReaderUtils::getInt64($raw, self::BIG_ENDIAN_ORDER); return self::getInt64($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
@@ -125,7 +126,7 @@ final class Transform
*/ */
public static function getUInt32LE($raw) public static function getUInt32LE($raw)
{ {
return ReaderUtils::getUInt32($raw, self::LITTLE_ENDIAN_ORDER); return self::getUInt32($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
@@ -135,7 +136,7 @@ final class Transform
*/ */
public static function getUInt32BE($raw) public static function getUInt32BE($raw)
{ {
return ReaderUtils::getUInt32($raw, self::BIG_ENDIAN_ORDER); return self::getUInt32($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
@@ -168,7 +169,7 @@ final class Transform
*/ */
public static function getUInt16LE($raw) public static function getUInt16LE($raw)
{ {
return ReaderUtils::getUInt16($raw, self::LITTLE_ENDIAN_ORDER); return self::getUInt16($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
@@ -178,7 +179,7 @@ final class Transform
*/ */
public static function getUInt16BE($raw) public static function getUInt16BE($raw)
{ {
return ReaderUtils::getUInt16($raw, self::BIG_ENDIAN_ORDER); return self::getUInt16($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
@@ -215,7 +216,7 @@ final class Transform
foreach (unpack(($order == 2 ? "n" : foreach (unpack(($order == 2 ? "n" :
($order == 1 ? "v" : "S")) . "*", $raw) as $char) ($order == 1 ? "v" : "S")) . "*", $raw) as $char)
$string .= pack("S", $char); $string .= pack("S", $char);
return rtrim($string, "\0"); return $string;
} }
/** /**
@@ -225,7 +226,7 @@ final class Transform
*/ */
public static function getString16LE($raw) public static function getString16LE($raw)
{ {
return ReaderUtils::getString16($raw, self::LITTLE_ENDIAN_ORDER); return self::getString16($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
@@ -235,7 +236,7 @@ final class Transform
*/ */
public static function getString16BE($raw) public static function getString16BE($raw)
{ {
return ReaderUtils::getString16($raw, self::BIG_ENDIAN_ORDER); return self::getString16($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**