*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, FileOutputStreamer {
+ /**
+ * File header indicator
+ */
+ private $fileHeader = '@head';
+
+ /**
+ * Data block indicator
+ */
+ private $dataBlock = '@data';
+
+ /**
+ * Seperator #1
+ */
+ private $chunker = ':';
+
+ /**
+ * Seperator #2
+ */
+ private $seperator = '^';
+
/**
* Protected constructor
*/
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
-
- // Clean-up a little
- $this->removeNumberFormaters();
- $this->removeSystemArray();
}
/**
}
/**
- * Saves data to a given local file
+ * Saves data to a given local file and create missing directory structures
*
- * @param $fileName The file name for the to be saved file
- * @param $dataArray The data we shall store to the file
+ * @param $fileName The file name for the to be saved file
+ * @param $dataArray The data we shall store to the file
* @return void
* @see FileOutputStreamer
+ * @todo This method needs heavy rewrite
*/
public final function saveFile ($fileName, $dataArray) {
// Try it five times
- $dirName = ""; $fileInstance = null;
+ $dirName = ''; $fileInstance = null;
for ($idx = 0; $idx < 5; $idx++) {
// Get a file output pointer
try {
$fileInstance = FrameworkFileOutputPointer::createFrameworkFileOutputPointer($fileName, 'w');
- } catch (FilePointerNotOpenedException $e) {
+ } catch (FileIoException $e) {
// Create missing directory
$dirName = dirname($fileName);
for ($idx2 = 0; $idx2 < (2 - $idx); $idx2++) {
$dirName = dirname($dirName);
- }
+ } // END - for
+
// Try to create it
@mkdir($dirName);
}
- }
+ } // END - for
// Write a header information for validation purposes
- $fileInstance->writeToFile(sprintf("@head^%s:%s:%s:%s\n",
+ $fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n",
+ $this->fileHeader,
+ $this->seperator,
$dataArray[0],
+ $this->chunker,
time(),
+ $this->chunker,
strlen($dataArray[1]),
+ $this->chunker,
md5($dataArray[1])
));
$b64Stream = base64_encode($dataArray[1]);
// write the data line by line
- $line = str_repeat(" ", 50); $idx = 0;
+ $line = str_repeat(' ', 50); $idx = 0;
while (strlen($line) == 50) {
// Get 50 chars or less
$line = substr($b64Stream, $idx, 50);
// Save it to the stream
- $fileInstance->writeToFile(sprintf("@data^%s:%s\n",
+ $fileInstance->writeToFile(sprintf("%s%s%s%s%s\n",
+ $this->dataBlock,
+ $this->seperator,
$line,
+ $this->chunker,
md5($line)
));
// Advance to the next 50-chars block
$idx += 50;
- }
+ } // END - while
// Close the file
$fileInstance->closeFile();
*/
public final function loadFileContents ($fqfn) {
// Initialize some variables and arrays
- $inputBuffer = "";
- $lastBuffer = "";
+ $inputBuffer = '';
+ $lastBuffer = '';
$header = array();
$data = array();
- $readData = ""; // This will contain our read data
+ $readData = ''; // This will contain our read data
// Get a file input handler
$fileInstance = FrameworkFileInputPointer::createFrameworkFileInputPointer($fqfn);
$rawLine = rtrim($rawLine);
// Analyze this line
- if (substr($rawLine, 0, 5) == "@head") {
+ if (substr($rawLine, 0, 5) == $this->fileHeader) {
// Header found, so let's extract it
- $header = explode("^", $rawLine);
+ $header = explode($this->seperator, $rawLine);
$header = trim($header[1]);
// Now we must convert it again into an array
- $header = explode(":", $header);
+ $header = explode($this->chunker, $header);
// Is the header (maybe) valid?
if (count($header) != 4) {
// Throw an exception
throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
}
- } elseif (substr($rawLine, 0, 5) == "@data") {
+ } elseif (substr($rawLine, 0, 5) == $this->dataBlock) {
// Is a data line!
- $data = explode("^", $rawLine);
+ $data = explode($this->seperator, $rawLine);
$data = $data[1];
// First element is the data, second the MD5 checksum
- $data = explode(":", $data);
+ $data = explode($this->chunker, $data);
// Validate the read line
if (count($data) == 2) {
$readData .= $data[0];
} else {
// Other raw lines than header/data tagged lines and re-add the new-line char
- $readData .= $rawLine."\n";
+ $readData .= $rawLine . "\n";
}
}