/
utils.php
158 lines (146 loc) · 3.09 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
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
<?php
function concatValues($o) {
if (is_array($o)) {
$rt = NULL;
foreach($o as $item) {
if ($rt) {
$rt = $rt . " " . "$item";
} else {
$rt = "$item";
}
}
return $rt;
} else {
return "$o";
}
}
function dumpValue_( $o, $depth) {
if (is_array($o)) {
if ($depth == 0) {
return "{...}";
}
$s = '{ ';
$i = 0;
foreach($o as $k => $v) {
if ($i > 0) {
$s = $s . ', ';
}
$i = $i + 1;
$s = $s . '[' . $k . '] = ' . dumpValue_( $v, $depth-1);
}
return $s . '} ';
} elseif (is_numeric($o)) {
$num = round( floatval( $o), 5);
return "$num";
} elseif (is_string($o)) {
return "$o";
} elseif (is_null($o)) {
return "NULL";
} else {
return dumpValue_( get_object_vars($o), $depth);
}
}
function dumpValue( $o) {
return dumpValue_( $o, 10);
}
function dumpTree_( $indent, $o, $depth, $excludeSet) {
if (is_array($o)) {
if ($depth == 0) {
return "{...}";
}
$keyset = [];
foreach($o as $k => $v) {
if ($excludeSet[$k] != TRUE)
{
array_push( $keyset, $k);
}
}
sort( $keyset);
$s = "";
foreach($keyset as $k) {
$ke = "\n" . $indent . gettype($k) . " " . $k;
$ve = dumpTree_( $indent . ' ', $o[ $k], $depth-1, $excludeSet);
$s = $s . $ke . ": " . $ve;
}
return $s;
} elseif (is_numeric($o)) {
$num = round( floatval( $o), 5);
return "$num";
} elseif (is_string($o)) {
return "\"$o\"";
} elseif (is_null($o)) {
return NULL;
} elseif (is_bool($o)) {
if ($o)
{
return "TRUE";
}
else
{
return "FALSE";
}
} else {
return dumpTree_( $indent, get_object_vars($o), $depth, $excludeSet);
}
}
function dumpTree( $o) {
return dumpTree_( "", $o, 20, []);
}
function dumpTreeWithFilter( $o, $excludeList) {
$excludeSet = [];
foreach ($excludeList as $exl) {
$excludeSet[ $exl] = TRUE;
}
return dumpTree_( "", $o, 20, $excludeSet);
}
function readFileContent( $path) {
$myfile = fopen( $path, "r") or die("Unable to open file '$path' for reading");
$content = fread( $myfile, filesize( $path));
fclose($myfile);
return $content;
}
function writeFileContent( $path, $content) {
$myfile = fopen( $path, "w") or die("Unable to open file '$path' for writing");
fwrite( $myfile, $content);
fclose($myfile);
}
function verifyTestOutput( $outputdir, $result, $expected) {
if (strcmp( $result, $expected) != 0) {
writeFileContent( $outputdir . "/RES", $result);
writeFileContent( $outputdir . "/EXP", $expected);
echo "Failed\n";
exit( 1);
} else {
echo "OK\n";
}
}
function getPathArray( $filepath) {
return array_filter( explode('/', $filepath), 'strlen');
}
function getFileName( $filepath) {
$pathar = getPathArray( $filepath);
return end($pathar);
}
function joinLists(...$args) {
$rt = [];
foreach($args as $arg) {
if (is_object($arg) || is_array($arg)) {
foreach($arg as $elem) {
array_push( $rt, $elem);
}
} else {
array_push( $rt, $arg);
}
}
return $rt;
}
function getContextConfig( $argval) {
if (strcmp($argval,'trace') == 0) {
return ["trace" => ["log" => "dump", "file" => "stdout"]];
} elseif ($argval) {
return ["rpc" => argval];
} else {
return NULL;
}
}
?>