3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Text;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
7 use Org\Mxchange\CoreFramework\Filesystem\Pointer\OutputPointer;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
12 use \InvalidArgumentException;
16 * A class for writing files
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 <<<<<<< HEAD:framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Create a file pointer based on the given file. The file will also
56 * @param $fileInstance An instance of a SplFileInfo class
57 * @param $mode The output mode ('w', 'a' are valid)
58 * @throws InvalidArgumentException If mode is empty
59 * @throws FileIoException If fopen() returns not a file resource
62 public static final function createFrameworkTextFileOutputPointer (SplFileInfo $fileInstance, $mode) {
63 // Some pre-sanity checks...
66 throw new InvalidArgumentException('Parameter "mode" is empty');
69 // Try to open a handler
70 $fileObject = $fileInstance->openFile($mode);
73 if ((is_null($fileObject)) || ($fileObject === false)) {
74 // Something bad happend
75 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
78 // Create new instance
79 $pointerInstance = new FrameworkTextFileOutputPointer();
82 $pointerInstance->setFileObject($fileObject);
84 // Return the instance
85 return $pointerInstance;
89 * Write data to a file pointer
91 * @param $dataStream The data stream we shall write to the file
92 * @return mixed Number of writes bytes or false on error
93 * @throws NullPointerException If the file pointer instance is not set by setPointer()
94 * @throws LogicException If there is no object being set
96 public function writeToFile ($dataStream) {
97 if (is_null($this->getFileObject())) {
98 // Pointer not initialized
99 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
100 } elseif (!is_object($this->getFileObject())) {
101 // Pointer is not a valid object!
102 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
105 // Write data to the file pointer and return written bytes
106 return $this->getFileObject()->fwrite($dataStream);
110 * Analyzes entries in index file. This will count all found (and valid)
111 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
112 * only gaps are found, the file is considered as "virgin" (no entries).
115 * @throws UnsupportedOperationException If this method is called
117 public function analyzeFile () {
118 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
122 * Writes at given position by seeking to it.
124 * @param $seekPosition Seek position in file
125 * @param $data Data to be written
126 * @return mixed Number of writes bytes or false on error
127 * @throws UnsupportedOperationException If this method is called
129 public function writeAtPosition ($seedPosition, $data) {
130 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
134 * Advances to next "block" of bytes
137 * @throws UnsupportedOperationException If this method is called
139 public function next () {
140 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
144 * Checks wether the current entry is valid (not at the end of the file).
145 * This method will return true if an emptied (nulled) entry has been found.
147 * @return $isValid Whether the next entry is valid
148 * @throws UnsupportedOperationException If this method is called
150 public function valid () {
151 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
155 * Gets current seek position ("key").
157 * @return $key Current key in iteration
158 * @throws UnsupportedOperationException If this method is called
160 public function key () {
161 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);