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 138 2009-06-23 13:22:09Z 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 | $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
|
---|
24 | }
|
---|
25 |
|
---|
26 | function _substr($string, $start, $length) {
|
---|
27 | if ($this->is_overloaded) {
|
---|
28 | return mb_substr($string,$start,$length,'ascii');
|
---|
29 | } else {
|
---|
30 | return substr($string,$start,$length);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | function _strlen($string) {
|
---|
35 | if ($this->is_overloaded) {
|
---|
36 | return mb_strlen($string,'ascii');
|
---|
37 | } else {
|
---|
38 | return strlen($string);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | function read($bytes) {
|
---|
43 | $data = $this->_substr($this->_str, $this->_pos, $bytes);
|
---|
44 | $this->_pos += $bytes;
|
---|
45 | if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
|
---|
46 | return $data;
|
---|
47 | }
|
---|
48 |
|
---|
49 | function seekto($pos) {
|
---|
50 | $this->_pos = $pos;
|
---|
51 | if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
|
---|
52 | return $this->_pos;
|
---|
53 | }
|
---|
54 |
|
---|
55 | function pos() {
|
---|
56 | return $this->_pos;
|
---|
57 | }
|
---|
58 |
|
---|
59 | function length() {
|
---|
60 | return $this->_strlen($this->_str);
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Reads the contents of the file in the beginning.
|
---|
67 | */
|
---|
68 | class POMO_CachedFileReader extends POMO_StringReader {
|
---|
69 | function POMO_CachedFileReader($filename) {
|
---|
70 | parent::POMO_StringReader();
|
---|
71 | $this->_str = file_get_contents($filename);
|
---|
72 | if (false === $this->_str)
|
---|
73 | return false;
|
---|
74 | $this->_pos = 0;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Allows reading integers from a file.
|
---|
80 | */
|
---|
81 | class POMO_CachedIntFileReader extends POMO_CachedFileReader {
|
---|
82 |
|
---|
83 | var $endian = 'little';
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Opens a file and caches it.
|
---|
87 | *
|
---|
88 | * @param $filename string name of the file to be opened
|
---|
89 | * @param $endian string endianness of the words in the file, allowed
|
---|
90 | * values are 'little' or 'big'. Default value is 'little'
|
---|
91 | */
|
---|
92 | function POMO_CachedIntFileReader($filename, $endian = 'little') {
|
---|
93 | $this->endian = $endian;
|
---|
94 | parent::POMO_CachedFileReader($filename);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Sets the endianness of the file.
|
---|
99 | *
|
---|
100 | * @param $endian string 'big' or 'little'
|
---|
101 | */
|
---|
102 | function setEndian($endian) {
|
---|
103 | $this->endian = $endian;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Reads a 32bit Integer from the Stream
|
---|
108 | *
|
---|
109 | * @return mixed The integer, corresponding to the next 32 bits from
|
---|
110 | * the stream of false if there are not enough bytes or on error
|
---|
111 | */
|
---|
112 | function readint32() {
|
---|
113 | $bytes = $this->read(4);
|
---|
114 | if (4 != $this->_strlen($bytes))
|
---|
115 | return false;
|
---|
116 | $endian_letter = ('big' == $this->endian)? 'N' : 'V';
|
---|
117 | $int = unpack($endian_letter, $bytes);
|
---|
118 | return array_shift($int);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Reads an array of 32-bit Integers from the Stream
|
---|
123 | *
|
---|
124 | * @param integer count How many elements should be read
|
---|
125 | * @return mixed Array of integers or false if there isn't
|
---|
126 | * enough data or on error
|
---|
127 | */
|
---|
128 | function readint32array($count) {
|
---|
129 | $bytes = $this->read(4 * $count);
|
---|
130 | if (4*$count != $this->_strlen($bytes))
|
---|
131 | return false;
|
---|
132 | $endian_letter = ('big' == $this->endian)? 'N' : 'V';
|
---|
133 | return unpack($endian_letter.$count, $bytes);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | ?>
|
---|