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\FileIoException;
8 use Org\Mxchange\CoreFramework\Filesystem\Pointer\OutputPointer;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
13 use \InvalidArgumentException;
18 * A class for writing files
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
41 * Protected constructor
45 private function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Create a file pointer based on the given file. The file will also
54 * @param $fileInstance An instance of a SplFileInfo class
55 * @param $mode The output mode ('w', 'a' are valid)
56 * @throws InvalidArgumentException If mode is empty
57 * @throws FileIoException If fopen() returns not a file resource
60 public static final function createFrameworkTextFileOutputPointer (SplFileInfo $fileInstance, string $mode) {
61 // Some pre-sanity checks...
62 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-OUTPUT-POINTER: fileInstance[%s]=%s,mode=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString(), $mode));
65 throw new InvalidArgumentException('Parameter "mode" is empty');
68 // Try to open a handler
69 $fileObject = $fileInstance->openFile($mode);
72 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-OUTPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
73 if (!($fileObject instanceof SplFileObject)) {
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 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
86 return $pointerInstance;
90 * Write data to a file pointer
92 * @param $dataStream The data stream we shall write to the file
93 * @return mixed Number of writes bytes or false on error
94 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
95 * @throws LogicException If there is no object being set
97 public function writeToFile (string $dataStream) {
99 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-OUTPUT-POINTER: dataStream=%s - CALLED!', $dataStream));
100 if (empty($dataStream)) {
102 throw new InvalidArgumentException('Parameter "dataStream" is empty');
103 } elseif (is_null($this->getFileObject())) {
104 // Pointer not initialized
105 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
106 } elseif (!is_object($this->getFileObject())) {
107 // Pointer is not a valid object!
108 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
111 // Write data to the file pointer and return written bytes
112 return $this->getFileObject()->fwrite($dataStream);
116 * Analyzes entries in index file. This will count all found (and valid)
117 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
118 * only gaps are found, the file is considered as "virgin" (no entries).
121 * @throws UnsupportedOperationException If this method is called
123 public function analyzeFileStructure () {
124 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
128 * Writes at given position by seeking to it.
130 * @param $seekPosition Seek position in file
131 * @param $data Data to be written
132 * @return mixed Number of writes bytes or false on error
133 * @throws UnsupportedOperationException If this method is called
135 public function writeAtPosition (int $seedPosition, string $data) {
136 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
140 * Advances to next "block" of bytes
143 * @throws UnsupportedOperationException If this method is called
145 public function next () {
146 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
150 * Checks wether the current entry is valid (not at the end of the file).
151 * This method will return true if an emptied (nulled) entry has been found.
153 * @return $isValid Whether the next entry is valid
154 * @throws UnsupportedOperationException If this method is called
156 public function valid () {
157 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
161 * Gets current seek position ("key").
163 * @return $key Current key in iteration
164 * @throws UnsupportedOperationException If this method is called
166 public function key () {
167 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);