Contents ::
|
|
package k2;
;;;;;;;;;; use Cwd;
use Switch;
use Time::Local;
use Fcntl;
use File::Find;
use File::Copy;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sub DirSize{
my ($dir, $sw, $siz) = @_;
$$siz = 0;
$sw = MinMax($sw, 1, 2);
if(isExistsFile($dir)){
find(sub {$$siz += -s if -f}, $dir);
$$siz = ($sw == 2) ? Ceil($$siz / 1024) : $$siz;
}
return($$siz);
}
sub FileSize{
my ($fl, $sw, $siz) = @_;
$$siz = 0;
$sw = MinMax($sw, 1, 2);
if(isExistsFile($fl)){
$$siz = (-s $fl);
$$siz = ($sw == 2) ? Ceil($$siz / 1024) : $$siz;
}
return($$siz);
}
sub FilesInDir{
my ($dir, $fl) = @_;
@$fl = ();
if(RightStr($dir, 1) ne '/'){
$dir .= '/';
}
if(isExistsDir($dir)){
if(opendir(DP, "$dir")){
foreach(readdir(DP)){
next if($_ =~ /^\.{1,2}$/);
push(@$fl, $_)if(isExistsFile($dir.$_));
}
closedir(DP);
}
}
return(@$fl);
}
sub FilesInDir1{
my ($dir, $fl, $sw) = @_;
@$fl = ();
if(RightStr($dir, 1) ne '/'){
$dir .= '/';
}
if(isExistsDir($dir)){
if(opendir(DP, $dir)){
foreach(grep(!/^\.+/, readdir(DP))){
if(-d "$dir/$_"){FilesInDir1("$dir$_", \@$fl, 1); next}
if($sw){
unshift(@$fl, "$dir$_");
}else{
push(@$fl, "$dir$_");
}
}
closedir(DP);
}
}
return(@$fl);
}
sub DirsInDir{
my ($dir, $dirs) = @_;
@$dirs = ();
if(RightStr($dir, 1) ne '/'){
$dir .= '/';
}
if(isExistsDir($dir)){
if(opendir(DP, $dir)){
foreach(readdir(DP)){
next if($_ =~ /^\.{1,2}$/);
push(@$dirs, $_)if(isExistsDir($dir.$_));
}
closedir(DP);
}
}
return(@$dirs);
}
sub FilesMatch{
my ($match, $fl) = @_;
@$fl = glob($match);
return(@$fl);
}
sub FileDateTime{
my ($sw, $fl) = @_;
my $ret = '0';
if(isExistsFile($fl)){
my @tm = stat($fl);
@tm = localtime(timelocal(localtime($tm[9])));
if($sw){
$ret = sprintf("%4d/%02d/%02d %02d:%02d:%02d",
$tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2], $tm[1], $tm[0]);
}else{
$ret = sprintf("%4d%02d%02d%02d%02d%02d",
$tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2], $tm[1], $tm[0]);
}
}
return($ret);
}
sub FileLastSave{
my ($fl) = @_;
my $ret = (isExistsFile($fl)) ? (-M $fl) : 0;
return($ret);
}
;;;sub _cutoff_Path{
my ($pth) = @_;
my ($dir, $fil, $fnm, $ext);
if($pth =~ /(.*)\/(.*)/){
$dir = $1.'/';
$fil = $2;
}else{
$dir = './';
$fil = $pth;
}
if($fil =~ /(.*)\.(.*)/){
$fnm = $1;
$ext = $2;
}else{
$fnm = $fil;
$ext = "";
}
return($dir, $fnm, $ext);
}
sub ExtractFilePath{
my ($pth) = @_;
my ($dir, $fnm, $ext) = _cutoff_Path($pth);
return($dir);
}
sub ExtractFileName{
my ($pth) = @_;
my ($dir, $fnm, $ext) = _cutoff_Path($pth);
return($fnm);
}
sub ExtractFileExt{
my ($pth) = @_;
my ($dir, $fnm, $ext) = _cutoff_Path($pth);
return($ext);
}
sub ExtractFileOnly{
my ($pth) = @_;
my $ext = ExtractFileExt($pth);
$pth = ($ext) ? ExtractFileName($pth).'.'.$ext : '';
return($pth);
}
sub ChangeFileExt{
my ($fl, $ext) = @_;
$fl = ExtractFilePath($fl).ExtractFileName($fl).$ext;
return($fl);
}
;;;sub isExistsDir{
my ($dir) = @_;
my $ret = (-d $dir) ? 1 : 0;
return($ret)
}
sub isExistsFile{
my ($fl) = @_;
my $ret = (-e $fl) ? 1 : 0;
return($ret)
}
sub isWriteFile{
my ($fl) = @_;
my $ret = (-w $fl) ? 1 : 0;
return($ret)
}
sub isFileOf{
my ($ext, $fl) = @_;
$fl = ExtractFileExt(lc($fl));
my @ext = split(/,/, lc(TrimA($ext)));
my $ret = (HitOf($fl, @ext) >= 0) ? 1 : 0;
return($ret);
}
;;;sub CurrDir{
my ($dir) = @_;
my $ret = '0';
if($dir){
$ret = chdir($dir);
}else{
$ret = Cwd::getcwd();
}
return($ret);
}
sub MakeDir{
my ($dir) = @_;
my $ret = 0;
unless(isExistsDir($dir)){
umask(0);
$ret = mkdir("$dir", 0755);
}
return($ret);
}
sub DeleDir{
my ($dir) = @_;
my $ret = 0;
if(isExistsDir($dir)){
$ret = rmdir($dir);
}
return($ret);
}
sub DeleFile{
my ($fl) = @_;
my $ret = 0;
if(isExistsFile($fl)){
$ret = unlink($fl);
}
return($ret);
}
sub EraseFile{
my($fl) = @_;
if(ExtractFileExt($fl)){
my @file;
FilesMatch($fl, \@file);
foreach(@file){k2::DeleFile($_)}
return(1);
}else{
return(0);
}
}
sub RemoveDir{
my($dir) = @_;
if(opendir(FP, $dir)){
my @FL = readdir(FP);
close FP;
foreach(@FL){
if($_ =~ /^\.{1,2}$/){
if(-d "$dir/$_"){
deletedir("$dir/$_");
} else {
unlink("$dir/$_");
}
}
}
rmdir($dir);
return(1);
}else{
return(0);
}
}
sub RenameDir{
my ($old, $new) = @_;
my $ret = 0;
if(isExistsDir($old) && !isExistsDir($old)){
$ret = rename[$old], [$new];
}
return($ret);
}
sub RenameFile{
my ($old, $new) = @_;
my $ret = 0;
if(isExistsFile($old) && !isExistsFile($old)){
$ret = rename[$old], [$new];
}
return($ret);
}
sub Permission{
my ($fl, $pm) = @_;
if($pm){
return(setPermission($fl, $pm));
}else{
return(getPermission($fl));
}
}
sub getPermission{
my ($fl) = @_;
my $ret = '0';
if(isExistsFile($fl)){
my @st = stat "$fl";
my $pm = substr((sprintf "%03o", $st[2]), -3);
$ret = $pm;
}
return($ret);
}
sub setPermission{
my ($fl, $Permission) = @_;
if(isExistsFile($fl) && $Permission){
$Permission = RightStr('0000' + $Permission, 4);
chmod(eval($Permission), $fl);
return(1);
}else{
return(0);
}
}
;;;sub LoadFile{
my ($fl, $dat) = @_;
@$dat = ();
if(isExistsFile($fl)){
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@$dat = <FP>;
close(FP);
OmitRetLastEx(\@$dat);
}
return(@$dat);
}
sub SaveFile{
my ($fl, @dat) = @_;
if(isExistsFile($fl)){
DeleFile($fl);
}
unless(open(FP, ">$fl")){return(0)}
unless(Lock0(FP, 2)){return(-1)}
foreach(@dat){
print FP "$_\n";
}
close(FP);
return(1);
}
sub AppendFile{
my ($fl, @dt) = @_;
unless(isExistsFile($fl)){
$ret = SaveFile($fl, @dt);
}else{
unless(open(FP, ">>$fl")){return(0)}
unless(Lock0(FP, 2)){return(-1)}
foreach(@dt){
print FP "$_\n";
}
close(FP);
}
return(1);
}
sub CopyFile{
my ($f0, $f1, $pm) = @_;
if(isExistsFile($f0) && !isExistsFile($f1)){
!(-e "$f0") && return(0); !(copy($f0, $f1)) && return(0); if($pm){Permission($f1, $pm)}
return(1);
}else{
return(0);
}
}
sub MoveFile{
my ($f0, $f1) = @_;
if(isExistsFile($f0) && !isExistsFile($f1)){
if(move($f0, $f1)){
return(1);
}else{
return(0);
}
}else{
return(0);
}
}
;;;sub Lock0{
my ($FP, $no) = @_;
my $ok = eval{flock(FP, $no);};
if(defined($ok)){
return(1);
}else{
return(0);
}
}
sub Lock1{
my ($lock_fl) = @_;
my $try = 5;
while(!symlink(".", $lock_fl)){
if(--$try <= 0){
last;
}
sleep(1);
}
}
sub Lock2{
my ($lock_fl) = @_;
foreach(1..5){
if(-e $lock_fl){
sleep(1);
}else{
open(LOCK, ">$lock_fl");
close(LOCK);
return(1);
}
}
}
sub unLock{
DeleFile($lock_fl);
}
;;;sub LoadCSV{
my ($fl, $dat) = @_;
@$dat = ();
if(isExistsFile($fl)){
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
foreach(<FP>){
my @val = Csv2Array($_);
push(@$dat, [@val]);
}
close(FP);
}
return(@$dat);
}
sub SaveCSV{
my ($fl, @dat) = @_;
if(isExistsFile($fl)){
DeleFile($fl);
}
unless(open(FP, ">$fl")){return(0)}
unless(Lock0(FP, 2)){return(-1)}
foreach(@dat){
my $val = join(',', map {(s/"/""/g or /[\r\n,]/) ? qq("$_") : $_} @dat);
print FP "$val\n";
}
close(FP);
return(1);
}
;;;;;;sub LoadLine{
my ($fl, $no, $dat) = @_;
my $ret = '';
if(isExistsFile($fl)){
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
while(<FP>){
if($. == $no){
$ret = $_;
last;
}
}
close(FP);
}
$$dat = $ret;
return($ret);
}
sub SaveLine{
my ($fl, $no, $dt) = @_;
my ($pt, $ln, $fg);
if(isExistsFile($fl)){
unless(open(FP, "+<$fl")){return(0)}
unless(Lock0(FP, 2)){return(-1)}
unless($no){
$no = 1;
$fg = 1;
}
while(<FP>){
if($. == $no){
$pt = tell(FP) - length($_);
$ln = length($_) - 1;
last;
}
}
if($ln){
unless($fg){
$dt = Text2MaxLenW($dt, $ln, 1);
seek(FP, $pt, 0);
print FP "$dt\n";
}
}
close(FP);
if($fg){
$fg = 0;
unless(open(FP, "+<$fl")){return(0)}
unless(Lock0(FP, 2)){return(-1)}
while(<FP>){
$ln = length($_) - 1;
unless(Trim($_)){
$dt = _LineData($dt, $ln);
seek(FP, -length($_), 1);
print FP "$dt\n";
last;
}
}
unless($fg){
$dt = Text2MaxLenW($dt, $ln, 1);
print FP "$dt\n";
}
close(FP);
}
}else{
$ret = SaveFile($fl, $dt);
}
return(1);
}
sub SearchHitLine{
my ($fl, $key, $dat) = @_;
$$dat = '';
if(isExistsFile($fl)){
$key = cnv2sjis($key);
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
foreach(<FP>){
if(index($_, $key) >= 0){
$$dat = $_;
last;
}
}
close(FP);
}
return($$dat);
}
sub SearchNextLine{
my ($fl, $key, $no, $dat) = @_;
unless($no){$no = 0}
$$dat = '';
if(isExistsFile($fl)){
$key = cnv2sjis($key);
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
my $ct = 0;
foreach(<FP>){
$ct++;
if($ct > $no){
if(index($_, $key) >= 0){
$$dat = $ct.','.$_;
last;
}
}
}
close(FP);
}
return($$dat);
}
sub SearchAllLine{
my ($fl, $key, $dat) = @_;
my ($ret, $pt) = (0, 0);
if(isExistsFile($fl)){
my $ct = 0;
$key = cnv2sjis($key);
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
foreach(<FP>){
$ct++;
if(index($_, $key) >= 0){
$dat->[$pt] = $ct;
$pt++;
}
}
close(FP);
}
return($pt);
}
sub SpaceLine{
my ($fl) = @_;
my $ret = 0;
if(isExistsFile($fl)){
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
while(<FP>){
unless(Trim($_)){
$ret = $.;
last;
}
}
close(FP);
}
return($ret);
}
sub LastLine{
my ($fl) = @_;
my $ct = 0;
unless(open(FP, "<$fl")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
seek(FP, 0, 1);
my $ln = length(<FP>);
seek(FP, 0, 2);
if($ln){$ct = int(tell(FP) / $ln)};
close(FP);
return($ct);
}
;;;;;;;;;;;;;;;;;;;;;;;;;sub iniRead{
my ($ini, $section, $ident, $value) = @_;
my $ret = ($value) ? $value : '';
if($ini && $section && $ident){
if(isExistsFile($ini)){
my @dat = ();
unless(open(FP, "<$ini")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@dat = <FP>;
close(FP);
OmitRetLastEx(\@dat);
$ident .= '=';
my $pt = HitOf('['.$section.']', @dat);
if($pt >= 0){
for(my $i = $pt + 1; $i < @dat; $i++){
unless(LeftStrW($dat[$i], 1) eq '['){
if($dat[$i] =~ /$ident/i){
($a, $ret) = split(/=/,$dat[$i]);
$ret = TrimS($ret);
last;
}
}else{
last;
}
}
}
}
}
return($ret);
}
sub iniWrite{
my ($ini, $section, $ident, $value) = @_;
my $ret = 0;
if($ini && $section && $ident && $value){
my @dat = ();
$ident .= '=';
$section = '['.$section.']';
if(isExistsFile($ini)){
unless(open(FP, "<$ini")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@dat = <FP>;
close(FP);
OmitRetLastEx(\@dat);
my ($pt, $ed, $fg);
$pt = HitOf($section, @dat);
if($pt >= 0){
$value = TrimS($value);
for(my $i = $pt + 1; $i < @dat; $i++){
$ed = $i;
unless(LeftStrW($dat[$i], 1) eq '['){
if($dat[$i] =~ /$ident/i){
$dat[$i] = $ident.$value;
$fg = 1;
last;
}
}else{
last;
}
}
unless($fg){
$ed = ($ed == $#dat) ? $ed + 1 : $ed;
InsertOf(\@dat, $ed, $ident.$value);
}
}else{
$dat[@dat] = $section;
$dat[@dat] = $ident.$value;
}
}else{
$dat[@dat] = $section;
$dat[@dat] = $ident.$value;
}
$ret = SaveFile($ini, @dat);
}
return($ret);
}
sub iniReadAll{
my ($ini, $section, $value) = @_;
my $ret = 0;
if($ini && $section){
if(isExistsFile($ini)){
my @dat = ();
unless(open(FP, "<$ini")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@dat = <FP>;
close(FP);
OmitRetLastEx(\@dat);
my $pt = HitOf('['.$section.']', @dat);
if($pt >= 0){
my $ct = 0;
for(my $i = $pt + 1; $i < @dat; $i++){
unless(LeftStrW($dat[$i], 1) eq '['){
$value->[$ct] = TrimS($dat[$i]);
$ret = 1;
$ct++;
}else{
last;
}
}
}
}
}
return($ret);
}
sub iniWriteAll{
my ($ini, $section, @value) = @_;
my $ret = 0;
if($ini && $section){
my @dat = ();
if(isExistsFile($ini)){
iniRemove($ini, $section);
unless(open(FP, "<$ini")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@dat = <FP>;
close(FP);
OmitRetLastEx(\@dat);
}
$dat[@dat] = '['.$section.']';
foreach(@value){
$dat[@dat] = TrimS($_);
}
$ret = SaveFile($ini, @dat);
}
return($ret);
}
sub iniRemove{
my ($ini, $section, $ident) = @_;
my $ret = 0;
if($ini && $section){
if(isExistsFile($ini)){
my @dat = ();
unless(open(FP, "<$ini")){return(0)}
unless(Lock0(FP, 1)){return(-1)}
@dat = <FP>;
close(FP);
OmitRetLastEx(\@dat);
my ($pt, $fg);
$pt = HitOf('['.$section.']', @dat);
if($pt >= 0){
if($ident){
$ident .= '=';
}else{
$dat[$pt] = '';
}
for(my $i = $pt + 1; $i < @dat; $i++){
unless(LeftStrW($dat[$i], 1) eq '['){
if($ident){
if($dat[$i] =~ /$ident/i){
$dat[$i] = '';
$fg = 1;
last;
}
}else{
$dat[$i] = '';
$fg = 1;
}
}else{
last;
}
}
if($fg){
my @buf = ();
foreach(@dat){
if($_){$buf[@buf] = $_}
}
$ret = SaveFile($ini, @buf);
}
}
}
}
return($ret);
}
1;