]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pomo/streams.php
Wordpress 2.8
[autoinstalls/wordpress.git] / wp-includes / pomo / streams.php
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 33 2009-02-16 09:33:39Z nbachiyski $
7  * @package pomo
8  * @subpackage streams
9  */
10
11
12 /**
13  * Provides file-like methods for manipulating a string instead
14  * of a physical file.
15  */
16 class POMO_StringReader {
17   var $_pos;
18   var $_str;
19
20   function POMO_StringReader($str = '') {
21     $this->_str = $str;
22     $this->_pos = 0;
23   }
24
25   function read($bytes) {
26     $data = substr($this->_str, $this->_pos, $bytes);
27     $this->_pos += $bytes;
28     if (strlen($this->_str)<$this->_pos)
29       $this->_pos = strlen($this->_str);
30
31     return $data;
32   }
33
34   function seekto($pos) {
35     $this->_pos = $pos;
36     if (strlen($this->_str)<$this->_pos)
37       $this->_pos = strlen($this->_str);
38     return $this->_pos;
39   }
40
41   function pos() {
42     return $this->_pos;
43   }
44
45   function length() {
46     return strlen($this->_str);
47   }
48
49 }
50
51 /**
52  * Reads the contents of the file in the beginning.
53  */
54 class POMO_CachedFileReader extends POMO_StringReader {
55         function POMO_CachedFileReader($filename) {
56                 $this->_str = file_get_contents($filename);
57                 if (false === $this->_str)
58                         return false;
59                 $this->pos = 0;
60         }
61 }
62
63 /**
64  * Allows reading integers from a file.
65  */
66 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
67
68         var $endian = 'little';
69
70         /**
71          * Opens a file and caches it.
72          *
73          * @param $filename string name of the file to be opened
74          * @param $endian string endianness of the words in the file, allowed
75          *      values are 'little' or 'big'. Default value is 'little'
76          */
77         function POMO_CachedIntFileReader($filename, $endian = 'little') {
78                 $this->endian = $endian;
79                 parent::POMO_CachedFileReader($filename);
80         }
81
82         /**
83          * Sets the endianness of the file.
84          *
85          * @param $endian string 'big' or 'little'
86          */
87         function setEndian($endian) {
88                 $this->endian = $endian;
89         }
90
91         /**
92          * Reads a 32bit Integer from the Stream
93          *
94          * @return mixed The integer, corresponding to the next 32 bits from
95          *      the stream of false if there are not enough bytes or on error
96          */
97         function readint32() {
98                 $bytes = $this->read(4);
99                 if (4 != strlen($bytes))
100                         return false;
101                 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
102                 $int = unpack($endian_letter, $bytes);
103                 return array_shift($int);
104         }
105
106         /**
107          * Reads an array of 32-bit Integers from the Stream
108          *
109          * @param integer count How many elements should be read
110          * @return mixed Array of integers or false if there isn't
111          *      enough data or on error
112          */
113         function readint32array($count) {
114                 $bytes = $this->read(4 * $count);
115                 if (4*$count != strlen($bytes))
116                         return false;
117                 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
118                 return unpack($endian_letter.$count, $bytes);
119         }
120 }
121
122 ?>