-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpubcommon.php
More file actions
156 lines (117 loc) · 3.03 KB
/
pubcommon.php
File metadata and controls
156 lines (117 loc) · 3.03 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
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// Matt Browne
// http://php.net/manual/en/function.json-decode.php#112735
function json_clean_decode()
{
$args = func_get_args();
if (count($args) < 1)
return null;
$args[0] = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $args[0]);
return call_user_func_array('json_decode', $args);
}
class NameManager
{
private $name_usages = array();
public function generateName($item_name, $gender = null)
{
$name = strtolower($item_name);
$name = preg_replace('/[\\(\\[].*?[\\)\\]]/', '', $name);
$name = trim($name);
$name = preg_replace('/[^a-z0-9_]/', '_', $name);
$name = str_replace('__', '_', $name);
if (!is_null($gender))
{
if ($gender) $name .= '_m';
else $name .= '_f';
}
if (!isset($this->name_usages[$name]))
{
$this->name_usages[$name] = 1;
}
else
{
++$this->name_usages[$name];
$name .= '_' . $this->name_usages[$name];
}
return $name;
}
};
define('PUB_INT1', 1);
define('PUB_INT2', 2);
define('PUB_INT3', 3);
define('PUB_INT4', 4);
define('PUB_STRING', 5);
define('PUB_KEY_TYPE', 0);
define('PUB_KEY_NAME', 1);
define('PUB_KEY_ENUM', 2);
function pub_format_parse_line($line, $line_no = null)
{
static $type_map = array(
'int1' => PUB_INT1,
'int2' => PUB_INT2,
'int3' => PUB_INT3,
'int4' => PUB_INT4,
'string' => PUB_STRING
);
$format = array();
$parts = explode(' ', $line);
if (!is_null($line_no))
$line_message = " on line " . $line_no;
else
$line_message = '';
if (count($parts) < 2)
throw new Exception("Invalid line in pub format file" . $line_message . ": " . $line);
if (!isset($type_map[$parts[0]]))
throw new Exception("Unknown type in pub format file" . $line_message . ": " . $parts[0]);
$format[PUB_KEY_TYPE] = $type_map[$parts[0]];
$format[PUB_KEY_NAME] = $parts[1];
if (count($parts) >= 3)
{
if ($parts[2] != '{')
throw new Exception("Bad line in pub format file" . $line_message . ": " . $line);
$ended = false;
$enum = array();
for ($i = 3; $i < count($parts); ++$i)
{
if ($parts[$i] == '')
continue;
if ($ended)
throw new Exception("Junk after '}' in pub format file" . $line_message . ": " . $line);
if ($parts[$i] == '}')
{
$ended = true;
continue;
}
$value = $parts[$i];
if ($value == '?')
$value = null;
$enum[] = $value;
}
if (!$ended)
throw new Exception("Missing '}' in pub format file" . $line_message . ": " . $line);
$format[PUB_KEY_ENUM] = $enum;
}
return $format;
}
function pub_format_parse_file($filename)
{
$format = array();
$lines = file($filename);
$unk_count = 0;
$drop_count = 0;
foreach ($lines as $line_no => $line)
{
$line = trim($line);
if (strlen($line) == 0)
continue;
if (substr($line, 0, 1) == '#')
continue;
$entry = pub_format_parse_line($line, $line_no+1);
if ($entry[PUB_KEY_NAME] == '?')
$entry[PUB_KEY_NAME] = 'unknown'. (++$unk_count);
if ($entry[PUB_KEY_NAME] == '-')
$entry[PUB_KEY_NAME] = 'Xdrop'. (++$drop_count);
$format[] = $entry;
}
return $format;
}