-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_vsn1.php
42 lines (38 loc) · 1.12 KB
/
read_vsn1.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
<?php
function VSN1notempty($s) { return strlen(trim($s)) && $s[0] != '#'; }
function ParseVSN1Tokens($tokens, &$pos)
{
$end = count($tokens);
$res = Array();
while($pos < $end)
{
if($tokens[$pos][0] == '"') return stripslashes(substr($tokens[$pos++], 1, -1));
if(preg_match('/^[0-9]+$/', $tokens[$pos])) return (int)$tokens[$pos++];
if($tokens[$pos] == '{') { ++$pos; continue; }
if($tokens[$pos] == '}') { ++$pos; break; }
if($tokens[$pos] == '[')
{
++$pos;
for(;;)
{
if($pos >= $end) break;
if($tokens[$pos] == ']') { ++$pos; break; }
$res[] = ParseVSN1Tokens($tokens, $pos);
if($tokens[$pos] == ',') ++$pos;
}
return $res;
}
$key = $tokens[$pos++];
if($tokens[$pos] == '=') ++$pos;
$res[$key] = ParseVSN1Tokens($tokens, $pos);
}
return $res;
}
function parse_vsn1($s)
{
$tokens = preg_split('@(#.*|"(?:\\\"|[^"])*"|[0-9a-zA-Z_][-a-zA-Z_0-9.]*|.)@',
$s, -1, PREG_SPLIT_DELIM_CAPTURE);
$tokens = array_values(array_filter($tokens, 'VSN1notempty'));
$pos = 0;
return ParseVSN1Tokens($tokens, $pos);
}