-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathkh_jchar.pm
390 lines (332 loc) · 7.67 KB
/
kh_jchar.pm
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package kh_jchar;
use strict;
use vars qw($converter);
my %char_code = ();
if (eval 'require Encode::EUCJPMS'){
$char_code{euc} = 'eucJP-ms';
$char_code{sjis} = 'cp932';
} else {
$char_code{euc} = 'euc-jp';
$char_code{sjis} = 'cp932';
}
BEGIN{
if (eval 'require NKF'){
$converter = 'nkf';
}
elsif( $] > 5.008 ){
require Encode;
$converter = 'encode';
} else {
use Jcode;
$converter = 'jcode';
}
# print "Jcode conv: $converter\n";
}
# ファイル丸ごと変換
sub to_euc{
my $sjistoeuc = $_[1];
my $temp_file = 'temp.txt';
while (-e $temp_file){
$temp_file .= '.tmp';
}
#print "kh_jachar temp-file: $temp_file\n";
open (EUC,"$sjistoeuc")
or gui_errormsg->open(type => 'file',thefile => "$sjistoeuc");
open (TEMP,">$temp_file")
or gui_errormsg->open(type => 'file',thefile => "$temp_file");
my $n = 0; my $temp = '';
while (<EUC>){
$temp .= $_;
if ($n == 1000){
$temp = kh_jchar->s2e($temp);
print TEMP "$temp";
$n = 0; $temp = '';
}
++$n;
}
if ($temp){
$temp = kh_jchar->s2e($temp);
print TEMP "$temp";
}
close (EUC);
close (TEMP);
unlink ("$sjistoeuc");
rename ("$temp_file","$sjistoeuc");
}
sub to_sjis{
my $sjistoeuc = $_[1];
my $temp_file = 'temp.txt';
while (-e $temp_file){
$temp_file .= '.tmp';
}
#print "kh_jachar temp-file: $temp_file\n";
open (EUC,"$sjistoeuc")
or gui_errormsg->open(type => 'file',thefile => "$sjistoeuc");
open (TEMP,">$temp_file")
or gui_errormsg->open(type => 'file',thefile => "$temp_file");
my $n = 0; my $temp = '';
while (<EUC>){
$temp .= $_;
if ($n == 1000){
$temp = kh_jchar->e2s($temp);
print TEMP "$temp";
$n = 0; $temp = '';
}
++$n;
}
if ($temp){
$temp = kh_jchar->e2s($temp);
print TEMP "$temp";
}
close (EUC);
close (TEMP);
unlink ("$sjistoeuc");
rename ("$temp_file","$sjistoeuc");
}
# ファイルの文字コードを判別
sub check_code{
my $the_file = $_[1];
my $silent = $_[2];
my $lines = $_[3];
$lines = 1000 unless $lines;
if ( defined($::project_obj) ){
my $chk = $::project_obj->assigned_icode;
if (
( $::project_obj->file_target eq $the_file )
&& ( $chk )
) {
return $chk;
}
}
print "Checking icode (jp1)... " unless $silent;
open (TEMP,$the_file)
or gui_errormsg->open(type => 'file',thefile => $the_file);
my $n = 0;
my $t;
while (<TEMP>){
$_ =~ s/\x0D\x0A$|\x0D$|\x0A$/\n/;
chomp;
next unless length($_);
$t .= $_;
++$n;
last if $n > $lines;
}
close (TEMP);
#print "checking icode...(icode)\n";
use Jcode;
my $icode = Jcode->new($t)->icode;
$icode = 'euc' if $icode eq 'euc-jp';
$icode = 'euc' if $icode eq 'eucJP-ms';
$icode = 'sjis' if $icode eq 'shiftjis';
$icode = 'sjis' if $icode eq 'cp932';
$icode = 'jis' if $icode eq '7bit-jis';
print "$icode\n" unless $silent;
return $icode;
}
sub check_code2{
my $the_file = $_[1];
my $silent = $_[2];
my $lines = $_[3];
$lines = 1000 unless $lines;
if ( defined($::project_obj) ){
my $chk = $::project_obj->assigned_icode;
if (
( $::project_obj->file_target eq $the_file )
&& ( $chk )
) {
return $chk;
}
}
print "Checking icode (jp2)... " unless $silent;
open (TEMP,$the_file)
or gui_errormsg->open(type => 'file',thefile => $the_file);
my $n = 0;
my $t;
while (<TEMP>){
$_ =~ s/\x0D\x0A$|\x0D$|\x0A$/\n/;
chomp;
next unless length($_);
$t .= $_;
++$n;
last if $n > $lines;
}
close (TEMP);
#print "checking icode...(icode)\n";
use Jcode;
my $icode = Jcode->new($t)->icode;
my %char_code = ();
$char_code{shiftjis} = 'cp932';
$char_code{sjis} = 'cp932';
if (eval 'require Encode::EUCJPMS'){
$char_code{euc} = 'eucJP-ms';
} else {
$char_code{euc} = 'euc-jp';
}
$icode = $char_code{$icode} if $char_code{$icode};
print "$icode\n" unless $silent;
return $icode;
}
sub check_code3{
my $the_file = $_[1];
my $silent = $_[2];
my $lines = $_[3];
$lines = 1000 unless $lines;
print "Checking icode (jp3)... " unless $silent;
open (TEMP,$the_file)
or gui_errormsg->open(type => 'file',thefile => $the_file);
my $n = 0;
my $t;
while (<TEMP>){
$_ =~ s/\x0D\x0A$|\x0D$|\x0A$/\n/;
chomp;
next unless length($_);
$t .= $_;
++$n;
last if $n > $lines;
}
close (TEMP);
my @candi = (
$char_code{euc},
'cp932',
'7bit-jis'
);
if ($^O eq 'darwin'){
push @candi, 'MacJapanese';
}
use Encode::Guess;
my $enc = guess_encoding($t, @candi);
print ref $enc ? $enc->name : $enc unless $silent;
print "\n" unless $silent;
if (ref $enc){
$enc = $enc->name;
} elsif ($enc =~ /MacJapanese/ && $^O eq 'darwin') {
$enc = 'MacJapanese';
} elsif ($enc =~ /utf8/i){
$enc = 'utf8';
} elsif ($enc =~ /cp932/){
$enc = 'cp932';
} elsif ($enc =~ /euc/){
$enc = $char_code{euc};
} elsif ($enc =~ /^No / ){
warn("\nFailed to guess encoding of the text.\nMaybe, you need to clean up your data...\n");
$enc = 'utf8';
} else {
die("something wrong with icode! $enc");
}
return $enc;
}
# ファイルの文字コードを判別(英語)
sub check_code_en{
my $the_file = $_[1];
my $silent = $_[2];
my $lines = $_[3];
$lines = 50000 unless $lines;
print "Checking icode (en)... " unless $silent;
open (TEMP,$the_file)
or gui_errormsg->open(type => 'file',thefile => $the_file);
my $n = 0;
my $t;
while (<TEMP>){
$t .= $_;
++$n;
last if $n > $lines;
}
close (TEMP);
#use Devel::Size qw(size total_size);
#print size($t);
use Encode::Guess;
my $enc = guess_encoding($t, qw/latin1 cp1252/);
print ref $enc ? $enc->name : $enc unless $silent;
print "\n" unless $silent;
if (ref $enc){
$enc = $enc->name;
} elsif ($enc =~ /utf8/i) {
$enc = 'utf8';
} elsif ($enc =~ /cp1252/){
$enc = 'cp1252';
} elsif ($enc =~ /latin1/){
$enc = 'latin1';
} elsif ($enc =~ /^No / ){
warn("\nFailed to guess encoding of the text: $enc\n\nMaybe, you need to clean up your data...\n");
$enc = 'utf8';
} else {
warn("Something wrong with text encoding: $enc\n\nPlease make a UTF-8 text without any non-printing characters or control characters.");
$enc = 'utf8';
}
return $enc;
}
# ファイルの文字コードを判別(対応コードすべて)
sub check_code_all{
my $the_file = $_[1];
my $silent = $_[2];
my $lines = $_[3];
$lines = 50000 unless $lines;
print "Checking icode (en)... " unless $silent;
open (TEMP,$the_file)
or gui_errormsg->open(type => 'file',thefile => $the_file);
my $n = 0;
my $t;
while (<TEMP>){
$t .= $_;
++$n;
last if $n > $lines;
}
close (TEMP);
#use Devel::Size qw(size total_size);
#print size($t);
use Encode::Guess;
my $enc = guess_encoding($t, qw/cp932 euc-jp ISO-2022-JP latin1 cp1252/);
print ref $enc ? $enc->name : $enc unless $silent;
print "\n" unless $silent;
if (ref $enc){
$enc = $enc->name;
} elsif ($enc =~ /utf8/) {
$enc = 'utf8';
} elsif ($enc =~ /euc-jp/) {
$enc = 'euc-jp';
} elsif ($enc =~ /cp932/) {
$enc = 'cp932';
} elsif ($enc =~ /ISO-2022-JP/){
$enc = 'ISO-2022-JP';
} elsif ($enc =~ /cp1252/){
$enc = 'cp1252';
} elsif ($enc =~ /latin1/){
$enc = 'latin1';
} elsif ($enc =~ /^No / ){
warn("\nFailed to guess encoding of the text.\nMaybe, you need to clean up your data...\n");
$enc = 'utf8';
} else {
die("something wrong with icode! $enc");
}
return $enc;
}
# 文字列変換
sub s2e{
my $conv = '_s2e_'.$kh_jchar::converter;
kh_jchar->$conv($_[1]);
}
sub _s2e_nkf{
return NKF::nkf('-e -S',$_[1]);
}
sub _s2e_encode{
Encode::from_to($_[1],$char_code{sjis},$char_code{euc});
return $_[1];
}
sub _s2e_jcode{
return Jcode->new($_[1],'sjis')->euc;
}
sub e2s{
my $conv = '_e2s_'.$kh_jchar::converter;
kh_jchar->$conv($_[1]);
}
sub _e2s_nkf{
return NKF::nkf('-s -E',$_[1]);
}
sub _e2s_encode{
Encode::from_to($_[1],$char_code{euc},$char_code{sjis});
return $_[1];
}
sub _e2s_jcode{
return Jcode->new($_[1],'euc')->sjis;
}
1;