#======================================================== # VCF #======================================================== package VCF; use Common; @ISA = qw(Common); use strict; use MyCharConvEUCJP; #============================================================ # 変数等取得関数 #============================================================ #============================================================ # 静的関数 #============================================================ my $TemporaryCharCode = 'euc-jp'; my $SourceCharCode = ''; #============================================================ # コンストラクタ、デストラクタ #============================================================ BEGIN { } sub new { my ($module) = @_; my $this = {}; bless $this; return $this; } sub DESTROY { my $this = shift; } #============================================================ # 一般関数 #============================================================ sub Read { my ($this, $App, $VCFFile, $sourcecharcode, $charcode) = @_; my $debug = 0; if($sourcecharcode) { $SourceCharCode = $sourcecharcode; } else { $SourceCharCode = ''; } my $in = JFile->new($VCFFile, 'r'); if(!$in) { $App->H2("Error in SearchAddressBook::SearchVCF: Can not read [$VCFFile].\n"); return; } my @list; my ($key, $val); my $count = 0; while(1) { my $line = $in->ReadLine(); last if($in->eof()); Utils::DelSpace($line); my $Name; my $pHash = {}; if($line =~ /^BEGIN:\s*VCARD$/i) { ($key, $val) = ('', ''); while(!$in->eof()) { my $line2 = $in->ReadLine(); Utils::DelSpace($line2); #print "l[$line2]\n"; if($line2 =~ /^END:\s*VCARD$/i) { #print "***end found\n"; if($key ne '') { #その前にkeyが設定されていたら、hashに入れて更新 ($key, $val) = $this->DecodeVal($App, $key, $val, $charcode); $Name = $val if($key eq 'FN'); $pHash->{$key} = $val; $pHash->{sn} = $count; $pHash->{all} .= ":/:$val"; $list[$count] = $pHash; # push(@list, $pHash); $App->print("[$Name] 1 $key: $val\n") if($debug); ($key, $val) = ('', ''); $Name = $val if($key eq 'FN'); $count++; } $pHash = {}; last; } elsif($line2 =~ /^(.*?):(.*)$/) { if($key ne '') { #その前にkeyが設定されていたら、hashに入れて更新 ($key, $val) = $this->DecodeVal($App, $key, $val, $charcode); $Name = $val if($key eq 'FN'); $pHash->{$key} = $val; $pHash->{all} .= ":/:$val"; $App->print("[$Name] 2 $key: $val\n") if($debug); ($key, $val) = ('', ''); $Name = $val if($key eq 'FN'); } else { #新規にkeyを設定 $key = $1; $val = $2; ($key, $val) = $this->DecodeVal($App, $key, $val, $charcode); #print "[$Name] * $key: $val\n"; } } elsif($line2 =~ /^\s+(.*)$/) { $val .= &DecodeVal($1); } else { if(0) { if($key ne '') { #その前にkeyが設定されていたら、hashに入れて更新 ($key, $val) = $this->DecodeVal($App, $key, $val, $charcode); $Name = $val if($key eq 'FN'); $pHash->{$key} = $val; $pHash->{sn} = $count; $pHash->{all} .= ":/:$val"; $list[$count] = $pHash; # push(@list, $pHash); $App->print("[$Name] 3 $key: $val\n") if($debug); ($key, $val) = ('', ''); $Name = $val if($key eq 'FN'); $count++; } } } } } } $in->Close(); return \@list; } sub DecodeVal { my ($this, $App, $key, $val, $charcode) = @_; my @a = Utils::Split(";", $key); return ($key, $val) if(@a <= 1); $key = $a[0]; my %h; for(my $i = 0 ; $i < @a ; $i++) { if($a[$i] =~ /^(.*?)=(.*)$/) { my $key = $1; my $val = $2; $h{$key} = $val; } } if($h{ENCODING} =~ /^QUOTED-P/i) { $val = Utils::QPDecode($val); } my $cc = lc Jcode::getcode($val); if(!$SourceCharCode and ($cc ne '' and $cc ne 'ascii' and $cc ne 'bin') ) { $SourceCharCode = $cc; } if($h{CHARSET}) { Jcode::convert(\$val, $TemporaryCharCode, $h{CHARSET}); # $val = Jcode->new($val)->h2z(); # Jcode::convert(\$val, $ScriptCodeCharCode, $TemporaryCharCode); # $val = Jcode->new($val)->tr('ア-ン', 'あ-ん') . ''; # $val = $CharConv->HankakuKana2Zenkaku($val, $SourceCharCode); $val = Jcode->new($val)->h2z(); # $val = $CharConv->Hankaku2Zenkaku($val, $SourceCharCode, 1); Jcode::convert(\$val, $charcode, $TemporaryCharCode) if($charcode); } return ($key, $val); } 1;