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