|
%cat jutil.pl
#!/usr/bin/perl
require "jutil.pm";
my $apath = "./adir/../../../../../\${JUTIL_HOME}_abc/../\${DISPLAY}cdir/../jutil/ffile/bdir/b.v";
$apath = join("/",pathoptimize(envvar2word($apath)));
print "[$apath]\n";
if(-e $apath) { print "$apath exist.\n";}
if(-f $apath) { print "$apath file exist.\n";}
if(-d $apath) { print "$apath dir exist.\n";}
$apath = "\$JUTIL_HOME/ffile//./adir";
$apath = join("/",pathoptimize(envvar2word($apath)));
print "[$apath]\n";
if(-e $apath) { print "$apath exist.\n";}
if(-f $apath) { print "$apath file exist.\n";}
if(-d $apath) { print "$apath dir exist.\n";}
%cat jutil.pm
#!/usr/bin/perl
#==============================================================================#
package main;
#==============================================================================#
#------------------------------------------------------------------------------#
sub envvar2word {
#------------------------------------------------------------------------------#
my ($aline) = @_;
while($aline =~ /\$/) {
my @chrs = split('',$aline);
$aline = "";
while($chrs[0] ne "\$") {
$aline .= $chrs[0];
shift(@chrs);
}
$avar = "";
shift(@chrs);
while($chrs[0] =~ /[\{a-zA-Z0-9\_]/) {
if($chrs[0] !~ /[\{\}]/) {
$avar .= $chrs[0];
}
shift(@chrs);
}
if($chrs[0] eq "\}") { shift(@chrs);}
if($avar) {
if($ENV{$avar}) { $aline .= $ENV{$avar};}
else { $aline .= "JMJSDOLLAR{$avar}";}
}
while(@chrs) {
$aline .= $chrs[0];
shift(@chrs);
}
}
$aline =~ s/JMJSDOLLAR/\$/g;
return($aline);
}
#------------------------------------------------------------------------------#
sub list_extract {
#------------------------------------------------------------------------------#
my ($num,@tlist) = @_;
if(($num < 0)||($num > $#tlist)) { return(@tlist);}
elsif($num == 0) { shift(@tlist); return(@tlist);}
elsif($num == $#tlist) { pop(@tlist); return(@tlist);}
@tlist = (@tlist[0..($num-1)],@tlist[($num+1)..$#tlist]);
return(@tlist);
}
#------------------------------------------------------------------------------#
sub list_has {
#------------------------------------------------------------------------------#
my ($word,@tlist) = @_;
my @olist = ();
for(my $i=0;$i<=$#tlist;$i++) {
if($word eq $tlist[$i]) { push(@olist,$i);}
}
return @olist;
}
#------------------------------------------------------------------------------#
sub pathoptimize {
#------------------------------------------------------------------------------#
my ($apath) = @_;
my @dirs = split(/\//,$apath);
my @newdirs = ($dirs[0]);
shift(@dirs);
foreach $adir (@dirs) {
if( $adir eq "" ) { }
elsif( $adir eq "." ) { }
elsif(($adir eq "..") && $#newdirs
&& ($newdirs[$#newdirs] ne "..")) { pop(@newdirs);}
else { push(@newdirs,$adir);}
}
@newdirs;
}
#------------------------------------------------------------------------------#
sub time_chrs {
#------------------------------------------------------------------------------#
my ($atime) = @_;
if(! $atime) { $atime = time;}
my ($xs,$xm,$xh,$xdd,$xmm,$xyy,$xwd,$xyd,$xisdst) = localtime($atime);
$xmm ++;
$xyy += 1900;
$atime = sprintf("%4d%02d%02d%02d%02d%02d",$xyy,$xmm,$xdd,$xh,$xm,$xs);
my @olist = split(//,$atime);
@olist;
}
#------------------------------------------------------------------------------#
sub unilist {
#------------------------------------------------------------------------------#
my @ilist = @_;
my @olist = ();
for(my $i=0;$i<=$#ilist;$i++) {
my $flag = 0;
foreach $oword (@olist) {
if($oword eq $ilist[$i]) { $flag = 1;}
}
if(! $flag) { push(@olist,$ilist[$i]);}
}
@olist;
}
#==============================================================================#
package JARRAY;
#==============================================================================#
#------------------------------------------------------------------------------#
sub new {
#------------------------------------------------------------------------------#
my ($class, @data) = @_;
my $ref = [];
bless $ref, $class;
$ref->init(@data);
return $ref;
}
#------------------------------------------------------------------------------#
sub init {
#------------------------------------------------------------------------------#
my ($ref, @data) = @_;
$ref->add(@data);
}
#------------------------------------------------------------------------------#
sub add {
#------------------------------------------------------------------------------#
my ($ref, @data) = @_;
if(@data) { @$ref = (@$ref,@data);}
}
#------------------------------------------------------------------------------#
sub jprint {
#------------------------------------------------------------------------------#
my ($ref,$tab,$hier) = @_;
if(! $hier) { $hier = 1;}
else { $hier ++;}
my $ctab = "";
for(my $i=1;$i<$hier;$i++) { $ctab .= $tab;}
my $oline = "$ref = (\n";
for(my $i=0;$i<=$#$ref;$i++) {
$alist = $$ref[$i];
$oline .= "$ctab$tab\[$i\]=";
if( ref($alist) eq "" ) {
$oline .= "$alist\n";
}
elsif( ref($alist) eq "SCALAR" ) {
$oline .= "$alist\n";
}
elsif(ref($alist) eq "ARRAY" ) {
$oline .= $alist->jprint($tab,$hier);
}
elsif( ref($alist) eq "HASH" ) {
$oline .= $alist->jprint($tab,$hier);
}
else {
$oline .= $alist->jprint($tab,$hier);
}
}
$oline .= "$ctab)\n";
$oline;
}
#------------------------------------------------------------------------------#
sub list { my ($ref) = @_; return (@$ref); }
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
sub remove {
#------------------------------------------------------------------------------#
my ($ref, @nums) = @_;
my @data = @$ref;
@$ref = ();
foreach my $i (@nums) { $data[$i] = ();}
for($i=0;$i<=$#data;$i++) { if($data[$i]) { $ref->add($data[$i]);} }
}
#------------------------------------------------------------------------------#
sub DESTROY {
#------------------------------------------------------------------------------#
#rint "Destroy the object at @_.\n";
}
#==============================================================================#
package JHASH;
#==============================================================================#
#------------------------------------------------------------------------------#
sub new {
#------------------------------------------------------------------------------#
my ($class, @data) = @_;
my $ref = {};
bless $ref, $class;
$ref->init(@data);
return $ref;
}
#------------------------------------------------------------------------------#
sub init {
#------------------------------------------------------------------------------#
my ($ref, @data) = @_;
$ref->add(@data);
}
#------------------------------------------------------------------------------#
sub add {
#------------------------------------------------------------------------------#
my ($ref, @data) = @_;
if(@data) { %$ref = (%$ref,@data);}
}
#------------------------------------------------------------------------------#
sub jprint {
#------------------------------------------------------------------------------#
my ($ref,$tab,$hier) = @_;
if(! $hier) { $hier = 1;}
else { $hier ++;}
my $ctab = "";
for(my $i=1;$i<$hier;$i++) { $ctab .= $tab;}
my $oline = "$ref = (\n";
foreach $key (keys %$ref) {
$oline .= "$ctab$tab\{$key\}=>";
my $alist = %$ref->{$key};
if( ref($alist) eq "" ) {
$oline .= "$alist\n";
}
elsif( ref($alist) eq "SCALAR" ) {
$oline .= "$alist\n";
}
elsif( ref($alist) eq "ARRAY" ) {
$oline .= $alist->jprint($tab,$hier);
}
elsif( ref($alist) eq "HASH" ) {
$oline .= $alist->jprint($tab,$hier);
}
else {
$oline .= $alist->jprint($tab,$hier);
}
}
$oline .= "$ctab)\n";
$oline;
}
#------------------------------------------------------------------------------#
sub remove {
#------------------------------------------------------------------------------#
my ($ref, @keys) = @_;
my @data = %$ref;
%$ref = ();
for($i=0;$i<$#data;$i+=2) {
my $flag = 0;
foreach $akey (@keys) { if($data[$i] eq $akey) { $flag = 1;} }
if(! $flag) { $ref->add($data[$i],$data[$i +1]);}
}
}
#------------------------------------------------------------------------------#
sub DESTROY {
#------------------------------------------------------------------------------#
#rint "Destroy the object at @_.\n";
}
1; |
|