Completed
Push — master ( 56c695...dbb523 )
by Michal
03:48
created

StringReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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 19
    public function __construct($filename)
40
    {
41 19
        $this->_str = file_get_contents($filename);
42 19
        $this->_len = strlen($this->_str);
43 19
    }
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 19
    public function read($pos, $bytes)
54
    {
55 19
        if ($pos + $bytes > $this->_len) {
56 4
            throw new ReaderException('Not enough bytes!');
57
        }
58
59 18
        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 17
    public function readint($unpack, $pos)
71
    {
72 17
        $data = unpack($unpack, $this->read($pos, 4));
73
74 17
        return $data[1];
75
    }
76
77
    /**
78
     * Reads an array of integers from the stream.
79
     *
80
     * @param string $unpack Unpack string
81
     * @param int    $pos    Position
82
     * @param int    $count  How many elements should be read
83
     *
84
     * @return array Array of Integers
85
     */
86 14
    public function readintarray($unpack, $pos, $count)
87
    {
88 14
        return unpack($unpack . $count, $this->read($pos, 4 * $count));
89
    }
90
}
91