* 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]
--- /dev/null
+<?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]
+?>
--- /dev/null
+<?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]
+?>
* 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
*
* 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.
* 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
*
* 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
*
* 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
*
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]
* 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) {
*
* @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...
*/
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 {
} // 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],
$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,
$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;
* @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) {