X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FileIoStream.php;h=457c51f6d155782c8fddb55320fd2616d1bdf736;hb=b9c18d6c24e3be4393bf41005aa4e428a0ea3218;hp=7e257be47de862745dc7ce7ca2d6d3f1e557183e;hpb=c6d73b0e3246efc824cb98338d4be7ee5bc9f308;p=core.git diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/class_FileIoStream.php index 7e257be4..457c51f6 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, 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 * @@ -22,6 +22,26 @@ * 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 */ @@ -49,16 +69,17 @@ 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 { @@ -68,17 +89,23 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $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 +113,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 +144,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 +177,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 +213,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"; } }