From: Roland Haeder Date: Fri, 16 May 2014 22:34:12 +0000 (+0200) Subject: Continued with file-based stacks and file i/o: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=f18669b3ece63a283957eb0114967d322648094a Continued with file-based stacks and file i/o: - added new interfaces (with some methods) - added those methods to BaseFileIo so all implementations can have it. Still I need to find out if really all can e.g. seek. - Used those new interfaces accordingly Signed-off-by: Roland Häder --- diff --git a/inc/classes/interfaces/io/class_Streamable.php b/inc/classes/interfaces/io/class_Streamable.php index 68165ce6..7503dba4 100644 --- a/inc/classes/interfaces/io/class_Streamable.php +++ b/inc/classes/interfaces/io/class_Streamable.php @@ -22,6 +22,28 @@ * along with this program. If not, see . */ interface Streamable extends FrameworkInterface { + /** + * "Getter" for seek position + * + * @return $seekPosition Current seek position + */ + function getPosition (); + + /** + * Seek to given offset (default) or other possibilities as fseek() gives. + * + * @param $offset Offset to seek to (or used as "base" for other seeks) + * @param $whence Added to offset (default: only use offset to seek to) + * @return $status Status of file seek: 0 = success, -1 = failed + */ + function seek ($offset, $whence = SEEK_SET); + + /** + * Size of file stack + * + * @return $size Size (in bytes) of file + */ + function size (); } // [EOF] diff --git a/inc/classes/interfaces/io/class_StreamableInput.php b/inc/classes/interfaces/io/class_StreamableInput.php new file mode 100644 index 00000000..5ade97b7 --- /dev/null +++ b/inc/classes/interfaces/io/class_StreamableInput.php @@ -0,0 +1,28 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface StreamableInput extends Streamable { +} + +// [EOF] +?> diff --git a/inc/classes/interfaces/io/class_StreamableOutput.php b/inc/classes/interfaces/io/class_StreamableOutput.php new file mode 100644 index 00000000..8d978104 --- /dev/null +++ b/inc/classes/interfaces/io/class_StreamableOutput.php @@ -0,0 +1,28 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface StreamableOutput extends Streamable { +} + +// [EOF] +?> diff --git a/inc/classes/interfaces/io/file/class_FileInputStreamer.php b/inc/classes/interfaces/io/file/class_FileInputStreamer.php index b5bafdb8..e094eecd 100644 --- a/inc/classes/interfaces/io/file/class_FileInputStreamer.php +++ b/inc/classes/interfaces/io/file/class_FileInputStreamer.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface FileInputStreamer extends Streamable { +interface FileInputStreamer extends StreamableInput { /** * Reads from a local or remote file * diff --git a/inc/classes/interfaces/io/file/class_FileOutputStreamer.php b/inc/classes/interfaces/io/file/class_FileOutputStreamer.php index f85a4049..106e01ce 100644 --- a/inc/classes/interfaces/io/file/class_FileOutputStreamer.php +++ b/inc/classes/interfaces/io/file/class_FileOutputStreamer.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface FileOutputStreamer extends Streamable { +interface FileOutputStreamer extends StreamableOutput { /** * Saves streamed (that are mostly serialized objects) data to files or * external servers. diff --git a/inc/classes/interfaces/io/output/class_OutputStreamer.php b/inc/classes/interfaces/io/output/class_OutputStreamer.php index 70a08177..3c13e9ac 100644 --- a/inc/classes/interfaces/io/output/class_OutputStreamer.php +++ b/inc/classes/interfaces/io/output/class_OutputStreamer.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface OutputStreamer extends Streamable { +interface OutputStreamer extends StreamableOutput { /** * Output the code * diff --git a/inc/classes/interfaces/io/pointer/class_InputPointer.php b/inc/classes/interfaces/io/pointer/class_InputPointer.php index d312dc9f..1d67cd24 100644 --- a/inc/classes/interfaces/io/pointer/class_InputPointer.php +++ b/inc/classes/interfaces/io/pointer/class_InputPointer.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface InputPointer extends Streamable { +interface InputPointer extends StreamableInput { /** * Read data a file pointer * diff --git a/inc/classes/interfaces/io/pointer/class_OutputPointer.php b/inc/classes/interfaces/io/pointer/class_OutputPointer.php index 8d367ebe..7d8300ee 100644 --- a/inc/classes/interfaces/io/pointer/class_OutputPointer.php +++ b/inc/classes/interfaces/io/pointer/class_OutputPointer.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface OutputPointer extends Streamable { +interface OutputPointer extends StreamableOutput { /** * Write data to a file pointer * diff --git a/inc/classes/main/file_directories/class_BaseFileIo.php b/inc/classes/main/file_directories/class_BaseFileIo.php index a18a8529..8cc8f60b 100644 --- a/inc/classes/main/file_directories/class_BaseFileIo.php +++ b/inc/classes/main/file_directories/class_BaseFileIo.php @@ -122,6 +122,52 @@ class BaseFileIo extends BaseFrameworkSystem { public final function getFileName () { return $this->fileName; } + + /** + * "Getter" for seek position + * + * @return $seekPosition Current seek position + */ + public final function getPosition () { + return ftell($this->getPointer()); + } + + /** + * Seek to given offset (default) or other possibilities as fseek() gives. + * + * @param $offset Offset to seek to (or used as "base" for other seeks) + * @param $whence Added to offset (default: only use offset to seek to) + * @return $status Status of file seek: 0 = success, -1 = failed + */ + public function seek ($offset, $whence = SEEK_SET) { + // Seek to position + $status = fseek($this->getPointer(), $offset, $whence); + + // Return status + return $status; + } + + /** + * Size of this file + * + * @return $size Size (in bytes) of file + */ + public function size () { + // Get current seek position + $seekPosition = $this->getPosition(); + + // Seek to end + $this->seek(0, SEEK_END); + + // Get position again (which is the end of the file) + $size = $this->getPosition(); + + // Reset seek position to old + $this->seek($seekPosition); + + // Return size + return $size; + } } // [EOF] diff --git a/inc/classes/main/file_directories/input/class_FrameworkFileInputPointer.php b/inc/classes/main/file_directories/input/class_FrameworkFileInputPointer.php index bd14d9db..e216c4b2 100644 --- a/inc/classes/main/file_directories/input/class_FrameworkFileInputPointer.php +++ b/inc/classes/main/file_directories/input/class_FrameworkFileInputPointer.php @@ -36,9 +36,9 @@ class FrameworkFileInputPointer extends BaseFileIo implements InputPointer { * Create a file pointer based on the given file. The file will also * be verified here. * - * @param $fileName The file name we shall pass to fopen() + * @param $fileName The file name we shall pass to fopen() * @throws FileIsEmptyException If the provided file name is empty. - * @throws FileIoException If fopen() returns not a file resource + * @throws FileIoException If fopen() returns not a file resource * @return void */ public static final function createFrameworkFileInputPointer ($fileName) { diff --git a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php index 88c565cc..078c206b 100644 --- a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -38,10 +38,10 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * * @param $fileName The file name we shall pass to fopen() * @return void - * @throws FileIsEmptyException If the given file name is NULL or empty + * @throws FileIsEmptyException If the given file name is NULL or empty * @throws FileReadProtectedException If PHP cannot read an existing file * @throws FileWriteProtectedException If PHP cannot write an existing file - * @throws FileIoException If fopen() returns not a file resource + * @throws FileIoException If fopen() returns not a file resource */ public static final function createFrameworkFileInputOutputPointer ($fileName) { // Some pre-sanity checks... diff --git a/inc/classes/main/file_directories/io_handler/class_FileIoStream.php b/inc/classes/main/file_directories/io_handler/class_FileIoStream.php index 27ceab32..956ca34a 100644 --- a/inc/classes/main/file_directories/io_handler/class_FileIoStream.php +++ b/inc/classes/main/file_directories/io_handler/class_FileIoStream.php @@ -75,7 +75,9 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil */ public final function saveFile ($fileName, array $dataArray) { // Try it five times - $dirName = ''; $fileInstance = NULL; + $dirName = ''; + $fileInstance = NULL; + for ($idx = 0; $idx < 5; $idx++) { // Get a file output pointer try { @@ -93,7 +95,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil } // END - for // Write a header information for validation purposes - $fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n", + $fileInstance->writeToFile(sprintf('%s%s%s%s%s%s%s%s%s' . PHP_EOL, self::FILE_IO_FILE_HEADER_ID, self::FILE_IO_SEPARATOR, $dataArray[0], @@ -115,7 +117,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $line = substr($b64Stream, $idx, 50); // Save it to the stream - $fileInstance->writeToFile(sprintf("%s%s%s%s%s\n", + $fileInstance->writeToFile(sprintf('%s%s%s%s%s' . PHP_EOL, self::FILE_IO_DATA_BLOCK_ID, self::FILE_IO_SEPARATOR, $line, @@ -155,7 +157,9 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil $inputBuffer .= $readRawLine; // Break infinite loop maybe caused by the input handler - if ($lastBuffer == $inputBuffer) break; + if ($lastBuffer == $inputBuffer) { + break; + } // END - if // Remember last read line for avoiding possible infinite loops $lastBuffer = $inputBuffer; diff --git a/inc/classes/main/file_directories/output/class_FrameworkFileOutputPointer.php b/inc/classes/main/file_directories/output/class_FrameworkFileOutputPointer.php index a3da1856..94db1bcd 100644 --- a/inc/classes/main/file_directories/output/class_FrameworkFileOutputPointer.php +++ b/inc/classes/main/file_directories/output/class_FrameworkFileOutputPointer.php @@ -39,7 +39,7 @@ class FrameworkFileOutputPointer extends BaseFileIo implements OutputPointer { * @param $fileName The file name we shall pass to fopen() * @param $mode The output mode ('w', 'a' are valid) * @throws FileIsEmptyException If the provided file name is empty. - * @throws FileIoException If fopen() returns not a file resource + * @throws FileIoException If fopen() returns not a file resource * @return void */ public static final function createFrameworkFileOutputPointer ($fileName, $mode) {