1 Star 0 Fork 0

t3190687/liberty-file-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FlHandle.pm 6.24 KB
一键复制 编辑 原始数据 按行查看 历史
t3190687 提交于 2020-09-25 06:12 +08:00 . 20w39d415_dfa1354b
package FlHandle;
use strict;
use warnings;
use CON;
use Cwd qw{abs_path cwd};
use Digest::SHA qw/sha512_hex/;
use Env qw(USER);
use File::Basename;
use File::Path qw(make_path);
use File::Slurp;
use IO::File;
use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use List::AllUtils qw/max/;
use Math::Random::ISAAC;
use Time::HiRes qw/gettimeofday/;
use DDP;
$| = 1;
use Exporter;
use vars qw(@ISA @EXPORT);
use Carp::Always::Color;
push @ISA, qw(Exporter);
push @EXPORT, qw(
FlHandle
GetRndHex
GetRndStr
GetTmpDir
GetYWRndStr
GetYWStr
ReadPartOfZFile
SortBits
);
sub ReadPartOfZFile
{
my ( $fi, $rstr_back, $length ) = @_;
my $zf;
&FlHandle( $fi, \$zf );
if ($length) { $zf->read( $rstr_back, $length ); }
else { $zf->read($rstr_back); }
close $zf;
}
sub FlHandle
{ #_{{{
my ( $fi, $rfile, $rMainWarningStr ) = @_;
if ( not $fi ) {
Carp::cluck "-W- no file provided";
if ($rMainWarningStr) {
$$rMainWarningStr .= sprintf "%s no file provided;\n", &color_or_not( q/-W-/, q/red/ );
}
return;
}
my $absF;
if ( $fi and -e $fi ) {
$absF = abs_path($fi);
} else {
Carp::cluck "-W- cannot see $fi";
if ($rMainWarningStr) {
$$rMainWarningStr .= sprintf "%s cannot see %s ;\n", &color_or_not( q/-W-/, q/red/ ), &color_or_not( $fi, q/red/ );
}
return;
}
my ( $tfbase, $tDir, $suffix ) = fileparse( $absF, qr{\.[^.]*(\.(?:gz|bz2))?} );
# printf "-I- reading %8s\n", &color_or_not($absF);
if ( $suffix =~ /\.gz/ ) { $$rfile = IO::Uncompress::Gunzip->new($fi) or Carp::confess "-E-cannot open $fi; $GunzipError"; }
elsif ( $suffix =~ /\.bz2/ ) { $$rfile = IO::Uncompress::Bunzip2->new($fi) or Carp::confess "-E-cannot open $fi; $Bunzip2Error"; }
else { $$rfile = IO::File->new($fi) or Carp::confess "-E-cannot open $fi"; }
return;
} #_}}}
sub GetTmpDir
{#_{{{
my ($reuse) = @_;
my $opt_dir = qq{/tmp/$USER};
if (not -d $opt_dir){
make_path($opt_dir) or Carp::confess "-E-cannot create $opt_dir";
}
my $yywwString = &GetYWStr;
my $Dir = qq#$opt_dir/$yywwString#;
my $tDir;
my @Ar2 = ('a' .. 'z');
my @Ar3 = map { $_ = ".".$_; } ( 1 .. 100);
foreach my $lt ( @Ar2, @Ar3 ) {
$tDir = qq#$Dir$lt#;
if ( $reuse and -d $tDir ) {
return $tDir;
} elsif ( -e $tDir ) {
next;
} else {
make_path($tDir) or Carp::confess "-E-cannot make path $tDir";
return $tDir;
}
}
}#_}}}
sub GetYWStr
{
my ($two_four_digit_year) = @_;
my $yywwString;
if ( $two_four_digit_year and ( $two_four_digit_year == 4 ) ) {
$yywwString = qx(date +"%Yw%Vd%u%H");
} else {
# $yywwString = qx(date +"%yww%Wd%w%H");
$yywwString = qx(date +"%yw%Vd%u%H");
}
chomp $yywwString;
return $yywwString;
}
sub GetRndStr
{#_{{{
my ($Digit) = @_;
# $Digit //= 10; # the defined or assign // operator not working? 20ww23
if (defined $Digit){ } else { $Digit = 10; }
my $rn0 = q//;
my $oDigit = $Digit;
while ( $Digit > 0 ) {
$rn0 .= sprintf "%010d", Math::Random::ISAAC->new((gettimeofday)[-1])->irand();
$Digit -= 10;
}
my $rngstr = substr( $rn0, 0, $oDigit );
return $rngstr;
}#_}}}
sub GetRndHex
{#_{{_
my ($Digit) = @_;
if (defined $Digit){ } else { $Digit = 8; }
my $rndN = &GetRndStr($Digit);
my $rndH = sha512_hex($rndN);
my $rngstr = substr( $rndH, 0, $Digit );
return $rngstr;
}#_}}_
sub GetYWRndStr
{
my $s1= &GetYWStr(10);
my $s2 = sprintf "%.8x", Math::Random::ISAAC->new((gettimeofday)[-1])->irand();
return qq/${s1}_$s2/;
}
sub SortBits
{
my ($rA) = @_;
my @A2 = sort {
( $a =~ s/\[.*//rg ) cmp( $b =~ s/\[.*//rg )
or
( $a =~ s/\[\d+\]//rg ) cmp( $b =~ s/\[\d+\]//rg )
or
( $a =~ /\[(\d+)\]/ )[0] <=> ( $b =~ /\[(\d+)\]/ )[0]
or
( $a =~ /\[(\d+)\]/g )[1] <=> ( $b =~ /\[(\d+)\]/g )[1]
or
( $a =~ /\[(\d+)\]/g )[2] <=> ( $b =~ /\[(\d+)\]/g )[2] } @$rA;
@$rA = @A2;
}
1;
__END__
=head1 NAME
=head2 FlHandle()
Assign File handle onto the passed-in reference to scalar
-- regardless file is gzip'ed, bzip2'ed or nonzip'ed at all
my $zctf;
&FlHandle($fi, \$zctf);
# and we can use File::Slurp's read_file, like
my $str = read_file($zctf);
=head2 ReadPartOfZFile()
To read the first part of a gzipped/bzipped/non-zipped
file. E.g., to read in A.txt.gz file's first
20k content, into scalar string $str1.
my $str1;
my $in_file = qq(A.txt.gz);
&FlHandle::ReadPartOfZFile( $in_file, \$str1, 20_480);
=head2 GetTmpDir()
create a temporary directory in /tmp/$USER/ area
my $tdir = &GetTmpDir(0);
my $outF = qq($tdir/$rngstr.$bct.$fbname.txt);
=head2 GetRndStr(N)
returns an N-digit long random number (default N=10).
This random number is encrypto-wise secure from ISAAC.
=head3 See Also
Math::Random::ISAAC
=head2 GetRndHex(N)
returns an N-character long random hex string ([0-9a-f]
character set, and N=8 by default). This hex string
is (1) using a 10-digit random number from &GetRndStr
as a seed, and (2) acquiring the seed's SHA-512 hex
representation through &Digest::SHA::sha512_hex();
=head3 See Also
Math::Random::ISAAC
Digest::SHA
=head2 GetYWStr()
returns a string in 19w41d312 format. 19w41d312
stands for year 2019, workweek 41, day-3, hour-12.
GetYWStr(4) would return the 4-digit year as 2019
=head2 GetYWRndStr()
returns the yyww-string appended with random number above.
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Perl
1
https://gitee.com/t3190687/liberty-file-utils.git
git@gitee.com:t3190687/liberty-file-utils.git
t3190687
liberty-file-utils
liberty-file-utils
master

搜索帮助