Updated sample data / sample data extraction routine

Implemented initial MagicFileResource class

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5883 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2007-10-24 02:59:59 +00:00
parent 3ced96f8be
commit aca88deda4
2 changed files with 128 additions and 20 deletions

View file

@ -40,13 +40,6 @@ class MagicDb extends Object {
**/
var $db = array();
/**
* The file currently being read. Can be a string or a file resource
*
* @var mixed
*/
var $_file = null;
/**
* Reads a MagicDb from various formats
*
@ -122,11 +115,13 @@ class MagicDb extends Object {
}
$columns = explode("\t", $line);
if (in_array($columns[0]{0}, array('&', '>'))) {
if (in_array($columns[0]{0}, array('>', '&'))) {
$format[] = $columns;
} elseif (!empty($format)) {
$db['database'][] = $format;
$format = array();
$format = array($columns);
} else {
$format = array($columns);
}
}
@ -163,13 +158,126 @@ class MagicDb extends Object {
return false;
}
if (file_exists($file)) {
$this->_file =& new File($file);
} else {
$this->_file = $file;
$matches = array();
$MagicFileResource =& new MagicFileResource($file);
foreach ($this->db['database'] as $format) {
$magic = $format[0];
$match = $MagicFileResource->test($magic);
if ($match === false) {
continue;
}
$matches[] = $magic;
}
return $matches;
}
}
/**
* undocumented class
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
*/
class MagicFileResource extends Object{
/**
* undocumented variable
*
* @var unknown
* @access public
*/
var $resource = null;
/**
* undocumented variable
*
* @var unknown
* @access public
*/
var $offset = 0;
/**
* undocumented function
*
* @param unknown $file
* @return void
* @access public
*/
function __construct($file) {
if (file_exists($file)) {
$this->resource =& new File($file);
} else {
$this->resource = $file;
}
}
/**
* undocumented function
*
* @param unknown $magic
* @return void
* @access public
*/
function test($magic) {
@list($offset, $type, $expected, $comment) = $magic;
$val = $this->extract($offset, $type, $expected);
return $val == $expected;
}
/**
* undocumented function
*
* @param unknown $type
* @param unknown $length
* @return void
* @access public
*/
function read($length = null) {
if (!is_object($this->resource)){
return substr($this->resource, $this->offset, $length);
} else {
return $this->resource->read($length);
}
}
/**
* undocumented function
*
* @param unknown $type
* @param unknown $expected
* @return void
* @access public
*/
function extract($offset, $type, $expected) {
switch ($type) {
case 'string':
$this->offset($offset);
$val = $this->read(strlen($expected));
if ($val === $expected) {
return true;
}
break;
}
}
/**
* undocumented function
*
* @param unknown $offset
* @param unknown $whence
* @return void
* @access public
*/
function offset($offset = null) {
if (is_null($offset)) {
if (!is_object($this->resource)) {
return $this->offset;
}
return $this->offset;
}
if (!ctype_digit($offset)) {
return false;
}
if (is_object($this->resource)) {
$this->resource->offset($offset);
} else {
$this->offset = $offset;
}
}
}

File diff suppressed because one or more lines are too long