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