forked from wonderfulfrog/keyvalues-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdfparser.php
More file actions
144 lines (114 loc) · 2.9 KB
/
vdfparser.php
File metadata and controls
144 lines (114 loc) · 2.9 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* VDF (Valve Data Format) file parser
* author: devinwl
* version: 1.03
*/
define("QUOTE", "\"");
define("CURLY_BRACE_BEGIN", "{");
define("CURLY_BRACE_END", "}");
define("NEW_LINE", "\n");
define("C_RETURN", "\r");
define("C_TAB", "\t");
define("C_ESCAPE", "\\");
function VDFParse($filename) {
$parsed = array();
$ptr = &$parsed;
$path = ''; // The current level represented as a string
$p = 0; // How many quotes have been seen
$string = ""; // The string of consumed characters
$key = ""; // The discovered key
$value = ""; // The discovered corresponding value
$reading = false; // Currently consuming characters
$lastCharSeen = ""; // Tracks the last character seen
$filecontent = file_get_contents($filename);
// Strip all comments before parsing
$filecontent = str_replace('!//.*!', '', $filecontent);
// Begin parsing by character
$chunks = str_split($filecontent, 2048);
unset($filecontent);
foreach ($chunks as &$chunk) {
$chars = str_split($chunk, 1);
foreach($chars as $c) {
// Dont consume any escapes or quotes (unless the last seen character is escaping them)
if($reading) {
if($lastCharSeen == C_ESCAPE) {
$string .= C_ESCAPE . $c;
}
else if($c != C_ESCAPE && $c != QUOTE)
$string .= $c;
}
// If both the key and value have been discovered store them and reset
if(strlen($key) > 0 && strlen($value) > 0) {
$ptr[$key] = $value;
$key = '';
$value = '';
$p = 0;
}
// Handle the character
switch($c) {
case QUOTE:
if($lastCharSeen != C_ESCAPE) {
$comment_chars_seen = 0;
$p++; // Quote counter
if($p == 5) $p = 1;
if($reading) {
// End parsing string
$reading = false;
switch($p) {
case 2: // Key
$key = $string;
$string = '';
break;
case 4: // Value
$value = $string;
$string = '';
break;
}
}
else {
$reading = true;
}
}
break;
case CURLY_BRACE_BEGIN:
$comment_chars_seen = 0;
if(!(strlen($key)>0)) die("Not properly formed key-value structure" . print_r($parsed));
$ptr[$key] = array();
$ptr = &$ptr[$key];
// Keep track of depth via a string path
if($path == '')
$path .= $key;
else
$path .= '.' . $key;
// Reset for new level
$string = '';
$key = '';
$value = '';
$p = 0;
break;
case CURLY_BRACE_END:
$ptr = &$parsed;
$full_path = explode(".", $path);
$new_path = '';
if(count($full_path) > 0) {
$i = 0;
for($i = 0; $i < count($full_path)-1; $i++) {
if($new_path == '')
$new_path .= $full_path[$i];
else
$new_path .= '.' . $full_path[$i];
$ptr = &$ptr[$full_path[$i]];
}
}
$path = $new_path;
break;
default:
$comment_chars_seen = 0;
}
$lastCharSeen = $c;
}
}
return $parsed;
}
?>