From: Will Budic Date: Wed, 15 Jan 2025 06:51:30 +0000 (+1100) Subject: New CNF specific file object. X-Git-Url: https://lifelog.hopto.org/gitweb/?a=commitdiff_plain;h=0c1ff36348d0d23503453acab07e2852ac39e9e9;p=PerlCNF.git New CNF specific file object. --- diff --git a/system/modules/CNFGlobalFile.pm b/system/modules/CNFGlobalFile.pm new file mode 100644 index 0000000..817887b --- /dev/null +++ b/system/modules/CNFGlobalFile.pm @@ -0,0 +1,66 @@ +# Wrapper around system files useful for caching if content are are text files. +# Holds by default its last modification time and checks for type and if has changed. +# Programed by : Will Budić + +# Documentation : Specifications_For_CNF_ReadMe.md +# Open Source Code License -> https://choosealicense.com/licenses/isc/ +# +package CNFGlobalFile; +use strict; use warnings; no warnings qw(experimental::signatures); use feature qw(signatures say); + +sub new ($class, $path) { + my @stat = stat($path); + bless {path=>$path, content_length => $stat[7], last_modified => $stat[9]}, $class; +} + +sub content($self) { + return _load($self) -> {content} ; +} +sub changed($self){ + if(exists $self->{last_modified}){ + my @cur = stat($self->{path}); + return $cur[9] != $self->{last_modified} ? 1 : 0 + } + return 1; +} +sub binary($self){ + _binary($self, *STDOUT) +} +sub _binary($self, $handle){ + select($handle) if $handle; + $| = 1; + open(my $fh, "<", $self->{path}) or die "Path: ".$self->{path}." not found!"; + print <$fh>; + close $fh; + $| = 0; + my @stat = stat($self->{path}); + $self -> {content_date} = time; + $self -> {content_length} = $stat[7]; + $self -> {last_modified} = $stat[9]; + return $self; +} +sub _load($self){ + open(my $fh,'<:encoding(UTF-8)', $self->{path}) or die "Path: ".$self->{path}." not found!"; + read $fh, my $content, -s $fh; + close $fh; + my @stat = stat($self->{path}); + $self -> {content_date} = time; + $self -> {content_length} = $stat[9]; + $self -> {content} = \$content; + return $self; +} + + +1; + +=begin copyright +Programed by : Will Budić +EContactHash : 990MWWLWM8C2MI8K (https://github.com/wbudic/EContactHash.md) +Source : git clone git://lifelog.hopto.org/PerlCNF + : https://github.com/wbudic/PerlCNF.git +Documentation : Specifications_For_CNF_ReadMe.md + This source file is copied and usually placed in a local directory, outside of its repository project. + So it could not be the actual or current version, can vary or has been modified for what ever purpose in another project. + Please leave source of origin in this file for future references. +Open Source Code License -> https://github.com/wbudic/PerlCNF/blob/master/ISC_License.md +=cut copyright \ No newline at end of file diff --git a/tests/testCNFGlobalFile.pl b/tests/testCNFGlobalFile.pl new file mode 100644 index 0000000..2d93a3b --- /dev/null +++ b/tests/testCNFGlobalFile.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use warnings; use strict; +use feature 'say'; +use lib "tests"; +use lib "system/modules"; + +require TestManager; +require CNFGlobalFile; +require CNFDateTime; + +my $test = TestManager->new($0); + +use Syntax::Keyword::Try; try { + + ### + # Test instance creation. + ### + $test->case("Test CNFGlobalFile instance creation."); + die $test->failed() if not my $file = CNFGlobalFile->new($0); + $test->evaluate("path==$0",$file->{path},"$0"); + $test->passed("Passed new instance for CNFParser "); + # + # + $test-> nextCase(); + # + $test->case("Test CNFDateTime instance creation for last_Modifed."); + die $test->failed() if not my $last_modified = CNFDateTime->now({epoh=>$file->{last_modified}}); + $test->evaluate("path==$0",$file->{last_modified},$last_modified->{epoh}); + $test->evaluate($last_modified->toSchlong()); + # + $test->done(); + # +} +catch{ + $test -> dumpTermination($@); + $test->doneFailed(); +}