-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.php
73 lines (68 loc) · 1.42 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
function read_until_whitespace($stream, $maxchars=null) {
$txt = '';
while (true) {
$tok = fread($stream, 1);
if (trim($tok) == '') {
break;
}
$txt .= $tok;
if (strlen($txt) == $maxchars) {
break;
}
}
return $txt;
}
function read_non_whitespace($stream) {
$tok = ' ';
while (($tok == "\n") || ($tok == "\r") || ($tok == " ") || ($tok == "\t")) {
$tok = fread($stream, 1);
}
return $tok;
}
function utf16_decode($str) {
if (strlen($str) < 2) {
return $str;
}
$bom_be = true;
$c0 = ord($str{0});
$c1 = ord($str{1});
if (($c0 == 0xfe) && ($c1 == 0xff)) {
$str = substr($str, 2);
} else if (($c0 == 0xff) && ($c1 == 0xfe)) {
$str = substr($str, 2);
$bom_be = false;
}
$len = strlen($str);
$newstr = '';
for ($i=0; $i<$len; $i+=2) {
if ($bom_be) {
$val = ord($str{$i}) << 4;
$val += ord($str{$i+1});
} else {
$val = ord($str{$i+1}) << 4;
$val += ord($str{$i});
}
$newstr .= ($val == 0x228) ? "\n" : chr($val);
}
return $newstr;
}
# debugging
function hi($o) {
print _hi($o) . "\n";
}
function _hi($o) {
if ($o === null) {
return "None";
} else if (is_array($o)) {
$out = "{";
foreach ($o as $k=>$v) {
$out .= _hi($k) . " => " . _hi($v) . ", ";
}
$out .= "}";
return $out;
} else {
return $o;
}
}
?>