Passed
Push — master ( f3b2b6...6d582f )
by Michal
02:40
created

StringReader::readint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 29
        $result = $data[1];
74
75
        /* We're reading unsigned int, but PHP will happily
76
         * give us negative number on 32-bit platforms.
77
         *
78
         * See also documentation:
79
         * https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes
80
         */
81 29
        return $result < 0 ? PHP_INT_MAX : $result;
82
    }
83
84
    /**
85
     * Reads an array of integers from the stream.
86
     *
87
     * @param string $unpack Unpack string
88
     * @param int    $pos    Position
89
     * @param int    $count  How many elements should be read
90
     *
91
     * @return array Array of Integers
92
     */
93 26
    public function readintarray($unpack, $pos, $count)
94
    {
95 26
        return unpack($unpack . $count, $this->read($pos, 4 * $count));
96
    }
97
}
98