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