]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php
Continued:
[core.git] / framework / main / classes / file_directories / input / text / class_FrameworkTextFileInputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Input;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\BaseFileIo;
8 use Org\Mxchange\CoreFramework\Filesystem\FileIoException;
9 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
10 use Org\Mxchange\CoreFramework\Filesystem\FileReadProtectedException;
11 use Org\Mxchange\CoreFramework\Filesystem\Pointer\InputPointer;
12 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
13 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
14 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
15 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
16
17 // Import SPL stuff
18 use \SplFileInfo;
19 use \SplFileObject;
20
21 /**
22  * A class for reading text files
23  *
24  * @author              Roland Haeder <webmaster@shipsimu.org>
25  * @version             0.0.0
26  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
27  * @license             GNU GPL 3.0 or any newer version
28  * @link                http://www.shipsimu.org
29  *
30  * This program is free software: you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation, either version 3 of the License, or
33  * (at your option) any later version.
34  *
35  * This program is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program. If not, see <http://www.gnu.org/licenses/>.
42  */
43 class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         private function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Create a file pointer based on the given file. The file will also
56          * be verified here.
57          *
58          * @param       $fileName       The file name we shall pass to fopen()
59          * @throws      FileIoException                         If the file is not reachable
60          * @throws      FileReadProtectedException      If the file cannot be read from
61          * @return      void
62          */
63         public static final function createFrameworkTextFileInputPointer (SplFileInfo $fileInstance) {
64                 // Check parameter
65                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
66                 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
67                         // File cannot be reached
68                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
69                 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
70                         // File does not exist!
71                         throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
72                 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
73                         // File cannot be read from (but exists)
74                         throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
75                 }
76
77                 // Try to open a handler
78                 $fileObject = $fileInstance->openFile('r');
79
80                 // Is it valid?
81                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
82                 if (!($fileObject instanceof SplFileObject)) {
83                         // Something bad happend
84                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
85                 }
86
87                 // Create new instance
88                 $pointerInstance = new FrameworkTextFileInputPointer();
89
90                 // Set file pointer and file name
91                 $pointerInstance->setFileObject($fileObject);
92
93                 // Return the instance
94                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
95                 return $pointerInstance;
96         }
97
98         /**
99          * Read data a file pointer
100          *
101          * @return      $data   Read data from file
102          */
103         public function readFromFile () {
104                 // Read 1024 Byte data from the file pointer and return it
105                 return $this->read(1024);
106         }
107
108         /**
109          * Reads a line, maximum 4096 Bytes from current file pointer
110          *
111          * @return      $data   Read data from file
112          */
113         public function readLine () {
114                 // Read whole line from the file pointer and return it
115                 return $this->read();
116         }
117
118         /**
119          * Reads given amount of bytes from file.
120          *
121          * @param       $bytes  Amount of bytes to read or whole line (only text files)
122          * @return      $data   Data read from file
123          * @throws      OutOfBoundsException    If the position is not seekable
124          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
125          * @throws      LogicException  If $fileObject is not an object
126          */
127         public function read (int $bytes = 0) {
128                 // Some sanity checks
129                 if ($bytes < 0) {
130                         // Cannot be below zero
131                         throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
132                 } elseif (is_null($this->getFileObject())) {
133                         // Pointer not initialized
134                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
135                 } elseif (!is_object($this->getFileObject())) {
136                         // Pointer is not a valid resource!
137                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
138                 }
139
140                 // Is $bytes set?
141                 if ($bytes > 0) {
142                         // Try to read given characters
143                         $data = $this->getFileObject()->fread($bytes);
144                 } else {
145                         // Try to read whole line
146                         $data = $this->getFileObject()->fgets();
147                 }
148
149                 // Then return it
150                 return $data;
151         }
152
153         /**
154          * Analyzes entries in index file. This will count all found (and valid)
155          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
156          * only gaps are found, the file is considered as "virgin" (no entries).
157          *
158          * @return      void
159          * @throws      UnsupportedOperationException   If this method is called
160          */
161         public function analyzeFileStructure () {
162                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
163         }
164
165         /**
166          * Advances to next "block" of bytes
167          *
168          * @return      void
169          * @throws      UnsupportedOperationException   If this method is called
170          */
171         public function next () {
172                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
173         }
174
175         /**
176          * Checks wether the current entry is valid (not at the end of the file).
177          * This method will return true if an emptied (nulled) entry has been found.
178          *
179          * @return      $isValid        Whether the next entry is valid
180          * @throws      UnsupportedOperationException   If this method is called
181          */
182         public function valid () {
183                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
184         }
185
186         /**
187          * Gets current seek position ("key").
188          *
189          * @return      $key    Current key in iteration
190          * @throws      UnsupportedOperationException   If this method is called
191          */
192         public function key () {
193                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
194         }
195
196 }