#============================================================ # MyApplication2 #============================================================ package MyApplication2; use lib '../lib'; use lib '../lib2.0'; use Exporter; use Common2; @ISA = qw(Exporter Common2); use strict; use English; use File::Path; use File::Basename; use File::Find; use CGI; use Deps; use Utils; use JFile; use IniFile; use GetArg; my $PermitDebugPrint = 1; BEGIN { # $| = 1; open (STDERR, ">&STDOUT"); } #=============================================== # 未定義関数呼び出しの処理 #=============================================== sub AUTOLOAD { my $this = shift; no strict; die "Error!!: Undefined subroutine call: $AUTOLOAD\n"; use strict } #============================================================ # 変数等取得関数 #============================================================ sub PermitDebugPrint { return $PermitDebugPrint; } sub GetApplication { return shift; } sub Application { return shift; } sub App { return shift; } sub SetAppName { my ($this,$p) = @_; return $this->{'AppName'} = $p; } sub GetDocument { return shift->{'Document'}; } sub Document { return shift->GetDocument(); } sub Doc { return shift->GetDocument(); } sub GetIniFile { return shift->{'IniFile'}; } #sub IniFile { return shift->IniFile(); } sub Ini { return shift->IniFile(); } sub Args { return shift->{'Args'}; } sub CGIForm { return shift->{'CGIForm'}; } sub MainWindow { return shift->{'MainWindow'}; } sub mw { return shift->{'MainWindow'}; } sub Usage { return shift->Args()->Usage(); } sub WorkDir { return shift->{'WorkDir'}; } #sub SetWorkDir($path); sub Debug { return shift->{'Debug'}; } sub SetDebug { my ($this, $f) = @_; $f = 0 if(!defined $f); if($f == 1 or $f =~ /on/i) { return $this->{Debug} = 1; } elsif($f =~ /\+/) { return $this->{Debug}++; } elsif($f =~ /-/) { return $this->{Debug}--; } } sub DoConfirm { return shift->{DoConfirm}; } sub PrintLevel { return shift->{PrintLevel}; } sub stdio { return shift->{stdio}; } sub set { my ($this, $key, $val) = @_; return $this->{$key} = $val; } sub get { my ($this, $key, $defval) = @_; return $this->{$key} if(defined $this->{$key}); return $defval; } sub DeleteHTMLFlag { return shift->{DeleteHTMLFlag}; } sub SetDeleteHTMLFlag { my ($this,$f)=@_; return shift->{DeleteHTMLFlag} = $f;} sub RegLF { return shift->{RegLF}; } sub LF { return shift->{LF}; } sub SetLF { my ($this, $LF) = @_; $this->{'LF'} = $LF; $this->{'RegLF'} = Utils::RegExpQuote($LF); return $LF; } sub Space { return shift->{'Space'}; } sub SetSpace { my ($this, $Space) = @_; $this->{'Space'} = $Space; $this->{'RegSpace'} = Utils::RegExpQuote($Space); return $Space; } sub DirectorySeparator { return shift->{'DirectorySeparator'}; } sub OS { return shift->{'OS'}; } sub AppName { return shift->{'AppName'}; } sub Program { my ($this) = @_; return $this->{Program}; } sub SetProgram { my ($this,$p) = @_; return $this->{'Program'} = $p; } sub ProgramPath { return shift->{'ProgramPath'}; } sub SetProgramPath { my ($this,$p) = @_; return $this->{'ProgramPath'} = $p; } sub Version { return shift->{'Version'}; } sub SetVersion { my ($this,$p) = @_; return $this->{'Version'} = $p; } sub AppTitle { return shift->{'Title'}; } sub GetTitle { return shift->{'Title'}; } sub Title { return shift->{'Title'}; } sub SetTitle { my ($this,$p) = @_; return $this->{'Title'} = $p; } sub PrintCharCode { return shift->{'PrintCharCode'}; } sub SetPrintCharCode { my ($this, $c, $SourceCharCode) = @_; $this->{'PrintCharCode'} = $c; if($this->{'stdio'}) { $this->{'stdio'}->Close(); unless( $this->{'stdio'}->Open("con", "rw", $this->{'PrintCharCode'} ) ) { print "Can not open STDIN/STDOUT.\n"; return -1; } $this->{'stdio'}->SetSourceCharCode($SourceCharCode); } return $c; } sub SetSourceCharCode { my ($this, $SourceCharCode) = @_; $this->{SourceCharCode} = $SourceCharCode; $this->{stdio}->SetSourceCharCode($SourceCharCode) if($this->{stdio}); } sub SourceCharCode { return shift->{SourceCharCode}; } sub OSCharCode { return shift->{'OSCharCode'}; } sub SetOSCharCode { my($this,$c)=@_; return $this->{'OSCharCode'}=$c; } sub ProgramCharCode { return shift->{'ProgramCharCode'}; } sub SetProgramCharCode { my($this,$c)=@_; return $this->{'ProrgramCharCode'}=$c; } sub PerlCharCode { return shift->{'PerlCharCode'}; } sub SetPerlCharCode { my($this,$c)=@_; return $this->{'PerlCharCode'}=$c; } sub FileSystemCharCode { return shift->{'FileSystemCharCode'}; } sub SetFileSystemCharCode { my($this,$c)=@_; return $this->{'FileSystemCharCode'}=$c; } sub FileCharCode { return shift->{'FileCharCode'}; } sub SetFileCharCode { my($this,$c)=@_; return $this->{'FileCharCode'}=$c; } sub SQLCharCode { return shift->{'SQLCharCoderCode'}; } sub SetSQLCharCode { my($this,$c)=@_; return $this->{'SQLCharCode'}=$c; } sub WebCharCode { return shift->{'WebCharCode'}; } sub SetWebCharCode { my ($this, $c) = @_; $this->{'WebCharCode'} = $c; $this->SetWebCharSet(GetWebCharSetByCharCode($c)); return $this->{'WebCharCode'}; } sub WebCharSet { my ($App) = @_; #print "C2: ", $App->{WebCharSet}, "
\n"; $App->{WebCharSet} = "iso-8859-1" if(!defined $App->{'WebCharSet'}); return $App->{'WebCharSet'}; } sub SetWebCharSet { my ($this, $c) = @_; return $this->{'WebCharSet'} = $c; } sub GetWebCharSetByCharCode { my ($WebCharCode) = @_; return "iso-2022-jp" if($WebCharCode =~ /^jis$/i); return "euc-jp" if($WebCharCode =~ /^euc$/i); return "iso-8859-1" if($WebCharCode =~ /^en$/i); return "x-$WebCharCode"; } sub GetWebCharSet { my ($Language, $WebCharCode) = @_; return "iso-8859-1" if($Language =~ /English/i); return "iso-2022-jp" if($WebCharCode =~ /^jis$/i); return "euc-jp" if($WebCharCode =~ /^euc$/i); return "iso-8859-1" if($WebCharCode =~ /^en$/i); return "x-$WebCharCode"; } #============================================================ # コンストラクタ、デストラクタ #============================================================ BEGIN { } sub new { my ($module, @args) = @_; #print "module: $module
\n"; my $this = $module; if(!$this or $this !~ /=HASH/) { $this = {}; bless $this; } #print "this: $this
\n"; $this->Initialize(); Common2::new($this, @args); if($this->{OutputMode}) { $this->SetOutputMode($this->{OutputMode}); } if($this->{SourceCharCode}) { $this->SourceCharCode($this->{SourceCharCode}); } return $this; } sub DESTROY { my $this = shift; $this->SaveSetting(); $this->EndPrint(); # $this->SUPER::DESTROY(@_); } sub Initialize { my ($this) = @_; $this->{'IniFileVariables'} = {}; $this->{'IniFileDefaultVariables'} = {}; $this->{'Debug'} = 0; $this->{'DoConfirm'} = 0; #$PrintLevelが大きいほど、情報が詳しくなる $this->{'PrintLevel'} = 0; #出力先: console, HTML $this->{'OutputMode'} = 'console'; # sjis, euc, jis, noconv, utf8 $this->{'OSCharCode'} = Deps::OSCharCode(); $this->{'PrintCharCode'} = Deps::OSCharCode(); $this->{'FileSystemCharCode'} = Deps::FileSystemCharCode(); $this->{'FileCharCode'} = Deps::FileCharCode(); $this->{'PerlCharCode'} = Deps::PerlCharCode(); $this->{'MySQLCharCode'} = Deps::MySQLCharCode(); $this->{'WebCharCode'} = Deps::WebCharCode(); $this->{'WebCharSet'} = Deps::WebCharSet(); $this->{'stdio'} = new JFile; unless( $this->{'stdio'}->Open("con", "rw", $this->{'PrintCharCode'} ) ) { print "Can not open STDIN/STDOUT.\n"; return -1; } $this->SetLF(Deps::LF()); $this->{'DirectorySeparator'} = Deps::DirectorySeparator(); $this->{'OS'} = Deps::OS(); $this->{'Program'} = basename($0); $this->{'StartTime'} = $BASETIME; $this->{'StartDate'} = Utils::BuildDateString($this->{'StartTime'}); $this->{'RemoteIPAddress'} = $ENV{'REMOTE_ADDR'}; return 1; } sub MainLoop { my ($this) = @_; Tk::MainLoop(); } #=============================================== # Application固有共通関数 #=============================================== sub Title { return shift->{Title}; } sub SetTitle { my($App,$t)=@_; return $App->{Title}=$t; } sub StartTime { return shift->{'StartTime'}; } sub StartDate { return shift->{'StartDate'}; } sub RemoteIPAddress { return shift->{'RemoteIPAddress'}; } sub SetCGIForm { my ($this,$form)=@_; return $this->{'CGIForm'} = $form; } sub AddArgument { my ($this, $arg, $explanation, $defval) = @_; $this->{'Args'} = new GetArg($this) unless($this->{'Args'}); my $Args = $this->{'Args'}; $Args->AddArgument($arg, $explanation, $defval); } sub GetFileNameArray { my ($this) = @_; my $Args = $this->{'Args'}; my $files = $Args->FileNameArray(); return @$files; } sub GetArgFileName { my ($this, $idx) = @_; my $Args = $this->{'Args'}; my $files = $Args->FileNameArray(); return $files->[$idx]; } sub GetGetArg { my ($this, $key, $defvalue, $StopFlag) = @_; $this->{'Args'} = new GetArg($this, $StopFlag) unless($this->{'Args'}); return undef unless(defined $key); # $keyが整数の場合、FileName引数を返す if($key =~ /^\d+$/) { my $str = $this->GetArgFileName($key); return $str if(defined $str); } # コマンドライン引数をチェック my $Args = $this->{'Args'}; my $str = $Args->GetGetArg($key); return $str if(defined $str); # $form引数をチェック if($this->CGIForm()) { $str = $this->{'CGIForm'}->param($key); return $str if(defined $str); } return $defvalue; } sub GetArgHash { my ($this) = @_; return $this->Args()->GetArgHash(); } sub ReadArgs { my ($this, $StopFlag, $CheckAllowedArgs, $WebCharCode) = @_; $CheckAllowedArgs = 1 if(!defined $CheckAllowedArgs); $this->{'Args'} = new GetArg($this) unless($this->{'Args'}); my $Args = $this->{'Args'}; $Args->SetApplication($this); $Args->SetCheckAllowedArgs($CheckAllowedArgs); my $ret = $Args->Read(\@ARGV, $StopFlag); return undef if(!$ret); #CGIであれば読み込む if(Utils::IsCGI()) { $this->Args()->SetCGIForm(new CGI); $this->Args()->parseInput($WebCharCode); } my %Arg = $this->GetArgHash(); $this->SetDebug($Arg{DebugMode}); $this->SetLanguage($Arg{Language}); if(Utils::IsCGI()) { $this->SetPrintCharCode($this->WebCharCode()) if($this->WebCharCode()); } return %Arg; } sub AddIniFileVariable { my ($this, $KeyTree, $VarName, $DefVal) = @_; return unless($VarName); my ($section, $key) = ($KeyTree =~ /^\\(.+?)\\(.*)$/); return unless($section); $DefVal = '' unless($DefVal); #print "Set: $KeyTree: $VarName: $DefVal\n"; my $pHash = $this->{'IniFileVariables'}; $this->{'IniFileVariables'} = $pHash = {} if(!defined $pHash); $pHash->{$KeyTree} = $VarName; my $pDefHash = $this->{'IniFileDefaultVariables'}; $this->{'IniFileDefaultVariables'} = $pDefHash = {} if(!defined $pDefHash); $pDefHash->{$KeyTree} = $DefVal; } sub ReadSetting { my ($this) = @_; my $IniFile = $this->{'IniFile'}; unless($IniFile) { print "Error: Can not get IniFile class.\n"; return; } my $pHash = $this->{'IniFileVariables'}; unless($pHash) { print "Error: Can not get IniFile::IniFileVariables.\n"; return; } my $pDefHash = $this->{'IniFileDefaultVariables'}; unless($pDefHash) { print "Error: Can not get IniFile::IniFileDefaultVariables.\n"; return; } return unless($pHash); foreach my $KeyTree (keys %$pHash) { my $AppKey = $pHash->{$KeyTree}; my $DefVal = $pDefHash->{$KeyTree}; next unless($AppKey); my ($section, $key) = ($KeyTree =~ /^\\(.+?)\\(.*)$/); next unless($section); #print "kin: $AppKey: $section: $key\n"; my $val = $IniFile->GetString($section, $key, $DefVal); #print "$KeyTree: $AppKey: $DefVal: $val\n"; $val = '' unless(defined $val); #print "Set $val to $AppKey\n"; $this->{$AppKey} = $val; } return 1; } sub SaveSetting { my ($this) = @_; my $IniFile = $this->{'IniFile'}; return unless($IniFile); my $pHash = $this->{'IniFileVariables'}; return unless($pHash); foreach my $KeyTree (keys %$pHash) { my $AppKey = $pHash->{$KeyTree}; next unless($AppKey); my ($section, $key) = ($KeyTree =~ /^\\(.+?)\\(.*)$/); next unless($section); my $val = $this->{$AppKey}; next unless(defined $val); #print "kout: $AppKey: $section: $key: $val\n"; $IniFile->WriteString($section, $key, $val); #print "Save $val to $section \\ $key\n"; } return 1; } sub OpenIniFile { my ($this, $ProgramPath, $CreateIniFile) = @_; my $ret = $this->{'IniFile'} = new IniFile($ProgramPath, $CreateIniFile); return $ret; } sub ConnectDocument { my ($this, $doc) = @_; $this->{'Document'} = $doc; } sub SpeculateProgramPath { my ($this, $path, $BaseDir) = @_; my $ProgramPath = Deps::SpeculateProgramPath($path, $BaseDir); return $this->SetProgramPath($ProgramPath); } sub SetWorkDir { my ($this, $path) = @_; return $this->{'WorkDir'} = $path if(-d $path); my ($drive, $directory, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($path); return $this->{'WorkDir'} = Deps::MakePath($drive, $directory); } sub SetOutputMode { my ($this, $mode) = @_; $mode = "console" if(!defined $mode or $mode eq '' or $mode =~ /con/i); if($mode =~ /auto/i) { if(Utils::IsCGI()) { $mode = 'HTML'; } else { $mode = 'console'; } } if($mode =~ /HTML/i) { binmode(STDOUT); binmode(STDERR); $this->{DeleteHTMLFlag} = 0; } else { $this->{DeleteHTMLFlag} = 0; # $this->{DeleteHTMLFlag} = 1; } return $this->{OutputMode} = $mode; } sub OutputMode { return shift->{OutputMode}; } sub InitPrint { my($this,@a)=@_; return $this->InitHTML(@a); } sub InitHTML { my ($this, $s, $WebCharSet, $CSS) = @_; $s = $this->Title() if(!$s); my $LinkTarget; if($WebCharSet =~ /^_/) { $LinkTarget = $WebCharSet; $WebCharSet = $this->WebCharSet(); } if($this->OutputMode() =~ /HTML/i and $this->{'HTMLInitialized'} != 1) { Utils::InitHTML($s, $WebCharSet, $LinkTarget, $CSS); $this->{'HTMLInitialized'} = 1; } } sub EndPrint { return shift->EndHTML(); } sub EndHTML { my ($this, $s, $WebCharSet) = @_; if($this->OutputMode =~ /HTML/i) { if($this->{'HTMLInitialized'}) { Utils::EndHTML(); } $this->{'HTMLInitialized'} = 0; } } sub H1 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H1', $s); } sub H2 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H2', $s); } sub H3 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H3', $s); } sub H4 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H4', $s); } sub H5 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H5', $s); } sub H6 { my ($this, $s) = @_; $this->PrintWithHTMLTag('H6', $s); } sub P { my ($this, $s) = @_; $this->PrintWithHTMLTag('P', $s); } sub BR { my ($this, $s) = @_; $s = '' if(!defined $s); if($this->OutputMode =~ /HTML/i) { $s = Utils::ConvertToHTMLString($s); $this->PrintRawHTML("$s
\n"); } else { $this->print($s); } } sub HR { my ($this) = @_; if($this->OutputMode =~ /HTML/i) { $this->PrintRawHTML("
\n"); } } sub CheckBlankString { my ($this, $s, $message) = @_; return 1 if(defined $s and $s ne ''); $this->H2($message); return 0; } sub PrintRawHTML { my ($this, $s) = @_; #print "O: ", $this->OutputMode() , " I: ", $this->{'HTMLInitialized'}, "\n"; if($this->OutputMode() =~ /HTML/i) { if($this->{'HTMLInitialized'} != 1) { $this->InitPrint(); } } my $mode = $this->OutputMode(); $this->{'OutputMode'} = ""; $this->print2(0, $s); $this->{'OutputMode'} = $mode; } sub PrintWithHTMLTag { my ($this, $tag, $s) = @_; if($this->OutputMode =~ /HTML/i) { if($this->{'HTMLInitialized'} != 1) { $this->InitPrint(); } $s = Utils::ConvertToHTMLString($s); $this->PrintRawHTML("<$tag>$s\n"); } else { $this->print($s); } } #sub PrintWithHTMLTag #{ # my ($this, @args) = @_; # return $this->print2(0, @args); #} sub print { my ($this, @args) = @_; if($this->OutputMode() =~ /HTML/i and $this->{HTMLInitialized} != 1) { $this->InitPrint(@args); return $this->print2(1, @args); } else { return $this->print2(0, @args); } } sub print2 { my ($this, $ConvertHTML, @args) = @_; my $RegLF = $this->{'RegLF'}; #print "print: $RegLF\n"; if($ConvertHTML and $RegLF and $RegLF ne '\n') { for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\n/$RegLF/g; } } if($ConvertHTML and $this->{OutputMode} =~ /HTML/i) { for(my $i = 0 ; $i < @args ; $i++) { $args[$i] = Utils::ConvertToHTMLString($args[$i]); } } if($this->{RegSpace} and $this->{RegSpace} ne ' ') { my $sp = $this->{'RegSpace'}; for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\t/$sp$sp$sp$sp/g; $args[$i] =~ s/ /$sp/g; } } if($this->{DeleteHTMLFlag}) { for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\<[^\<\>]+?\>//g; Utils::InvalidateHTMLTags($args[$i]); } } # if(defined $this->{'PrintCharCode'}) { # for(my $i = 0 ; $i < @args ; $i++) { # Jcode::convert(\($args[$i]), $this->{'PrintCharCode'}); # } # } return $this->{'stdio'}->print(@args) if($this->{'stdio'}); return print(@args); } sub printf { my ($this, @args) = @_; my $RegLF = $this->{'RegLF'}; #print "print: $RegLF\n"; if($RegLF and $RegLF ne '\n') { for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\n/$RegLF/g; } } if($this->{RegSpace} and $this->{RegSpace} ne ' ') { my $sp = $this->{'RegSpace'}; for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\t/$sp$sp$sp$sp/g; $args[$i] =~ s/ /$sp/g; } } if($this->{DeleteHTMLFlag}) { for(my $i = 0 ; $i < @args ; $i++) { $args[$i] =~ s/\<[^\<\>]+?\>//g; Utils::InvalidateHTMLTags($args[$i]); } } # if(defined $this->{PrintCharCode}) { # for(my $i = 0 ; $i < @args ; $i++) { # Jcode::convert(\($args[$i]), $this->{'PrintCharCode'}); # } # } return $this->{'stdio'}->printf(@args) if($this->{'stdio'}); return printf(@args); } sub DebugPrint { my ($this, @args) = @_; return if(!$PermitDebugPrint); return $this->print(@args) if($this->{'Debug'}); return ''; } #============================================================ # 初期化ファイルの設定 #============================================================ sub ConfigureIniFileVariables { my ($this) = @_; # my $Args = $this->Args(); # $App->AddIniFileVariable("\\Preferences\\EditorPath", "EditorPath", "notepad.exe"); # $App->ReadSetting(); return 1; } #======================================================== # 言語設定関係 #======================================================== sub SetLanguage { my ($App, $l) = @_; $App->{Language} = 'English' if(!defined $App->{Language}); return $App->{Language} if(!defined $l or $l eq ''); $App->{Language} = $l; $App->Args()->{'Language'} = $l if(defined $App->Args()); $App->SetWebCharSet(GetWebCharSet($l, $App->WebCharCode())); return $App->{Language} = $l; } sub Language { my ($App) = @_; $App->{Language} = 'English' if(!defined $App->{Language} or $App->{Language} eq ''); return $App->{Language}; } sub LanguageCode { my ($App) = @_; my %code = ( 'English', 'en', 'Japanese', 'jp', 'Korean', 'kr', 'Chinese', 'cn', 'Germany', 'de', 'French', 'fr', ); my $l = $App->Language(); my $c = $code{$l}; return 'Invalid Language' if(!defined $c or $c eq ''); return $c; } sub PrintWithReplaced { my ($App, $file, $target, @array) = @_; my $charcode = $App->PrintCharCode(); for(my $i = 0 ; $i < @array ; $i++) { Jcode::convert(\($array[$i]), $charcode); } return Utils::PrintWithReplaced($file, $target, @array); } sub PrintWithReplacedForHTML { my ($App, $file, $target, @array) = @_; my $charcode = $App->PrintCharCode(); for(my $i = 0 ; $i < @array ; $i++) { Jcode::convert(\($array[$i]), $charcode); } return Utils::PrintWithReplaced($file, $target, @array); } sub BuildDateString { my ($App, $d) = @_; return Utils::BuildDateString($d, $App->Language()); } 1;