Skip to content

Commit 6eb0419

Browse files
qindapaonkh
authored andcommitted
ADDED: Support for Chinese file names in msys2 environment.
The Perl package that comes with MSYS2 calls the Windows narrow character API (ANSI API). This API does not recognize UTF8, but uses the system default code page. (On Chinese Windows this is CP936/GBK). Therefore, the Chinese text in the file name is passed in in the CP936 parameter format, and an error will occur when decoding it as UTF8. In the Chinese environment of msys2, When comparing whether a directory exists, we can only use the cp936 format for comparison, using utf8 will be ineffective.
1 parent ee19e4b commit 6eb0419

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/App/Asciio/Actions/File.pm

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11

22
package App::Asciio::Actions::File ;
33
use utf8;
4-
use Encode;
4+
use Encode qw(decode encode FB_CROAK) ;
5+
6+
use File::Basename ;
57

68
#----------------------------------------------------------------------------------------------
79

8-
use File::Basename ;
10+
sub normalize_file_name
11+
{
12+
my ($file_name) = @_;
13+
return undef unless defined $file_name ;
14+
15+
my $normalized ;
16+
17+
# try UTF-8 decoding
18+
eval { $normalized = decode('UTF-8', $file_name, FB_CROAK) ; 1 ; } and return $normalized ;
19+
20+
# try CP936/GBK decoding
21+
eval { $normalized = decode('cp936', $file_name) ; 1 ; } and return $normalized ;
22+
23+
return $file_name ;
24+
}
925

1026
#----------------------------------------------------------------------------------------------
1127

@@ -93,7 +109,8 @@ return $file_name ;
93109
sub open
94110
{
95111
my ($self, $file_name) = @_ ;
96-
$file_name = decode('utf-8', $file_name);
112+
113+
$file_name = normalize_file_name($file_name);
97114

98115
my $user_answer = '' ;
99116

lib/App/Asciio/Io.pm

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use Data::TreeDumper ;
1212
use File::Slurper qw(read_text write_text read_binary write_binary) ;
1313
use Readonly ;
1414
use Compress::Bzip2 qw(:all :utilities :gzip) ;
15+
use Encode qw(encode);
1516

1617
use Sereal qw(
1718
get_sereal_decoder
@@ -24,6 +25,7 @@ use Sereal qw(
2425

2526
use Sereal::Encoder qw(SRL_SNAPPY SRL_ZLIB SRL_ZSTD) ;
2627

28+
2729
#-----------------------------------------------------------------------------
2830

2931
sub load_file
@@ -62,7 +64,7 @@ if
6264
}
6365
else
6466
{
65-
if(-e $file_name && -s $file_name)
67+
if(file_exists($file_name))
6668
{
6769
my $header_diagram = read_binary($file_name) ;
6870

@@ -369,4 +371,21 @@ return $decoder->decode(decompress($diagram)) ;
369371

370372
#-----------------------------------------------------------------------------
371373

374+
sub file_exists
375+
{
376+
my ($path) = @_ ;
377+
return 0 unless defined $path ;
378+
379+
# try the UTF-8 version
380+
return 1 if -e $path && -s $path ;
381+
382+
# try with CP936 encoded byte version as Windows Chinese API only recognizes GBK sequences
383+
my $gbk = eval { encode('cp936', $path) } ;
384+
return 1 if defined $gbk && -e $gbk && -s $gbk ;
385+
386+
return 0 ;
387+
}
388+
389+
#-----------------------------------------------------------------------------
390+
372391
1 ;

0 commit comments

Comments
 (0)