+++ /dev/null
-<?php
-
-/**
- * @brief Regular expression filter for string iterators
- * @author Marcus Boerger
- * @version 1.0
- *
- * Instances of this class act as a filter around iterators whose elements
- * are strings. In other words you can put an iterator into the constructor
- * and the instance will only return elements which match the given regular
- * expression.
- */
-class KeyFilter implements Iterator
-{
- protected $it;
- protected $regex;
- protected $key;
- protected $curr;
-
- /**
- * Constructs a filter around an iterator whose elemnts are strings.
- * If the given iterator is of type spl_sequence then its rewind()
- * method is called.
- *
- * @param it Object that implements at least spl_forward
- * @param regex Regular expression used as a filter.
- */
- function __construct(Iterator $it, $regex) {
- $this->it = $it;
- $this->regex = $regex;
- $this->fetch();
- }
-
- /**
- * Rewind input iterator
- */
- function rewind() {
- $this->it->rewind();
- }
-
- /**
- * Destruct the iterator.
- */
- function __destruct() {
- unset($this->it);
- }
-
- /**
- * Fetch next element and store it.
- *
- * @return void
- */
- protected function fetch() {
- $this->key = false;
- $this->curr = false;
- while ($this->it->valid()) {
- $key = $this->it->key();
- if (ereg($this->regex, $key)) {
- $this->key = $key;
- $this->curr = $this->it->current();
- return;
- }
- $this->it->next();
- };
- }
-
- /**
- * Move to next element
- *
- * @return void
- */
- function next() {
- $this->it->next();
- $this->fetch();
- }
-
- /**
- * @return Whether more elements are available
- */
- function valid() {
- return $this->key !== false;
- }
-
- /**
- * @return The current key
- */
- function key() {
- return $this->key;
- }
-
- /**
- * @return The current value
- */
- function current() {
- return $this->curr;
- }
-
- /**
- * hidden __clone
- */
- protected function __clone() {
- // disallow clone
- }
-}
-
-?>
\ No newline at end of file
<?php
+/** \internal
+ * Tries to load class $classname from directory $dir.
+ */
function __load_class($classname, $dir)
{
$file = $dir . '/' . $classname . '.inc';
return false;
}
+/**
+ * @brief Class loader for SPL example classes
+ * @author Marcus Boerger
+ * @version 1.0
+ *
+ * Loads classes automatically from include_path as given by ini or from
+ * current directory of script or include file.
+ */
function __autoload($classname) {
$classname = strtolower($classname);
foreach(split(':', ini_get('include_path')) as $dir)
*
* Note: configure with --enable-dba
*
- * (c) Marcus Boerger, 2003
+ * (c) Marcus Boerger, 2003 - 2004
*/
if ($argc < 4) {
exit(1);
}
-class DbaArray implements ArrayAccess {
- private $db;
-
- function __construct($file, $handler)
- {
- $this->db = dba_popen($file, "c", $handler);
- if (!$this->db) {
- throw new exception("Databse could not be opened");
- }
- }
-
- function __destruct()
- {
- dba_close($this->db);
- }
-
- function get($name)
- {
- $data = dba_fetch($name, $this->db);
- if($data) {
- if (ini_get('magic_quotes_runtime')) {
- $data = stripslashes($data);
- }
- //return unserialize($data);
- return $data;
- }
- else
- {
- return NULL;
- }
- }
-
- function set($name, $value)
- {
- //dba_replace($name, serialize($value), $this->db);
- dba_replace($name, $value, $this->db);
- return $value;
- }
-
- function exists($name)
- {
- return dba_exists($name, $this->db);
- }
-
- function del($name)
- {
- return dba_delete($name, $this->db);
- }
-}
+if (!class_exists("DbaReader", false)) require_once("dbareader.inc");
try {
if ($argc > 2) {
*
* Note: configure with --enable-dba
*
- * (c) Marcus Boerger, 2003
+ * (c) Marcus Boerger, 2003 - 2004
*/
if ($argc < 3) {
exit(1);
}
-require_once("dba_reader.inc");
-require_once("KeyFilter.inc");
+if (!class_exists("DbaReader")) require_once("dbareader.inc");
+if (!class_exists("KeyFilter")) require_once("keyfilter.inc");
$db = new DbaReader($argv[1], $argv[2]);
if ($argc>3) {
- $db = new keyFilter($db, $argv[3]);
+ $db = new KeyFilter($db, $argv[3]);
}
foreach($db as $key => $val) {
--- /dev/null
+<?php
+
+if (!class_exists("DbaReader")) require_once("dbareader.inc");
+
+/** \ingroup Examples
+ * @brief This implements a DBA Array
+ * @author Marcus Boerger
+ * @version 1.0
+ */
+class DbaArray extends DbaReader implements ArrayAccess
+{
+
+ /**
+ * Open database $file with $handler in read only mode.
+ *
+ * @param file Database file to open.
+ * @param handler Handler to use for database access.
+ */
+ function __construct($file, $handler)
+ {
+ $this->db = dba_popen($file, "c", $handler);
+ if (!$this->db) {
+ throw new exception("Databse could not be opened");
+ }
+ }
+
+ /**
+ * Close database.
+ */
+ function __destruct()
+ {
+ parent::__destruct();
+ }
+
+ /**
+ * Read an entry.
+ *
+ * @param $name key to read from
+ * @return value associated with $name
+ */
+ function offsetGet($name)
+ {
+ $data = dba_fetch($name, $this->db);
+ if($data) {
+ if (ini_get('magic_quotes_runtime')) {
+ $data = stripslashes($data);
+ }
+ //return unserialize($data);
+ return $data;
+ }
+ else
+ {
+ return NULL;
+ }
+ }
+
+ /**
+ * Set an entry.
+ *
+ * @param $name key to write to
+ * @param $value value to write
+ */
+ function offsetSet($name, $value)
+ {
+ //dba_replace($name, serialize($value), $this->db);
+ dba_replace($name, $value, $this->db);
+ return $value;
+ }
+
+ /**
+ * @return whether key $name exists.
+ */
+ function offsetExists($name)
+ {
+ return dba_exists($name, $this->db);
+ }
+
+ /**
+ * Delete a key/value pair.
+ *
+ * @param $name key to delete.
+ */
+ function offsetUnset($name)
+ {
+ return dba_delete($name, $this->db);
+ }
+}
+
+?>
\ No newline at end of file
<?php
-/**
- * @brief This implements a Dba Iterator.
+/** \ingroup Examples
+ * @brief This implements a DBA Iterator.
* @author Marcus Boerger
* @version 1.0
*/
class DbaReader implements Iterator
{
- private $db = NULL;
+ protected $db = NULL;
private $key = false;
private $val = false;
<?php
+/** \ingroup Examples
+ * @brief A filtered DirectoryIterator
+ * @author Marcus Boerger
+ * @version 1.0
+ *
+ * This Iteraotr takes a pathname from which it creates a DirectoryIterator
+ * and makes it recursive. Further more it filters the entries '.' and '..'.
+ */
class DirectoryFilterDots extends FilterIterator implements RecursiveIterator
{
- function __construct($path) {
+ /** Construct from a path.
+ * @param $path directory to iterate
+ */
+ function __construct($path)
+ {
parent::__construct(new DirectoryIterator($path));
}
-
- function accept() {
+
+ /** @return whether the current entry is neither '.' nor '..'
+ */
+ function accept()
+ {
return !$this->it->isDot();
}
- function hasChildren() {
+ /** @return whether the current entry is a directory
+ */
+ function hasChildren()
+ {
return $this->it->hasChildren();
}
- function getChildren() {
+ /** @return the current subdirectory as a new DirectoryFilterDots instance.
+ */
+ function getChildren()
+ {
return new DirectoryFilterDots($this->it->getPathname());
}
- function key() {
+ /** @return the current entries path name
+ */
+ function key()
+ {
return $this->it->getPathname();
}
}
+++ /dev/null
-<?php
-
-class DirectoryGraphIterator extends DirectoryTreeIterator
-{
- function __construct($path)
- {
- RecursiveIteratorIterator::__construct(new CachingRecursiveIterator(new ParentIterator(new RecursiveDirectoryIterator($path)), CIT_CALL_TOSTRING|CIT_CATCH_GET_CHILD), 1);
- }
-}
-
-?>
\ No newline at end of file
<?php
+/** \ingroup Examples
+ * @brief A directory iterator that does not show '.' and '..'.
+ * @author Marcus Boerger
+ * @version 1.0
+ */
class DirectoryTree extends RecursiveIteratorIterator
{
+ /** Construct from a path.
+ * @param $path directory to iterate
+ */
function __construct($path) {
parent::__construct(new DirectoryFilterDots($path));
}
<?php
+/** \ingroup Examples
+ * @brief DirectoryIterator to generate ASCII graphic directory trees
+ * @author Marcus Boerger
+ * @version 1.0
+ */
class DirectoryTreeIterator extends RecursiveIteratorIterator
{
+ /** Construct from a path.
+ * @param $path directory to iterate
+ */
function __construct($path)
{
parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), CIT_CALL_TOSTRING|CIT_CATCH_GET_CHILD), 1);
}
-
+
+ /** @return the current element prefixed with ASCII graphics
+ */
function current()
{
$tree = '';
return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
. $this->getSubIterator($l)->__toString();
}
-
+
+ /** Aggregates the inner iterator
+ */
function __call($func, $params)
{
return call_user_func_array(array($this->getSubIterator(), $func), $params);
<?php
-/**
+/** \ingroup Examples
* @brief An empty Iterator
* @author Marcus Boerger
* @version 1.0
*/
class EmptyIterator implements Iterator
{
+ /** No operation.
+ * @return void
+ */
function rewind()
{
// nothing to do
}
+ /** @return \c false
+ */
function valid()
{
return false;
}
+ /** This function must not be called. It throws an exception upon access.
+ * @throw Exception
+ * @return void
+ */
function current()
{
throw new Exception('Accessing the value of an EmptyIterator');
}
+ /** This function must not be called. It throws an exception upon access.
+ * @throw Exception
+ * @return void
+ */
function key()
{
throw new Exception('Accessing the key of an EmptyIterator');
}
+ /** No operation.
+ * @return void
+ */
function next()
{
// nothing to do
<?php
+if (!class_exists("FindFile")) require_once("findfile.inc");
+if (!class_exists("AppendIterator")) require_once("appenditerator.inc");
+
/**
* @brief Base class to find files
* @author Marcus Boerger
- * @version 1.0
+ * @version 1.1
*
*/
class FindFile extends FilterIterator
{
- protected $file;
+ /** @internal filename to find */
+ private $file;
+ /** Construct from path and filename
+ *
+ * @param $path the directory to search in
+ * If path contains ';' then this parameter is split and every
+ * part of it is used as separate directory.
+ * @param $file the name of the files to search fro
+ */
function __construct($path, $file)
{
$this->file = $file;
}
}
+ /** @return whether the current file matches the given filename
+ */
function accept()
{
return !strcmp($this->current(), $this->file);
}
+
+ /** @return the filename to search for.
+ * @note This may be overloaded and contain a regular expression for an
+ * extended class that uses regular expressions to search.
+ */
+ function getSearch()
+ {
+ return $this->file;
+ }
}
?>
\ No newline at end of file
exit(1);
}
-class RegexFindFile extends FindFile
-{
- function accept()
- {
- return preg_match($this->file, $this->current());
- }
-}
+if (!class_exists("RegexFindFile")) require_once("regexfindfile.inc");
-foreach(new RegexFindFile($argv[1], $argv[2]) as $file) {
+foreach(new RegexFindFile($argv[1], $argv[2]) as $file)
+{
echo $file->getPathname()."\n";
}
<?php
-/**
+/** \ingroup Examples
* @brief An infinite Iterator
* @author Marcus Boerger
* @version 1.0
*/
class InfiniteIterator implements Iterator
{
+ /** @internal
+ * The inner Iterator. */
private $it;
+ /** Construct from another Iterator.
+ * @param $it the inner Iterator.
+ */
function __construct(Iterator $it)
{
$this->it = $it;
}
+ /** @return the inner iterator
+ */
function getInnerIterator()
{
return $this->it;
}
+ /** Rewind the inner iterator.
+ * @return void
+ */
function rewind()
{
$this->it->rewind();
}
+ /** @return whether the current element is valid
+ */
function valid()
{
return $this->it->valid();
}
+ /** @return the current value
+ */
function current()
{
return $this->it->current();
}
+ /** @return the current key
+ */
function key()
{
return $this->it->key();
}
+ /** Move the inner Iterator forward to its next element or rewind it.
+ * @return void
+ */
function next()
{
$this->it->next();
$this->it->rewind();
}
}
+
+ /** Aggregates the inner iterator
+ */
+ function __call($func, $params)
+ {
+ return call_user_func_array(array($this->getSubIterator(), $func), $params);
+ }
}
?>
\ No newline at end of file
exit(1);
}
-require_once("dba_reader.inc");
-require_once("IniGroups.inc");
+if (!class_exists("KeyFilter")) require_once("keyfilter.inc");
+if (!class_exists("IniGroups")) require_once("inigroups.inc");
$it = new IniGroups($argv[1]);
if ($argc>2) {
<?php
-require_once("KeyFilter.inc");
+if (!class_exists("KeyFilter")) require_once("keyfilter.inc");
+if (!class_exists("DbaReader")) require_once("dbareader.inc");
-/**
+/** \ingroup Examples
* @brief Class to iterate all groups within an ini file.
* @author Marcus Boerger
- * @version 1.0
+ * @version 1.1
*
* Using this class you can iterator over all groups of a ini file.
*
* @param file Ini file to open.
*/
function __construct($file) {
- parent::__construct(new dba_reader($file, 'inifile'), '^\[.*\]$');
+ parent::__construct(new DbaReader($file, 'inifile'), '^\[.*\]$');
}
/**
--- /dev/null
+<?php
+
+/** \ingroup Examples
+ * @brief Regular expression filter for string iterators
+ * @author Marcus Boerger
+ * @version 1.1
+ *
+ * Instances of this class act as a filter around iterators whose elements
+ * are strings. In other words you can put an iterator into the constructor
+ * and the instance will only return elements which match the given regular
+ * expression.
+ */
+class KeyFilter extends FilterIterator
+{
+ /** @internal regular exoression used as filter */
+ private $regex;
+
+ /**
+ * Constructs a filter around an iterator whose elemnts are strings.
+ * If the given iterator is of type spl_sequence then its rewind()
+ * method is called.
+ *
+ * @param it Object that implements at least spl_forward
+ * @param regex Regular expression used as a filter.
+ */
+ function __construct(Iterator $it, $regex)
+ {
+ parent::__construct($it);
+ $this->regex = $regex;
+ }
+
+ /** \return whether the current key mathes the regular expression
+ */
+ function accept()
+ {
+ return ereg($this->regex, $this->getInnerIterator()->key());
+ }
+
+ /** @return regular expression used as filter
+ */
+ function getRegex()
+ {
+ return $this->regex;
+ }
+
+ /**
+ * hidden __clone
+ */
+ protected function __clone()
+ {
+ // disallow clone
+ }
+}
+
+?>
\ No newline at end of file
<?php
+/** @ingroup Examples
+ * @brief Iterator to search for a specific element
+ * @author Marcus Boerger
+ * @version 1.0
+ *
+ * This extended FilterIterator stops after finding the first acceptable
+ * value.
+ */
abstract class SearchIterator extends FilterIterator
{
+ /** @internal whether an entry was found already */
private $done = false;
- function rewind() {
+ /** Rewind and reset so that it once again searches.
+ * @return void
+ */
+ function rewind()
+ {
parent::rewind();
$this->done = false;
}
- function valid() {
+ /** @return whether the current element is valid
+ * which can only happen once per iteration.
+ */
+ function valid()
+ {
return !$this->done && parent::valid();
}
- function next() {
+ /** Do not move forward but instead mark as finished.
+ * @return void
+ */
+ function next()
+ {
$this->done = true;
}
+
+ /** Aggregates the inner iterator
+ */
+ function __call($func, $params)
+ {
+ return call_user_func_array(array($this->getSubIterator(), $func), $params);
+ }
}
?>
\ No newline at end of file
*
* Simply specify the path to tree with parameter <path>.
*
- * (c) Marcus Boerger, 2003
+ * (c) Marcus Boerger, 2003 - 2004
*/
// The following line only operates on classes which are converted to c already.
exit(1);
}
+if (!class_exists("DirectoryTreeIterator")) require_once("directorytreeiterator.inc");
+
echo $argv[1]."\n";
-foreach(new DirectoryGraphIterator($argv[1]) as $file) {
+foreach(new DirectoryTreeIterator($argv[1]) as $file)
+{
echo $file . "\n";
}