~canonical-sysadmins/wordpress/4.6

« back to all changes in this revision

Viewing changes to wp-includes/pomo/streams.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Classes, which help reading streams of data from files.
 
4
 * Based on the classes from Danilo Segan <danilo@kvota.net>
 
5
 *
 
6
 * @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $
 
7
 * @package pomo
 
8
 * @subpackage streams
 
9
 */
 
10
 
 
11
if ( !class_exists( 'POMO_Reader' ) ):
 
12
class POMO_Reader {
 
13
 
 
14
        var $endian = 'little';
 
15
        var $_post = '';
 
16
 
 
17
        function POMO_Reader() {
 
18
                $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
 
19
                $this->_pos = 0;
 
20
        }
 
21
 
 
22
        /**
 
23
         * Sets the endianness of the file.
 
24
         *
 
25
         * @param $endian string 'big' or 'little'
 
26
         */
 
27
        function setEndian($endian) {
 
28
                $this->endian = $endian;
 
29
        }
 
30
 
 
31
        /**
 
32
         * Reads a 32bit Integer from the Stream
 
33
         *
 
34
         * @return mixed The integer, corresponding to the next 32 bits from
 
35
         *      the stream of false if there are not enough bytes or on error
 
36
         */
 
37
        function readint32() {
 
38
                $bytes = $this->read(4);
 
39
                if (4 != $this->strlen($bytes))
 
40
                        return false;
 
41
                $endian_letter = ('big' == $this->endian)? 'N' : 'V';
 
42
                $int = unpack($endian_letter, $bytes);
 
43
                return array_shift($int);
 
44
        }
 
45
 
 
46
        /**
 
47
         * Reads an array of 32-bit Integers from the Stream
 
48
         *
 
49
         * @param integer count How many elements should be read
 
50
         * @return mixed Array of integers or false if there isn't
 
51
         *      enough data or on error
 
52
         */
 
53
        function readint32array($count) {
 
54
                $bytes = $this->read(4 * $count);
 
55
                if (4*$count != $this->strlen($bytes))
 
56
                        return false;
 
57
                $endian_letter = ('big' == $this->endian)? 'N' : 'V';
 
58
                return unpack($endian_letter.$count, $bytes);
 
59
        }
 
60
 
 
61
 
 
62
        function substr($string, $start, $length) {
 
63
                if ($this->is_overloaded) {
 
64
                        return mb_substr($string, $start, $length, 'ascii');
 
65
                } else {
 
66
                        return substr($string, $start, $length);
 
67
                }
 
68
        }
 
69
 
 
70
        function strlen($string) {
 
71
                if ($this->is_overloaded) {
 
72
                        return mb_strlen($string, 'ascii');
 
73
                } else {
 
74
                        return strlen($string);
 
75
                }
 
76
        }
 
77
 
 
78
        function str_split($string, $chunk_size) {
 
79
                if (!function_exists('str_split')) {
 
80
                        $length = $this->strlen($string);
 
81
                        $out = array();
 
82
                        for ($i = 0; $i < $length; $i += $chunk_size)
 
83
                                $out[] = $this->substr($string, $i, $chunk_size);
 
84
                        return $out;
 
85
                } else {
 
86
                        return str_split( $string, $chunk_size );
 
87
                }
 
88
        }
 
89
 
 
90
 
 
91
        function pos() {
 
92
                return $this->_pos;
 
93
        }
 
94
 
 
95
        function is_resource() {
 
96
                return true;
 
97
        }
 
98
 
 
99
        function close() {
 
100
                return true;
 
101
        }
 
102
}
 
103
endif;
 
104
 
 
105
if ( !class_exists( 'POMO_FileReader' ) ):
 
106
class POMO_FileReader extends POMO_Reader {
 
107
        function POMO_FileReader($filename) {
 
108
                parent::POMO_Reader();
 
109
                $this->_f = fopen($filename, 'rb');
 
110
        }
 
111
 
 
112
        function read($bytes) {
 
113
                return fread($this->_f, $bytes);
 
114
        }
 
115
 
 
116
        function seekto($pos) {
 
117
                if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
 
118
                        return false;
 
119
                }
 
120
                $this->_pos = $pos;
 
121
                return true;
 
122
        }
 
123
 
 
124
        function is_resource() {
 
125
                return is_resource($this->_f);
 
126
        }
 
127
 
 
128
        function feof() {
 
129
                return feof($this->_f);
 
130
        }
 
131
 
 
132
        function close() {
 
133
                return fclose($this->_f);
 
134
        }
 
135
 
 
136
        function read_all() {
 
137
                $all = '';
 
138
                while ( !$this->feof() )
 
139
                        $all .= $this->read(4096);
 
140
                return $all;
 
141
        }
 
142
}
 
143
endif;
 
144
 
 
145
if ( !class_exists( 'POMO_StringReader' ) ):
 
146
/**
 
147
 * Provides file-like methods for manipulating a string instead
 
148
 * of a physical file.
 
149
 */
 
150
class POMO_StringReader extends POMO_Reader {
 
151
 
 
152
        var $_str = '';
 
153
 
 
154
        function POMO_StringReader($str = '') {
 
155
                parent::POMO_Reader();
 
156
                $this->_str = $str;
 
157
                $this->_pos = 0;
 
158
        }
 
159
 
 
160
 
 
161
        function read($bytes) {
 
162
                $data = $this->substr($this->_str, $this->_pos, $bytes);
 
163
                $this->_pos += $bytes;
 
164
                if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
 
165
                return $data;
 
166
        }
 
167
 
 
168
        function seekto($pos) {
 
169
                $this->_pos = $pos;
 
170
                if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
 
171
                return $this->_pos;
 
172
        }
 
173
 
 
174
        function length() {
 
175
                return $this->strlen($this->_str);
 
176
        }
 
177
 
 
178
        function read_all() {
 
179
                return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
 
180
        }
 
181
 
 
182
}
 
183
endif;
 
184
 
 
185
if ( !class_exists( 'POMO_CachedFileReader' ) ):
 
186
/**
 
187
 * Reads the contents of the file in the beginning.
 
188
 */
 
189
class POMO_CachedFileReader extends POMO_StringReader {
 
190
        function POMO_CachedFileReader($filename) {
 
191
                parent::POMO_StringReader();
 
192
                $this->_str = file_get_contents($filename);
 
193
                if (false === $this->_str)
 
194
                        return false;
 
195
                $this->_pos = 0;
 
196
        }
 
197
}
 
198
endif;
 
199
 
 
200
if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
 
201
/**
 
202
 * Reads the contents of the file in the beginning.
 
203
 */
 
204
class POMO_CachedIntFileReader extends POMO_CachedFileReader {
 
205
        function POMO_CachedIntFileReader($filename) {
 
206
                parent::POMO_CachedFileReader($filename);
 
207
        }
 
208
}
 
209
endif;
 
 
b'\\ No newline at end of file'