| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Win32::Codepage; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23035
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
31
|
|
|
5
|
1
|
|
|
1
|
|
1136
|
use Win32::Locale; |
|
|
1
|
|
|
|
|
976
|
|
|
|
1
|
|
|
|
|
551
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $CODEPAGE_REGISTRY_KEY = 'HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/CodePage'; |
|
10
|
|
|
|
|
|
|
my $LANGUAGE_REGISTRY_KEY = 'HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/Language'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Win32::Codepage - get Win32 codepage information |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 LICENSE |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Copyright 2005 Clotho Advanced Media, Inc., |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it |
|
21
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Win32::Codepage; |
|
26
|
|
|
|
|
|
|
print "Current language: " . Win32::Codepage::get_codepage() . "\n"; # e.g. "en-us" |
|
27
|
|
|
|
|
|
|
print "Install language: " . Win32::Codepage::get_install_codepage() . "\n"; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Encode qw(encode); |
|
30
|
|
|
|
|
|
|
my $w32encoding = Win32::Codepage::get_encoding(); # e.g. "cp1252" |
|
31
|
|
|
|
|
|
|
my $encoding = $w32encoding ? Encode::resolve_alias($w32encoding) : ''; |
|
32
|
|
|
|
|
|
|
print $encoding ? encode($string, $encoding) : $string; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module is intended as a companion to Win32::Locale. That module |
|
37
|
|
|
|
|
|
|
offers information about user prefs for language and locale. However, |
|
38
|
|
|
|
|
|
|
Windows has a separate setting for how files and filenames are encoded |
|
39
|
|
|
|
|
|
|
by default, which is specified by the "codepage" (a legacy term from |
|
40
|
|
|
|
|
|
|
DOS days). It is possible to be on a computer whose language, date, |
|
41
|
|
|
|
|
|
|
currency, etc are set to English, but the file contents and filesystem |
|
42
|
|
|
|
|
|
|
names default to SHIFT-JIS (Japanese) encoding. |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
This module offers information about that codepage, which allows your |
|
45
|
|
|
|
|
|
|
Perl code to know what encoding to expect for file names and file |
|
46
|
|
|
|
|
|
|
contents. |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
On Windows XP, you can change the current codepage from the default |
|
49
|
|
|
|
|
|
|
via Control Panel > Regional and Language Settings > Advanced tab. If |
|
50
|
|
|
|
|
|
|
you change it to, say, Japanese and then reboot, the default codepage |
|
51
|
|
|
|
|
|
|
will be cp932, which is Microsoft's version of SHIFT-JIS. This will |
|
52
|
|
|
|
|
|
|
allow non-Unicode Windows applications (like ActiveState Perl) to read |
|
53
|
|
|
|
|
|
|
filenames that contain Japanese characters. If you have files named |
|
54
|
|
|
|
|
|
|
with Japanese characters but your codepage is set to cp1252 |
|
55
|
|
|
|
|
|
|
(Microsoft's version of ISO-latin-1), then the foreign characters |
|
56
|
|
|
|
|
|
|
in the filename appear as C> to Perl. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
If there's a better way around this than messing with codepages, |
|
59
|
|
|
|
|
|
|
PLEASE LET ME KNOW! I hate that I ever had to write this module... |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
L |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
I tried to contact the author of that module to get him to |
|
66
|
|
|
|
|
|
|
extend his distribution to include the codepage functionality, but I |
|
67
|
|
|
|
|
|
|
received no response for seven months. So, I created this module. |
|
68
|
|
|
|
|
|
|
See the RT ticket: L |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=over |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item get_codepage |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Returns the language name for the current codepage language. For |
|
77
|
|
|
|
|
|
|
example C or C. Returns false if the codepage language |
|
78
|
|
|
|
|
|
|
cannot be identified. |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
If this function is passed an argument (not recommended), then it |
|
81
|
|
|
|
|
|
|
returns the language name for the specified language ID instead of the |
|
82
|
|
|
|
|
|
|
system language ID. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub get_codepage |
|
87
|
|
|
|
|
|
|
{ |
|
88
|
0
|
|
0
|
0
|
|
0
|
my $lang = $Win32::Locale::MSLocale2LangTag{ $_[0] || get_ms_codepage() || '' }; |
|
|
|
|
0
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
0
|
return unless $lang; |
|
90
|
0
|
|
|
|
|
0
|
return $lang; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item get_install_codepage |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns the language name for the installed codepage language. This |
|
96
|
|
|
|
|
|
|
is the same as get_codepage(), but refers to the codepage that was the |
|
97
|
|
|
|
|
|
|
default when Windows was first installed. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub get_install_codepage |
|
102
|
|
|
|
|
|
|
{ |
|
103
|
0
|
|
0
|
0
|
|
0
|
my $lang = $Win32::Locale::MSLocale2LangTag{ $_[0] || get_ms_install_codepage() || '' }; |
|
|
|
|
0
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
0
|
return unless $lang; |
|
105
|
0
|
|
|
|
|
0
|
return $lang; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item get_encoding |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Returns an encoding name usable with Encode.pm based on the current |
|
111
|
|
|
|
|
|
|
codepage. For example, C for iso-8859-1 (latin-1) or C |
|
112
|
|
|
|
|
|
|
for Shift-JIS Japanese. Returns false if an encoding cannot be |
|
113
|
|
|
|
|
|
|
identified. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Note: this only returns encoding names that start with C. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub get_encoding |
|
120
|
|
|
|
|
|
|
{ |
|
121
|
1
|
|
0
|
1
|
|
427
|
my $key = _get_codepage_reg_key() || return; |
|
122
|
0
|
|
0
|
|
|
|
my $codepage = $key->GetValue("ACP") || $key->GetValue("OEMCP"); |
|
123
|
0
|
0
|
0
|
|
|
|
return unless $codepage && $codepage =~ m/^[0-9a-fA-F]+$/s; |
|
124
|
0
|
|
|
|
|
|
return "cp".lc($codepage); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item get_ms_codepage |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Returns the numeric language ID for the current codepage language. |
|
130
|
|
|
|
|
|
|
For example C<0x0409> for en-us or C<0x0411> for ja. Returns false if |
|
131
|
|
|
|
|
|
|
the codepage cannot be identified. |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub get_ms_codepage |
|
136
|
|
|
|
|
|
|
{ |
|
137
|
0
|
|
0
|
0
|
|
|
my $key = _get_lang_reg_key() || return; |
|
138
|
0
|
|
|
|
|
|
my $codepage = $key->GetValue("Default"); |
|
139
|
0
|
0
|
0
|
|
|
|
return unless $codepage && $codepage =~ m/^[0-9a-fA-F]+$/s; |
|
140
|
0
|
|
|
|
|
|
return hex($codepage); # from hex string to number |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item get_ms_install_codepage |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Returns the numeric language ID for the installed codepage language. This |
|
146
|
|
|
|
|
|
|
is the same as get_ms_codepage(), but refers to the codepage that was the |
|
147
|
|
|
|
|
|
|
default when Windows was first installed. |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub get_ms_install_codepage |
|
152
|
|
|
|
|
|
|
{ |
|
153
|
0
|
|
0
|
0
|
|
|
my $key = _get_lang_reg_key() || return; |
|
154
|
0
|
|
|
|
|
|
my $codepage = $key->GetValue("InstallLanguage"); |
|
155
|
0
|
0
|
0
|
|
|
|
return unless $codepage && $codepage =~ m/^[0-9a-fA-F]+$/s; |
|
156
|
0
|
|
|
|
|
|
return hex($codepage); # from hex string to number |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Returns the Windows registry entry for codepages |
|
160
|
|
|
|
|
|
|
sub _get_codepage_reg_key |
|
161
|
|
|
|
|
|
|
{ |
|
162
|
|
|
|
|
|
|
my $codekey; |
|
163
|
|
|
|
|
|
|
local $SIG{__DIE__} = 'DEFAULT'; |
|
164
|
|
|
|
|
|
|
eval { |
|
165
|
1
|
|
|
1
|
|
537
|
use Win32::TieRegistry (); |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
166
|
|
|
|
|
|
|
$codekey = Win32::TieRegistry->new($CODEPAGE_REGISTRY_KEY, |
|
167
|
|
|
|
|
|
|
{ Delimiter => "/" } |
|
168
|
|
|
|
|
|
|
); |
|
169
|
|
|
|
|
|
|
}; |
|
170
|
|
|
|
|
|
|
return $codekey; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Returns the Windows registry entry for languages |
|
174
|
|
|
|
|
|
|
sub _get_lang_reg_key |
|
175
|
|
|
|
|
|
|
{ |
|
176
|
|
|
|
|
|
|
my $langkey; |
|
177
|
|
|
|
|
|
|
local $SIG{__DIE__} = 'DEFAULT'; |
|
178
|
|
|
|
|
|
|
eval { |
|
179
|
|
|
|
|
|
|
use Win32::TieRegistry (); |
|
180
|
|
|
|
|
|
|
$langkey = Win32::TieRegistry->new($LANGUAGE_REGISTRY_KEY, |
|
181
|
|
|
|
|
|
|
{ Delimiter => "/" } |
|
182
|
|
|
|
|
|
|
); |
|
183
|
|
|
|
|
|
|
}; |
|
184
|
|
|
|
|
|
|
return $langkey; |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |