summaryrefslogtreecommitdiff
path: root/ext/standard/tests/file/windows_mb_path/util.inc
blob: 57fd8f9427644c590000d1564e07dbfbf5a1c491 (plain)
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
<?php

function get_active_cp($kind = "")
{
	if (version_compare(PHP_VERSION, '7.1', '<')) {
		$s = exec("chcp");
		preg_match(",.*: (\d+),", $s, $m);

		return $m[1];
	} else {
		return sapi_windows_cp_get($kind);
	}
}

function set_active_cp($cp, $echo = true)
{
	if (version_compare(PHP_VERSION, '7.1', '<')) {
		$ret = exec("chcp $cp");
	} else {
		if (!sapi_windows_cp_set($cp)) {
			echo "Failed to set cp $cp\n";
			return;
		}

		if ($echo) echo "Active code page: ", get_active_cp(), "\n";
	}
}

function get_basename_with_cp($path, $cp, $echo = true)
{
	$old_cp = get_active_cp();
	set_active_cp($cp, $echo);

	if ($echo) echo "getting basename of $path\n";

	$cmd = "powershell -command \"Get-Item -Path '$path' | Format-Table -HideTableHeaders Name\"";
	$out = trim(shell_exec($cmd));

	if ($echo) var_dump($out, $out == basename($path));
	if ($echo) var_dump(realpath($path));

	set_active_cp($old_cp, $echo);

	return $out;
}

function skip_if_wrong_cp($cp, $kind = "")
{
	if (get_active_cp($kind) != $cp) {
		die("skip this test expect codepage $cp");
	}
}

function skip_if_no_required_exts()
{
	$exts = func_get_args();
	$exts[] = "iconv";

	foreach ($exts as $ext) {
		if (!extension_loaded($ext)) {
			die("skip $ext is not loaded");
		}
	}
}

function skip_if_not_win()
{
	if(substr(PHP_OS, 0, 3) != 'WIN' ) {
		die('skip windows only test');
	}
}

function create_verify_file($prefix, $basename, $content = "", $cp = 65001)
{
	$full = $prefix . DIRECTORY_SEPARATOR . $basename;

	if (!touch($full)) {
		echo "failed to touch create $full\n";
		return;
	}

	$now = get_basename_with_cp($full, $cp, false);
	if ($now !== $basename) {
		echo "expected '$basename', got '$now'\n";
		return;
	}

	if ($content) {
		file_put_contents($full, $content);
	}
}

function create_verify_dir($prefix, $basename, $cp = 65001)
{
	$full = $prefix . DIRECTORY_SEPARATOR . $basename;

	if (!mkdir($full) || get_basename_with_cp($full, $cp, false) !== $basename) {
		echo "failed to create dir '$full'\n";
	}
}

function remove_data($id, $dir = NULL)
{
	if (!$dir) {
		$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;
	}

	if (is_dir($dir)) {
		$objects = scandir($dir);
		foreach ($objects as $object) {
			if ($object != "." && $object != "..") {
				if (filetype($dir . DIRECTORY_SEPARATOR . $object) == "dir") 
					remove_data($id, $dir . DIRECTORY_SEPARATOR . $object); 
				else 
					unlink($dir . DIRECTORY_SEPARATOR . $object);
			}
		}
		reset($objects);
		rmdir($dir);
	} 
}

function create_data($id, $item = "", $cp = 65001, $utf8 = true)
{
	if ($utf8) {
		/* Keep this file ASCII, so zend.multibyte related stuff can be tasted as well. */ 
		include dirname(__FILE__) . DIRECTORY_SEPARATOR . "util_utf8.inc";
		return create_data_from_utf8($id, $item, $cp);
	} else {

		$prefix = dirname(__FILE__) . DIRECTORY_SEPARATOR . $id;

		if (!is_dir($prefix)) {
			mkdir($prefix);
		}

		if (0 === strpos($id, "dir")) {
			create_verify_dir($prefix, $item, $cp);
		} else if (0 === strpos($id, "file")) {
			/* a bit unhandy, but content can be put from outside, if needed */
			create_verify_file($prefix, $item, "dummy content", $cp);
		} else {
			echo "Item has either to start with \"dir\" or \"file\"";
		}
	}

	return $prefix;
}