0309ed16 |
1 | <?php |
4b4abf93 |
2 | |
0309ed16 |
3 | /** |
4 | * Copyright (c) 2003 Danilo Segan <danilo@kvota.net>. |
5 | * |
6 | * This file is part of PHP-gettext. |
7 | * |
8 | * PHP-gettext is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * PHP-gettext is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with PHP-gettext; if not, write to the Free Software |
435bd78d |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
21 | * MA 02110-1301, USA |
0309ed16 |
22 | * |
33aab559 |
23 | * @copyright 2004-2024 The SquirrelMail Project Team |
4b4abf93 |
24 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
25 | * @version $Id$ |
0309ed16 |
26 | * @package squirrelmail |
27 | * @subpackage i18n |
28 | */ |
29 | |
30 | /** |
31 | * Class that is used to read .mo files. |
32 | * @package squirrelmail |
33 | * @subpackage i18n |
34 | */ |
35 | class FileReader { |
36 | /** |
37 | * Current position in file |
38 | * @var integer |
39 | */ |
40 | var $_pos; |
41 | /** |
42 | * File descriptor |
43 | * @var resource |
44 | */ |
45 | var $_fd; |
46 | /** |
47 | * File size |
48 | * @var integer |
49 | */ |
50 | var $_length; |
51 | /** |
52 | * contains error codes |
53 | * |
54 | * 2 = File doesn't exist |
55 | * 3 = Can't read file |
56 | * @var integer |
57 | */ |
58 | var $error=0; |
59 | |
60 | /** |
3741e45c |
61 | * Constructor (PHP5 style, required in some future version of PHP) |
0309ed16 |
62 | * reads translation file and fills translation input object properties |
63 | * @param string $filename path to file |
64 | * @return boolean false there is a problem with $filename |
3741e45c |
65 | TODO: Constructors should not return anything. |
0309ed16 |
66 | */ |
3741e45c |
67 | function __construct($filename) { |
392de6fb |
68 | // disable stat warnings for unreadable directories |
69 | if (@file_exists($filename)) { |
0309ed16 |
70 | |
71 | $this->_length=filesize($filename); |
0309ed16 |
72 | $this->_pos = 0; |
73 | $this->_fd = fopen($filename,'rb'); |
74 | if (!$this->_fd) { |
75 | $this->error = 3; // Cannot read file, probably permissions |
76 | return false; |
77 | } |
78 | } else { |
79 | $this->error = 2; // File doesn't exist |
80 | return false; |
81 | } |
82 | } |
83 | |
3741e45c |
84 | /** |
85 | * Constructor (PHP4 style, kept for compatibility reasons) |
86 | * reads translation file and fills translation input object properties |
87 | * @param string $filename path to file |
88 | * @return boolean false there is a problem with $filename |
89 | TODO: Constructors should not return anything. |
90 | */ |
91 | function FileReader($filename) { |
92 | return self::__construct($filename); |
93 | } |
94 | |
0309ed16 |
95 | /** |
96 | * reads data from current position |
97 | * @param integer $bytes number of bytes to read |
98 | * @return string read data |
99 | */ |
100 | function read($bytes) { |
101 | fseek($this->_fd, $this->_pos); |
102 | $data = fread($this->_fd, $bytes); |
103 | $this->_pos = ftell($this->_fd); |
104 | |
105 | return $data; |
106 | } |
107 | |
108 | /** |
109 | * Moves to defined position in a file |
110 | * @param integer $pos position |
111 | * @return integer current position |
112 | */ |
113 | function seekto($pos) { |
114 | fseek($this->_fd, $pos); |
115 | $this->_pos = ftell($this->_fd); |
116 | return $this->_pos; |
117 | } |
118 | |
119 | /** |
120 | * return current position |
121 | * @return integer current position |
122 | */ |
123 | function currentpos() { |
124 | return $this->_pos; |
125 | } |
126 | |
127 | /** |
128 | * return file length |
129 | * @return integer file length |
130 | */ |
131 | function length() { |
132 | return $this->_length; |
133 | } |
134 | |
135 | /** |
136 | * close translation file |
137 | */ |
138 | function close() { |
139 | fclose($this->_fd); |
140 | } |
141 | } |