X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FileIoStream.php;h=278d6d7d06fd71552af33da7bd650057685e18c8;hp=0eff6c5a9c3923e17e23f3f235f82a537e46efa6;hb=51caaa61ae7ee017abdfd116bbd8c438451315b2;hpb=361e6320e50a8bb1a3ccb675388b8042361669ae diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/class_FileIoStream.php index 0eff6c5a..278d6d7d 100644 --- a/inc/classes/main/io/class_FileIoStream.php +++ b/inc/classes/main/io/class_FileIoStream.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -22,16 +22,32 @@ * along with this program. If not, see . */ 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(); } /** @@ -49,36 +65,43 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil } /** - * 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]) )); @@ -86,20 +109,23 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $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(); @@ -114,11 +140,11 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil */ 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); @@ -147,26 +173,26 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $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) { @@ -183,7 +209,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $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"; } }