diff options
| author | fukachan <fukachan> | 2001-03-11 10:34:19 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2001-03-11 10:34:19 +0000 |
| commit | 5827693385f9d846bacef2860467916d8a607177 (patch) | |
| tree | 7934bd6b0e9e3e555221f9fdf4df7ca245895bed /fml/lib/IO/Adapter/File.pm | |
| parent | f3f65292a8e32fac39c8d893748b38e125b172ca (diff) | |
| download | fml8-5827693385f9d846bacef2860467916d8a607177.tar.gz fml8-5827693385f9d846bacef2860467916d8a607177.tar.bz2 fml8-5827693385f9d846bacef2860467916d8a607177.zip | |
prepare _read_open() and _rw_open() for each purpose
implemenet add() method using IO::File::Atomic module
Diffstat (limited to 'fml/lib/IO/Adapter/File.pm')
| -rw-r--r-- | fml/lib/IO/Adapter/File.pm | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/fml/lib/IO/Adapter/File.pm b/fml/lib/IO/Adapter/File.pm index 69794527..fd3dfe97 100644 --- a/fml/lib/IO/Adapter/File.pm +++ b/fml/lib/IO/Adapter/File.pm @@ -22,12 +22,23 @@ IO::Adapter::File - IO functions for a file $map = 'file:/some/where/file'; +To read list + use IO::MapAdapter; $obj = new IO::MapAdapter $map; $obj->open || croak("cannot open $map"); while ($x = $obj->getline) { ... } $obj->close; +To add the address + + $obj = new IO::MapAdapter $map; + $obj->add( $address ); + +To delete it + + $obj->delete( $address ); + =head1 DESCRIPTION This module provides real IO functions for a file used in @@ -65,12 +76,30 @@ C<flag> is the mode of open(). sub open { my ($self, $args) = @_; + my $file = $args->{ file }; + my $flag = $args->{ flag }; + + if ($flag eq 'r') { + $self->_read_open($args); # read only open() + } + else { + $self->_rw_open($args); # read/write open in atomic way + } +} + +# Descriptions: open file in "read only" mode +# Arguments: $self $args +# Side Effects: file is opened for read +# Return Value: file descriptor +sub _read_open +{ + my ($self, $args) = @_; my $file = $args->{ file }; my $flag = $args->{ flag }; - my $fh; + use FileHandle; - $fh = new FileHandle $file, $flag; + my $fh = new FileHandle $file, $flag; if (defined $fh) { $self->{_fh} = $fh; return $fh; @@ -82,6 +111,20 @@ sub open } +sub _rw_open +{ + my ($self, $args) = @_; + my $file = $args->{ file }; + my $flag = $args->{ flag }; + + require IO::File::Atomic; + my ($rh, $wh) = IO::File::Atomic->rw_open($file); + $self->{ _fh } = $rh; + $self->{ _wh } = $wh; + $rh; +} + + # debug tools my $c = 0; my $ec = 0; @@ -194,6 +237,37 @@ sub close } +=head2 C<add($address)> + +add $address to this map. + +=cut + +sub add +{ + my ($self, $addr) = @_; + + $self->open("w"); + + my $fh = $self->{ _fh }; + my $wh = $self->{ _wh }; + + if (defined $fh) { + while (<$fh>) { + print $wh $_; + } + close($fh); + } + else { + $self->_error_reason("Error: cannot open $self->{ _file }"); + return undef; + } + + print $wh $addr, "\n"; + $wh->close; +} + + =head1 SEE ALSO L<IO::MapAdapter> |
