Skip to content

Commit cd3d305

Browse files
committed
ext/standard: use sapi_windows_cp_*() functions directly
1 parent d863d4c commit cd3d305

File tree

5 files changed

+37
-68
lines changed

5 files changed

+37
-68
lines changed

ext/standard/tests/file/windows_mb_path/bug64699.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
1212

1313
include __DIR__ . DIRECTORY_SEPARATOR . "util.inc";
1414

15-
$old_cp = get_active_cp();
16-
set_active_cp(65001);
15+
$old_cp = sapi_windows_cp_get();
16+
sapi_windows_cp_set(65001);
17+
echo "Active code page: ", sapi_windows_cp_get(), "\n";
1718

1819
$prefix = __DIR__ . DIRECTORY_SEPARATOR . "testBug64699" . DIRECTORY_SEPARATOR;
1920

@@ -41,10 +42,10 @@ foreach ($dirs as $d) {
4142
}
4243
rmdir($prefix);
4344

44-
set_active_cp($old_cp);
45+
sapi_windows_cp_set($old_cp);
4546

4647
?>
47-
--EXPECTF--
48+
--EXPECT--
4849
Active code page: 65001
4950
filetype()[dir ] == is_dir()[dir ] -> OK: .
5051
filetype()[dir ] == is_dir()[dir ] -> OK: ..
@@ -54,4 +55,3 @@ filetype()[dir ] == is_dir()[dir ] -> OK: ソ
5455
filetype()[dir ] == is_dir()[dir ] -> OK: ゾ
5556
filetype()[dir ] == is_dir()[dir ] -> OK: 多国語
5657
filetype()[dir ] == is_dir()[dir ] -> OK: 表
57-
Active code page: %d

ext/standard/tests/file/windows_mb_path/test_cwd_mb_names.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ $prefix = create_data("dir_mb");
1717
$dirw = $prefix . DIRECTORY_SEPARATOR . "テストマルチバイト・パス42";
1818
touch($dirw . DIRECTORY_SEPARATOR . "dummy.txt");
1919

20-
$old_cp = get_active_cp();
21-
set_active_cp(65001);
20+
$old_cp = sapi_windows_cp_get();
21+
sapi_windows_cp_set(65001);
22+
echo "Active code page: ", sapi_windows_cp_get(), "\n";
2223

2324
$oldcwd = getcwd();
2425
var_dump(chdir($dirw));
2526
var_dump(getcwd());
2627

2728
var_dump(file_exists("dummy.txt"));
2829

29-
set_active_cp($old_cp);
30+
sapi_windows_cp_set($old_cp);
3031

3132
chdir($oldcwd);
3233
remove_data("dir_mb");
@@ -37,4 +38,3 @@ Active code page: 65001
3738
bool(true)
3839
string(%d) "%s\テストマルチバイト・パス42"
3940
bool(true)
40-
Active code page: %d

ext/standard/tests/file/windows_mb_path/test_readdir_mb_names.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ create_verify_dir($prefix, "żółć");
3434

3535
$dirw = $prefix . DIRECTORY_SEPARATOR;
3636

37-
$old_cp = get_active_cp();
38-
set_active_cp(65001);
37+
$old_cp = sapi_windows_cp_get();
38+
sapi_windows_cp_set(65001);
39+
echo "Active code page: ", sapi_windows_cp_get(), "\n";
3940

