package RDA::Macro::Temp; # $Id: Temp.pm,v 1.16 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Temp.pm,v 1.16 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Temp - Class Used for Temporary File Management Macros =head1 SYNOPSIS require RDA::Macro::Temp; =head1 DESCRIPTION The objects of the C class are used to interface with temporary file management macros. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Spec; use IO::File; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_fct = ( 'closeTemp' => [\&_m_close_temp, 'N'], 'createTemp' => [\&_m_create_temp, 'T'], 'getTemp' => [\&_m_get_temp, 'T'], 'unlinkTemp' => [\&_m_unlink_temp, 'N'], 'writeTemp' => [\&_m_write_temp, 'N'], ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Temp-Enew($agt)> The object constructor. It takes the agent object reference as an argument. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item B< '_afl' > Autoflush indicator =item B< '_agt' > Reference to the agent object =item B< '_grp' > Report group =item B< '_rpt' > Report directory =item B< '_tmp' > Hash containing the temporary file definitions =back Temporary file hashes use the following special keys: =over 16 =item B< '_cnt' > Number of lines written =item B< '_fil' > File name =item B< '_ofh' > Output file handle =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Create the macro object $slf = bless {_agt => $agt}, $class; # Setup some parameters by default $slf->{'_tmp'} = {}; # Get some settings $slf->{'_afl'} = $agt->get_setting('RDA_FLUSH', 1); $slf->{'_grp'} = $agt->get_setting('RPT_GROUP'); $slf->{'_rpt'} = $agt->get_setting('RPT_DIRECTORY', File::Spec->curdir); # Register the macros $agt->register($slf, [keys(%tb_fct)]); # Return the object reference $slf; } =head2 S<$h-Erun($name,$arg,$ctx)> This method executes the macro with the specified argument list in a given context. =cut sub run { my ($slf, $nam, $arg, $ctx) = @_; my ($fct, $ret, $typ); $fct = $tb_fct{$nam}; $typ = $fct->[1]; # Treat an array context return RDA::Value::new_list(&{$fct->[0]}($slf, $ctx, $arg->eval_as_array)) if $typ eq 'L'; # Treat a scalar context $ret = &{$fct->[0]}($slf, $ctx, $arg->eval_as_array); $typ = 'U' unless defined($ret); return RDA::Value->new($typ, $ret); } =head1 TEMPORARY FILE MACROS =head2 S This macro closes the specified temporary file. It returns 1 on successful completion and 0 if the file was not previously defined. =cut sub _m_close_temp { my ($slf, $ctx, $nam) = @_; my ($tmp); return 0 unless exists($slf->{'_tmp'}->{$nam}); $tmp = $slf->{'_tmp'}->{$nam}; if (exists($tmp->{'_ofh'})) { close($tmp->{'_ofh'}); delete($tmp->{'_ofh'}); } 1; } =head2 S This macro creates a temporary file name based on the specified name, used as unique identifier for other requests. It uses C<.tmp> as the default suffix and removes any previous file. When the flag is set, the file is made executable, but it is only accessible for the file owner. It returns the file name on successful completion. Otherwise, it returns an undefined value. =cut sub _m_create_temp { my ($slf, $ctx, $nam, $suf, $flg) = @_; my ($fil, $ofh, $tmp); # Delete any temporary file previously associated with that name _del_tmp($slf, $nam); # Define the new temporary file $tmp = _new_tmp($slf, $nam, $suf); $fil = $tmp->{'_fil'}; # Create the file and return the file name $tmp->{'_ofh'} = $ofh = IO::File->new; unless (open($ofh,">$fil")) { $tmp->{'_err'} = 0; return undef; } $ofh->autoflush(1) if $slf->{'_afl'}; chmod(0700, $fil) if $flg; $fil; } =head2 S This macro generates a temporary file name based on the specified name, which is used as a unique identifier for other requests. It uses C<.tmp> is used as the default suffix. =cut sub _m_get_temp { my ($slf, $ctx, $nam, $suf) = @_; exists($slf->{'_tmp'}->{$nam}) ? $slf->{'_tmp'}->{$nam}->{'_fil'} : _new_tmp($slf, $nam, $suf)->{'_fil'}; } =head2 S This macro unlinks a temporary file. It returns the number of versions removed. =cut sub _m_unlink_temp { my ($slf, $ctx, $nam) = @_; _del_tmp($slf, $nam); } =head2 S This macro writes a line in the temporary file. The file is automatically opened when it is not previously defined. It returns 1 for a successful completion. Otherwise, it returns 0. =cut sub _m_write_temp { my $slf = shift; my $ctx = shift; my $nam = shift; my ($ofh, $tmp); # Get the temporary file definition $tmp = exists($slf->{'_tmp'}->{$nam}) ? $slf->{'_tmp'}->{$nam} : _new_tmp($slf, $nam); # Get the output file handle if (exists($tmp->{'_ofh'})) { $ofh = $tmp->{'_ofh'}; } else { $tmp->{'_ofh'} = $ofh = IO::File->new; if (open($ofh,'>>'.$tmp->{'_fil'})) { $ofh->autoflush(1) if $slf->{'_afl'}; } else { $tmp->{'_err'} = 0; } } return 0 if exists($tmp->{'_err'}); # Write the line foreach my $str (@_) { print $ofh $str if defined($str); } print $ofh "\n"; 1; } #--- Internal methods --------------------------------------------------------- # Delete an existing temporary file sub _del_tmp { my ($slf, $nam) = @_; return 0 unless exists($slf->{'_tmp'}->{$nam}); my $cnt = 0; my $tmp = $slf->{'_tmp'}->{$nam}; my $fil = $tmp->{'_fil'}; close($tmp->{'_ofh'}) if exists($tmp->{'_ofh'}); ++$cnt while unlink($fil); delete($slf->{'_tmp'}->{$nam}); $cnt; } # Define a new temporary file sub _new_tmp { my ($slf, $nam, $suf) = @_; $suf = '.tmp' unless defined($suf); $slf->{'_tmp'}->{$nam} = { _cnt => 0, _fil => File::Spec->catfile($slf->{'_rpt'}, sprintf('%s_TMP_%s_%d%s', $slf->{'_grp'}, $nam, $$, $suf)), }; } 1; __END__ =head1 SEE ALSO See also L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut