]> git.mxchange.org Git - core.git/commitdiff
Continued with file-based stacks and file i/o:
authorRoland Haeder <roland@mxchange.org>
Fri, 16 May 2014 22:34:12 +0000 (00:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 16 May 2014 22:34:12 +0000 (00:34 +0200)
- 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 <roland@mxchange.org>
13 files changed:
inc/classes/interfaces/io/class_Streamable.php
inc/classes/interfaces/io/class_StreamableInput.php [new file with mode: 0644]
inc/classes/interfaces/io/class_StreamableOutput.php [new file with mode: 0644]
inc/classes/interfaces/io/file/class_FileInputStreamer.php
inc/classes/interfaces/io/file/class_FileOutputStreamer.php
inc/classes/interfaces/io/output/class_OutputStreamer.php
inc/classes/interfaces/io/pointer/class_InputPointer.php
inc/classes/interfaces/io/pointer/class_OutputPointer.php
inc/classes/main/file_directories/class_BaseFileIo.php
inc/classes/main/file_directories/input/class_FrameworkFileInputPointer.php
inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php
inc/classes/main/file_directories/io_handler/class_FileIoStream.php
inc/classes/main/file_directories/output/class_FrameworkFileOutputPointer.php

index 68165ce6c81c1a198e9764a0468412b9e7f55b1a..7503dba4f03fad87333647e4b944ea39f1d825b2 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 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 (file)
index 0000000..5ade97b
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for input streams
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..8d97810
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for output streams
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+interface StreamableOutput extends Streamable {
+}
+
+// [EOF]
+?>
index b5bafdb8532751df0009af9e2fa367b52684b40f..e094eecd80a29157de6225abd64abb0b600719f7 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface FileInputStreamer extends Streamable {
+interface FileInputStreamer extends StreamableInput {
        /**
         * Reads from a local or remote file
         *
index f85a4049904b38d7088b236f5c84e0ddd92d1727..106e01ce4513b127baba6c63964ee600ac2774ca 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface FileOutputStreamer extends Streamable {
+interface FileOutputStreamer extends StreamableOutput {
        /**
         * Saves streamed (that are mostly serialized objects) data to files or
         * external servers.
index 70a08177d90af21cc552e746816e1b6e75f059df..3c13e9ac92113065138efce74ee28ad9ea4edc27 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface OutputStreamer extends Streamable {
+interface OutputStreamer extends StreamableOutput {
        /**
         * Output the code
         *
index d312dc9f2cee2674eba8316863c730ea71a0cf5a..1d67cd24599fb3ba1b66e2bad501fdf510adb6eb 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface InputPointer extends Streamable {
+interface InputPointer extends StreamableInput {
        /**
         * Read data a file pointer
         *
index 8d367ebedd260bc170ebe4f3fb29443e45c5d93b..7d8300eebf50f0385207e42e898c2771315e4bc4 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface OutputPointer extends Streamable {
+interface OutputPointer extends StreamableOutput {
        /**
         * Write data to a file pointer
         *
index a18a8529df11a831ffd6b332cb0fe3e2e7cff126..8cc8f60b11cc3f91949c83e996adf41482f291e4 100644 (file)
@@ -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]
index bd14d9dbb6fe7180073384cced28ba3b6dff8586..e216c4b2d6bcfe28a6195aaf63b6d0fbe97b7fb7 100644 (file)
@@ -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) {
index 88c565cc20c8305c40ea4d83f98305f624254cdf..078c206be29400b5aee821748067a73f2827630d 100644 (file)
@@ -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...
index 27ceab32a4e7a3a0d981af5efee1e9f8e564989f..956ca34a6091e09710eb78fffc41751fbcbd5045 100644 (file)
@@ -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;
index a3da18563a2fb3ec4d3d1c71a045a677e87fd206..94db1bcdc501290c863fa06698569d5bc431edc0 100644 (file)
@@ -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) {