package Deps; use Common; @ISA = qw(Common); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath MakePath2 RegExpQuote); use strict; #if($^O eq 'MSWin32') { use Cwd; #} use File::Path; use File::Basename; use File::Find; my $OS = $^O; if($OS eq 'MSWin32') { eval('use Win32;'); } #=============================================== # デバッグ関係変数 #=============================================== my $Debug = 0; #=============================================== # スクリプト大域変数 #=============================================== my $LF = "\n"; my $DirectorySeparator = "\\"; my $RegSep = "\\\\"; #my $RegSep = "\\\\"; #=============================================== # 文字コード関係変数 #=============================================== # sjis, euc, jis, noconv, utf8 my $OSCharCode = 'sjis'; my $PrintCharCode = 'sjis'; my $FileSystemCharCode = 'sjis'; my $FileCharCode = 'sjis'; my $PerlCharCode = undef; #my $PerlCharCode = "utf8"; my $MySQLCharCode = 'sjis'; my $WebCharCode = 'sjis'; my $WebCharSet = 'x-sjis'; if($OS eq 'MSWin32') { } elsif($OS eq 'linux') { $DirectorySeparator = "/"; $RegSep = "\\/"; $OSCharCode = 'utf8'; $PrintCharCode = 'utf8'; $FileSystemCharCode = 'utf8'; $FileCharCode = 'utf8'; } if($ENV{'FILECHARCODE'}) { $FileCharCode = $ENV{'FILECHARCODE'}; $OSCharCode = $FileCharCode; } sub OS { return $OS; } sub LF { return $LF; } sub DirSep { return $DirectorySeparator; } sub DirectorySeparator { return $DirectorySeparator; } sub RegDirSep { return $RegSep; } sub RegDirectorySeparator { return $RegSep; } sub PrintCharCode { return $PrintCharCode; } sub OSCharCode { return $OSCharCode; } sub FileSystemCharCode { return $FileSystemCharCode; } sub FileCharCode { return $FileCharCode; } sub PerlCharCode { return $PerlCharCode; } sub SetPerlCharCode { my ($cc)=@_; return $PerlCharCode = $cc; } sub MySQLCharCode { return $MySQLCharCode; } sub WebCharCode { return $WebCharCode; } sub WebCharSet { return $WebCharSet; } BEGIN { } sub Sleep { my ($sec) = @_; if($OS eq 'MSWin32') { Win32::Sleep($sec * 1000); # in msec } else { sleep($sec); } } sub GetTime { if($OS eq 'MSWin32') { return Win32::GetTickCount() * 0.001; # sec } else { return time(); } } sub CreateDirecotry { my ($dir) = @_; return &CreateDirectory($dir); } sub CreateDirectory { #$Debug=1; my ($dir) = @_; my $directorysep = $DirectorySeparator; $dir =~ s/\\/\//g; if($Debug) { print "CreateDirectory: $dir\n"; } # my $IsHeadSep = 0; # $IsHeadSep = 1 if($dir =~ /^\//); my $IsRelative = 1; $IsRelative = 0 if($dir =~ /^[A-Za-z]:\// or $dir =~ /^\//); #print "IsRelative: $IsRelative\n"; my @eachpath = split(/\//, $dir); my $i = 0; my $path = ""; if($eachpath[$i] =~ /^[A-Za-z]:/) { $path = $eachpath[$i]; $i++; } $path .= $directorysep unless($IsRelative); for( ; $i < @eachpath ; $i++) { $path .= $eachpath[$i] . $directorysep; if($Debug) { print "path: $path\n"; } next if(-d $path); if(-f $path) { print "Can not create [$path]: $path is a file.\n"; return; } if($Debug) { print " Create $path [$directorysep]\n"; } return 0 unless(mkdir($path)); } return 1; } sub ExecBackground { my ($cmdline, $startapp) = @_; # return open(OUT351301463, "|$cmdline"); $startapp = $ENV{'StartApp'} unless($startapp); #print "OS: $OS\n"; if($OS eq 'MSWin32') { # use Win32::Process; # use Win32; # my $ProcessObj; # my ($cmd, $arg) = ($cmdline =~ /^(\S*)\s+(.*)$/); # $cmd = "$cmd.exe" if($cmd !~ /\.(exe|bat|com)$/i); #print "cmd=[$cmd] [$arg]\n"; #Win32::Spawn # my $ret = Win32::Process::Create($ProcessObj, # $cmd, # $arg, # 0, # NORMAL_PRIORITY_CLASS, # "."); #print "ret=$ret\n"; # return $ret; #use Win32::FileOp qw(ShellExecute); # my $ret = ShellExecute($cmdline); #return $ret; unless($startapp) { $cmdline = "cmd.exe /C $cmdline"; $startapp = "start"; } $startapp = "\"$startapp\"" if($startapp =~ /\s/); $cmdline = "$startapp $cmdline"; } else { $cmdline = "$cmdline&"; } print "Utils::ExecBackground: [cmd: $cmdline]\n"; my $ret = system($cmdline); unless($ret / 256 <= 32) { return 0; } return $ret } sub SpeculateProgramPath { my ($path, $BaseDir) = @_; # $pathがフルパスの場合 my ($drive, $directory, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); if($directory and $directory =~ /^[\\\/]/) { # if($directory and $directory !~ /^\./) { #print "Fullpath: $path [$directory]\n"; return $path; } # if($directory and $directory =~ /\//); # カレントディレクトリィを探す my $cwd = GetWorkingDirectory(); my $s = $path; $s =~ s/^\.\///; $s = MakePath($cwd, $s); if(-e $s) { #print "+cwd: $s\n"; return $s; } # $BaseDirを探す $s = "$BaseDir${DirectorySeparator}$path"; return $s if(-e $s); # 見つからない場合 return undef; } sub AddPath { my ($path) = @_; return AddPathLinux($path) if($OS eq 'linux'); return AddPathMSWin32($path); } sub AddPathLinux { my ($path) = @_; my @s = split(/:/, $ENV{'PATH'}); my $IsFound = 0; for(my $i = 0 ; $i < @s ; $i++) { if($s[$i] eq $path) { $IsFound = 1; last; } } unless($IsFound) { $ENV{'PATH'} = "$path:" . $ENV{'PATH'}; } return 1; } sub AddPathMSWin32 { my ($path) = @_; my @s = split(/;/, $ENV{'PATH'}); $path = lc $path; my $IsFound = 0; for(my $i = 0 ; $i < @s ; $i++) { if(lc $s[$i] eq $path) { $IsFound = 1; last; } } unless($IsFound) { $ENV{'PATH'} = "$path;" . $ENV{'PATH'}; } return 1; } sub RegExpQuote { my($str) = (@_); $str =~ s/\\/\\\\/g; $str =~ s/\//\\\//g; $str =~ s/\(/\\\(/g; $str =~ s/\)/\\\)/g; $str =~ s/\[/\\\[/g; $str =~ s/\]/\\\]/g; $str =~ s/\^/\\\^/g; $str =~ s/\$/\\\$/g; $str =~ s/\*/\\\*/g; $str =~ s/\?/\\\?/g; $str =~ s/\+/\\\+/g; $str =~ s/\./\\\./g; return $str; } #sub MakePath #{ # my ($dir, $fname, $DoTerminate) = (@_); # $DoTerminate = 0 unless(defined $DoTerminate); # return Utils::MakePath($dir, $fname, $DirectorySeparator, $DoTerminate); #} sub MakePath2 { my ($dir, $pfnames, $DoTerminate, $QuotationMode) = @_; $QuotationMode = '' if(!defined $QuotationMode); return $dir if(!defined $pfnames or @$pfnames == 0); my $path = &MakePath($dir, $pfnames->[0], $DoTerminate); for(my $i = 1 ; $i < @$pfnames ; $i++) { $path = &MakePath($path, $pfnames->[$i], $DoTerminate, ""); } if($QuotationMode eq 'auto') { $path = "\"$path\"" if($path =~ /\s/); } if($QuotationMode eq 'quote') { $path = "\"$path\""; } return $path; } sub MakePath { my ($dir, $fname, $DoTerminate, $QuotationMode) = (@_); $QuotationMode = '' if(!defined $QuotationMode); if(ref $fname eq 'ARRAY') { return &MakePath2($dir, $fname, $DoTerminate); } my $separator = $DirectorySeparator; my $RegSep = &RegExpQuote($separator); my $IsHeadSep = 0; #print "[$dir] ($fname) : $RegSep : "; # $fname = '' if(!defined $fname); $fname =~ s/^$RegSep//; $fname =~ s/$RegSep$//; #print "($fname)\n"; if(!$dir) { $dir = $fname; } elsif($dir =~ /$RegSep$/) { $dir .= $fname; } else { $dir .= $separator . $fname; } if($DoTerminate) { unless($dir =~ /$RegSep$/) { $dir .= $separator; } } else { if($dir ne '/' and $dir =~ /$RegSep$/) { $dir =~ s/$RegSep$//; } } if($QuotationMode eq 'auto') { $dir = "\"$dir\"" if($dir =~ /\s/); } if($QuotationMode eq 'quote') { $dir = "\"$dir\""; } return $dir; } sub ReplaceFileName { my ($path, $newfilename) = @_; my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); return Deps::MakePath("$drive$dir", $newfilename); } sub ReplaceExtension { my ($path, $newext) = @_; my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); #print "drive: $drive\n"; #print "dir: $dir\n"; #print "filebody: $filebody\n"; $newext = ".$newext" unless($newext =~ /^\./); if($dir eq './' or $dir eq '.\\') { return "$filebody$newext"; } return MakePath("$drive$dir", "$filebody$newext"); } sub GetWorkingDirectory { my $pd; #if($^O ne 'MSWin32') { # $pd = `cd`; # chomp($pd); # $pd =~ s:\\:/:g ; #} #else { $pd = cwd(); #} #セパレータをOSのものに置換する $pd =~ s/\//$DirectorySeparator/g if($DirectorySeparator ne '/'); return $pd; } #sub cwd { return GetWorkingDirectory(); } sub ExtractExtention { my ($path) = @_; return $path if(-d $path); my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); return $ext; } sub ExtractFileBody { my ($path) = @_; return $path if(-d $path); my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); return $filebody; } sub ExtractFileName { my ($path) = @_; my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); return $filename; } sub ExtractDirectory { my ($path) = @_; return $path if(-d $path); my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); #print "path[$path] [$drive][$dir]\n"; my $d = ($drive =~ /[\/\\]$/ or $drive eq '')? "$drive$dir" : "$drive$DirectorySeparator$dir"; $d =~ s/\\\\/\\/g; $d =~ s/\/\//\//g; # my $d = MakePath($drive, $dir); #print "d[$d]\n"; return $d; } sub GetLastDirectory { my ($path) = @_; my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = SplitFilePath($path); return $lastdir; } sub SplitFilePath { my ($path) = @_; return () if($path eq ''); my $code = Jcode::getcode($path); if($code =~ /jis/i) { Jcode::convert(\$path, "euc"); } my ($drive, $dir, $filename, $ext, $lastdir, $filebody); ($drive) = ($path =~ /^([a-zA-Z]:)/); $drive = "" unless(defined $drive); #セパレータをOSのものに置換する # (commonダイアログなどで取得したパスのセパレータは'/'になっている)) $path =~ s/\//$DirectorySeparator/g if($DirectorySeparator ne '/'); #print "Path: $path\n"; #ファイル名のあるパスの場合 if($path !~ /$RegSep$/) { my @filenames = fileparse($path, "\.[^\.]+"); #print "Path: $path\n"; #print " 0: $filenames[0] 1: $filenames[1] 2: $filenames[2]\n"; if($filenames[0] eq '') { $filenames[0] = $filenames[2]; $filenames[2] = ''; } $filenames[1] =~ s/^[a-zA-Z]://; $lastdir = $dir = $filenames[1]; $lastdir =~ s/$RegSep$//; #print "dir: $lastdir\n"; ($lastdir) = ($lastdir =~ /$RegSep([^$RegSep]+)$/); # ($lastdir) = ($lastdir =~ /\/([^$RegSep]+)$/); #print "lastdir: $lastdir\n"; $ext = $filenames[2]; # $ext = '' unless($ext); # $ext =~ s/^\.//; $filename = $filenames[0]; $filename .= "$ext" if($ext ne ''); #print "filename: $filename\n"; $filebody = $filenames[0]; } #ディレクトリィ名のみの場合 else { $path =~ s/^[a-zA-Z]://; $path =~ s/$RegSep$//; $dir = $path; #print "Dir: $dir\n"; ($lastdir) = ($dir =~ /$RegSep([^$RegSep]+)$/); #print "Last: $lastdir\n"; $filename = ''; $filebody = ''; $ext = ''; } if($code =~ /jis/i) { Jcode::convert(\$dir, $code, "euc"); Jcode::convert(\$filename, $code, "euc"); Jcode::convert(\$ext, $code, "euc"); Jcode::convert(\$lastdir, $code, "euc"); Jcode::convert(\$filebody, $code, "euc"); } return ($drive, $dir, $filename, $ext, $lastdir, $filebody); } 1;