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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Create a file pointer based on the given file. The file will also
52 * @param $fileInstance An instance of a SplFileInfo class
53 * @param $mode The output mode ('w', 'a' are valid)
54 * @throws InvalidArgumentException If mode is empty
55 * @throws FileIoException If fopen() returns not a file resource
58 public static final function createFrameworkTextFileOutputPointer (SplFileInfo $fileInstance, $mode) {
59 // Some pre-sanity checks...
62 throw new InvalidArgumentException('Parameter "mode" is empty');
65 // Try to open a handler
66 $fileObject = $fileInstance->openFile($mode);
69 if ((is_null($fileObject)) || ($fileObject === false)) {
70 // Something bad happend
71 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
74 // Create new instance
75 $pointerInstance = new FrameworkTextFileOutputPointer();
78 $pointerInstance->setFileObject($fileObject);
80 // Return the instance
81 return $pointerInstance;
85 * Write data to a file pointer
87 * @param $dataStream The data stream we shall write to the file
88 * @return mixed Number of writes bytes or false on error
89 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
90 * @throws LogicException If there is no object being set
92 public function writeToFile (string $dataStream) {
93 if (is_null($this->getFileObject())) {
94 // Pointer not initialized
95 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96 } elseif (!is_object($this->getFileObject())) {
97 // Pointer is not a valid object!
98 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
101 // Write data to the file pointer and return written bytes
102 return $this->getFileObject()->fwrite($dataStream);
106 * Analyzes entries in index file. This will count all found (and valid)
107 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
108 * only gaps are found, the file is considered as "virgin" (no entries).
111 * @throws UnsupportedOperationException If this method is called
113 public function analyzeFileStructure () {
114 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
118 * Writes at given position by seeking to it.
120 * @param $seekPosition Seek position in file
121 * @param $data Data to be written
122 * @return mixed Number of writes bytes or false on error
123 * @throws UnsupportedOperationException If this method is called
125 public function writeAtPosition (int $seedPosition, string $data) {
126 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
130 * Advances to next "block" of bytes
133 * @throws UnsupportedOperationException If this method is called
135 public function next () {
136 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
140 * Checks wether the current entry is valid (not at the end of the file).
141 * This method will return true if an emptied (nulled) entry has been found.
143 * @return $isValid Whether the next entry is valid
144 * @throws UnsupportedOperationException If this method is called
146 public function valid () {
147 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
151 * Gets current seek position ("key").
153 * @return $key Current key in iteration
154 * @throws UnsupportedOperationException If this method is called
156 public function key () {
157 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);