-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdfparser.php
170 lines (138 loc) · 3.83 KB
/
vdfparser.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* VDF (Valve Data Format) file parser
* author: devinwl
* version: 1.07
*/
define("QUOTE", "\"");
define("SINGLE_QUOTE", "'");
define("CURLY_BRACE_BEGIN", "{");
define("CURLY_BRACE_END", "}");
define("NEW_LINE", "\n");
define("C_RETURN", "\r");
define("C_TAB", "\t");
define("C_ESCAPE", "\\");
define("C_COMMENT", '/');
define("PATH_DEPTH_SEPARATOR", '>>>>');
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
$comment_chars_seen = 0; // Tracks the number of comments characters seen (2 comment characters designates the start of a comment)
$filecontent = file_get_contents($filename);
// 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) {
if($c == QUOTE || $c == SINGLE_QUOTE || $c == C_ESCAPE)
$string .= $c;
else
$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;
}
// If I haven't seen a comment yet, parse the character
// Otherwise, ignore all characters until a newline
if($comment_chars_seen < 2) {
// 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:
// Ignore character if we are consuming a key/value
if(!$reading) {
$comment_chars_seen = 0;
if(!(strlen($key)>0)) { print_r($parsed); die("Not properly formed key-value structure"); }
$ptr[$key] = array();
$ptr = &$ptr[$key];
// Keep track of depth via a string path
if($path == '')
$path .= $key;
else
$path .= PATH_DEPTH_SEPARATOR . $key;
// Reset for new level
$string = '';
$key = '';
$value = '';
$p = 0;
}
break;
case CURLY_BRACE_END:
// Ignore character if we are consuming a key/value
if(!$reading) {
$ptr = &$parsed;
$full_path = explode(PATH_DEPTH_SEPARATOR, $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 .= PATH_DEPTH_SEPARATOR . $full_path[$i];
$ptr = &$ptr[$full_path[$i]];
}
}
$path = $new_path;
}
break;
case C_COMMENT:
// Only look for comments if we're not currently reading a key or value
if(!$reading)
$comment_chars_seen++;
break;
default:
$comment_chars_seen = 0;
}
}
// Reset comment counter
if($c == NEW_LINE) {
$comment_chars_seen = 0;
}
$lastCharSeen = $c;
}
}
return $parsed;
}
?>