-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.php
More file actions
58 lines (50 loc) · 2.33 KB
/
Copy pathutils.php
File metadata and controls
58 lines (50 loc) · 2.33 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
<?php
/* SPDX-License-Identifier: GPL-3.0-or-later */
use JetBrains\PhpStorm\NoReturn;
function list_files(string $dir): array {
return array_diff(scandir($dir), array('..', '.'));
}
function list_files_recursive(string $dir) : array {
if ($dir[strlen($dir) - 1] != '/') $dir = $dir . '/';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$paths = array();
foreach ($iterator as $file) {
// Ignore .. and .
$pathname = $file->getPathname();
$last_segment = substr($pathname, strrpos($pathname, '/') + 1);
if ($last_segment == '..') continue;
$relative_path = str_replace($dir, '', $pathname);
if ($relative_path == '.') continue; // Ignore this directory
if ($last_segment == '.') $relative_path = substr($relative_path, 0, strlen($relative_path) - 1);
$paths[] = $relative_path;
}
return $paths;
}
// https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
function android_escape(string $string): string {
return strtr($string, array('@' => '\@', '?' => '\?', '<' => '<', '>' => '>', '"' => '\"', "'" => "\'", '&' => '&'));
}
function android_escape_slash(string $string) : string {
return strtr($string, array('@' => '\@', '?' => '\?', '<' => '<', '>' => '>', '"' => '\"', "'" => "\'", '&' => '&', '\\' => '\\\\'));
}
function android_escape_slash_newline(string $string) : string {
return strtr($string, array('@' => '\@', '?' => '\?', '<' => '<', '>' => '>', '"' => '\"', "'" => "\'", '&' => '&', '\\' => '\\\\', "\n" => '\n'));
}
function android_escape_slash_newline_reverse(string $string) : string {
return strtr($string, array('\@' => '@', '\?' => '?', '<' => '<', '>' => '>', '\"' => '"', "\'" => "'", '&' => '&', '\\\\' => '\\', '\n' => "\n"));
}
#[NoReturn]
function syntax_error_with_position(string $path, string $texts, int $position, string $error_message = "Syntax error"): void {
$lines = explode("\n", $texts);
$line_no = 0;
foreach ($lines as $line) {
++$line_no;
$len = strlen($line);
if ($len > $position) {
break;
}
$position -= ($len + 1);
}
fprintf(STDERR, "\e[41;1mError:\e[0m %s near $path:$line_no:%d \n", $error_message, abs($position));
exit(1);
}