4041
if (is_dir($dirw)) {
4142
if ($dh = opendir($dirw)) {
@@ -47,12 +48,12 @@ if (is_dir($dirw)) {
4748
} else {
4849
echo "is_dir failed\n";
4950
}
50-
set_active_cp($old_cp);
51+
sapi_windows_cp_set($old_cp);
5152

5253
remove_data("mb_names");
5354

5455
?>
55-
--EXPECTF--
56+
--EXPECT--
5657
Active code page: 65001
5758
filename: . : filetype: dir
5859
filename: .. : filetype: dir
@@ -72,4 +73,3 @@ filename: テストマルチバイト・パス : filetype: file
7273
filename: テストマルチバイト・パス42 : filetype: dir
7374
filename: 測試多字節路徑 : filetype: file
7475
filename: 測試多字節路徑5 : filetype: dir
75-
Active code page: %d

ext/standard/tests/file/windows_mb_path/test_rename_mb_names.phpt

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,34 @@ file2_mb
1010
--FILE--
1111
<?php
1212

13-
1413
include __DIR__ . DIRECTORY_SEPARATOR . "util.inc";
1514

16-
1715
$prefix = create_data("file2_mb");
1816

1917
$fw_orig = $prefix . DIRECTORY_SEPARATOR . "Ελλάδα.txt";
20-
21-
22-
2318
$fw_copied = $prefix . DIRECTORY_SEPARATOR . "Ελλάδα_copy.txt";
24-
25-
2619
$fw_renamed = $prefix . DIRECTORY_SEPARATOR . "測試多字節路徑17.txt";
2720

28-
29-
30-
$old_cp = get_active_cp();
31-
32-
set_active_cp(65001);
33-
34-
35-
36-
21+
$old_cp = sapi_windows_cp_get();
22+
sapi_windows_cp_set(65001);
23+
echo "Active code page: ", sapi_windows_cp_get(), "\n";
3724

3825
var_dump(copy($fw_orig, $fw_copied));
39-
var_dump(get_basename_with_cp($fw_copied, get_active_cp(), false));
26+
var_dump(get_basename($fw_copied, false));
4027
var_dump(file_exists($fw_copied));
4128

4229
var_dump(rename($fw_copied, $fw_renamed));
43-
var_dump(get_basename_with_cp($fw_renamed, get_active_cp(), false));
30+
var_dump(get_basename($fw_renamed, false));
4431
var_dump(file_exists($fw_renamed));
4532

4633
var_dump(unlink($fw_renamed));
4734

48-
set_active_cp($old_cp);
35+
sapi_windows_cp_set($old_cp);
4936

5037
remove_data("file2_mb");
5138

5239
?>
53-
--EXPECTF--
40+
--EXPECT--
5441
Active code page: 65001
5542
bool(true)
5643
string(21) "Ελλάδα_copy.txt"
@@ -59,4 +46,3 @@ bool(true)
5946
string(27) "測試多字節路徑17.txt"
6047
bool(true)
6148
bool(true)
62-
Active code page: %d

ext/standard/tests/file/windows_mb_path/util.inc

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,35 @@
11
<?php
22

3-
function get_active_cp($kind = "")
3+
function get_basename($path, $echo = true)
44
{
5-
if (version_compare(PHP_VERSION, '7.1', '<')) {
6-
$s = exec("chcp");
7-
preg_match(",.*: (\d+),", $s, $m);
5+
if ($echo) echo "getting basename of $path\n";
86

9-
return $m[1];
10-
} else {
11-
return sapi_windows_cp_get($kind);
12-
}
13-
}
7+
$cmd = "powershell -command \"Get-Item -Path '$path' | Format-Table -HideTableHeaders Name\"";
8+
$out = trim(shell_exec($cmd));
149

15-
function set_active_cp($cp, $echo = true)
16-
{
17-
if (version_compare(PHP_VERSION, '7.1', '<')) {
18-
$ret = exec("chcp $cp");
19-
} else {
20-
if (!sapi_windows_cp_set($cp)) {
21-
echo "Failed to set cp $cp\n";
22-
return;
23-
}
10+
if ($echo) var_dump($out, $out == basename($path));
11+
if ($echo) var_dump(realpath($path));
2412

25-
if ($echo) echo "Active code page: ", get_active_cp(), "\n";
26-
}
13+
return $out;
2714
}
2815

2916
function get_basename_with_cp($path, $cp, $echo = true)
3017
{
31-
$old_cp = get_active_cp();
32-
set_active_cp($cp, $echo);
33-
34-
if ($echo) echo "getting basename of $path\n";
18+
$old_cp = sapi_windows_cp_get();
19+
sapi_windows_cp_set($cp);
20+
if ($echo) echo "Active code page: ", sapi_windows_cp_get(), "\n";
3521

36-
$cmd = "powershell -command \"Get-Item -Path '$path' | Format-Table -HideTableHeaders Name\"";
37-
$out = trim(shell_exec($cmd));
38-
39-
if ($echo) var_dump($out, $out == basename($path));
40-
if ($echo) var_dump(realpath($path));
22+
$out = get_basename($path, $echo);
4123

42-
set_active_cp($old_cp, $echo);
24+
sapi_windows_cp_set($old_cp);
25+
if ($echo) echo "Active code page: ", sapi_windows_cp_get(), "\n";
4326

4427
return $out;
4528
}
4629

4730
function skip_if_wrong_cp($cp, $kind = "")
4831
{
49-
if (get_active_cp($kind) != $cp) {
32+
if (sapi_windows_cp_get($kind) != $cp) {
5033
die("skip this test expect codepage $cp");
5134
}
5235
}
@@ -121,9 +104,9 @@ function create_data($id, $item = "", $cp = 65001, $utf8 = true)
121104
mkdir($prefix);
122105
}
123106

124-
if (0 === strpos($id, "dir")) {
107+
if (str_starts_with($id, "dir")) {
125108
create_verify_dir($prefix, $item, $cp);
126-
} else if (0 === strpos($id, "file")) {
109+
} else if (str_starts_with($id, "file")) {
127110
/* a bit unhandy, but content can be put from outside, if needed */
128111
create_verify_file($prefix, $item, "dummy content", $cp);
129112
} else {

0 commit comments

Comments
 (0)