Passed
Push — master ( 690e70...f3b2b6 )
by Michal
02:30
created

StringReader::readintarray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
    Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <[email protected]>.
4
    Copyright (c) 2016 Michal Čihař <[email protected]>
5
6
    This file is part of MoTranslator.
7
8
    This program 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
    This program 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 along
19
    with this program; if not, write to the Free Software Foundation, Inc.,
20
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
*/
22
23
namespace PhpMyAdmin\MoTranslator;
24
25
/**
26
 * Simple wrapper around string buffer for
27
 * random access and values parsing.
28
 */
29
class StringReader
30
{
31
    private $_str;
32
    private $_len;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string $filename Name of file to load
38
     */
39 31
    public function __construct($filename)
40
    {
41 31
        $this->_str = file_get_contents($filename);
42 31
        $this->_len = strlen($this->_str);
43 31
    }
44
45
    /**
46
     * Read number of bytes from given offset.
47
     *
48
     * @param int $pos   Offset
49
     * @param int $bytes Number of bytes to read
50
     *
51
     * @return string
52
     */
53 31
    public function read($pos, $bytes)
54
    {
55 31
        if ($pos + $bytes > $this->_len) {
56 5
            throw new ReaderException('Not enough bytes!');
57
        }
58
59 30
        return substr($this->_str, $pos, $bytes);
60
    }
61
62
    /**
63
     * Reads a 32bit integer from the stream.
64
     *
65
     * @param string $unpack Unpack string
66
     * @param int    $pos    Position
67
     *
68
     * @return int Ingerer from the stream
69
     */
70 29
    public function readint($unpack, $pos)
71
    {
72 29
        $data = unpack($unpack, $this->read($pos, 4));
73
74
        /* We're reading unsigned int, but PHP will happily
75
         * give us negative number on 32-bit platforms.
76
         *
77
         * See also documentation:
78
         * https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes
79
         */
80 29
        if ($data[1] < 0) {
81
            return PHP_INT_MAX;
82
        }
83
84 29
        return $data[1];
85
    }
86
87
    /**
88
     * Reads an array of integers from the stream.
89
     *
90
     * @param string $unpack Unpack string
91
     * @param int    $pos    Position
92
     * @param int    $count  How many elements should be read
93
     *
94
     * @return array Array of Integers
95
     */
96 26
    public function readintarray($unpack, $pos, $count)
97
    {
98 26
        return unpack($unpack . $count, $this->read($pos, 4 * $count));
99
    }
100
}
101