summaryrefslogtreecommitdiff
path: root/fml/lib/IO
diff options
context:
space:
mode:
authorfukachan <fukachan>2001-01-28 07:32:40 +0000
committerfukachan <fukachan>2001-01-28 07:32:40 +0000
commit7ee280e93c6797fef8ab87454869272bd2df3526 (patch)
tree347ba0d76613c58a77f30301999bb013a248fd3c /fml/lib/IO
parent6d5d0c61438aa90bde9e1b368f09be53151ed188 (diff)
downloadfml8-7ee280e93c6797fef8ab87454869272bd2df3526.tar.gz
fml8-7ee280e93c6797fef8ab87454869272bd2df3526.tar.bz2
fml8-7ee280e93c6797fef8ab87454869272bd2df3526.zip
move file IO codes to a file adapter sub-layer (sub-class)
Diffstat (limited to 'fml/lib/IO')
-rw-r--r--fml/lib/IO/Adapter/File.pm91
-rw-r--r--fml/lib/IO/MapAdapter.pm103
2 files changed, 114 insertions, 80 deletions
diff --git a/fml/lib/IO/Adapter/File.pm b/fml/lib/IO/Adapter/File.pm
index df999494..73c17a69 100644
--- a/fml/lib/IO/Adapter/File.pm
+++ b/fml/lib/IO/Adapter/File.pm
@@ -8,7 +8,7 @@
# $FML$
#
-package IO::MapAdapter::File;
+package IO::Adapter::File;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
@@ -26,6 +26,95 @@ sub new
}
+sub open
+{
+ my ($self, $args) = @_;
+
+ my $file = $args->{ file };
+ my $flag = $args->{ flag };
+ my $fh;
+ use FileHandle;
+ $fh = new FileHandle $file, $flag;
+ if (defined $fh) {
+ $self->{_fh} = $fh;
+ return $fh;
+ }
+ else {
+ $self->_error_reason("Error: cannot open $file $flag");
+ return undef;
+ }
+}
+
+
+# debug tools
+my $c = 0;
+my $ec = 0;
+sub line_count { my ($self) = @_; return "${ec}/${c}";}
+
+
+sub get_next_value
+{
+ my ($self) = @_;
+
+ my ($buf) = '';
+ my $fh = $self->{_fh};
+
+ if (defined $fh) {
+ INPUT:
+ while ($buf = <$fh>) {
+ $c++; # for benchmark (debug)
+ next INPUT if not defined $buf;
+ next INPUT if $buf =~ /^\s*$/o;
+ next INPUT if $buf =~ /^\#/o;
+ next INPUT if $buf =~ /\sm=/o;
+ next INPUT if $buf =~ /\sr=/o;
+ next INPUT if $buf =~ /\ss=/o;
+ last INPUT;
+ }
+
+ if (defined $buf) {
+ my @buf = split(/\s+/, $buf);
+ $buf = $buf[0];
+ $buf =~ s/[\r\n]*$//o;
+ $ec++;
+ }
+ return $buf;
+ }
+ return undef;
+}
+
+
+sub getops
+{
+ my ($self) = @_;
+ my $fh = $self->{_fh};
+ defined $fh ? tell($fh) : undef;
+}
+
+
+sub setops
+{
+ my ($self, $pos) = @_;
+ my $fh = $self->{_fh};
+ seek($fh, $pos, 0);
+}
+
+
+sub eof
+{
+ my ($self) = @_;
+ my $fh = $self->{_fh};
+ $fh->eof if defined $fh;
+}
+
+
+sub close
+{
+ my ($self) = @_;
+ $self->{_fh}->close if defined $self->{_fh};
+}
+
+
#####
##### This is just a dummy yet now.
#####
diff --git a/fml/lib/IO/MapAdapter.pm b/fml/lib/IO/MapAdapter.pm
index a32aa7c8..e70861e0 100644
--- a/fml/lib/IO/MapAdapter.pm
+++ b/fml/lib/IO/MapAdapter.pm
@@ -89,36 +89,28 @@ sub new
my ($self, $map, $args) = @_;
my ($type) = ref($self) || $self;
my ($me) = {};
+ my $pkg;
if (ref($map) eq 'ARRAY') {
+ $pkg = 'IO::Adapter::Array';
$me->{_type} = 'array_reference';
$me->{_array_reference} = $map;
-
- my $pkg = 'IO::Adapter::Array';
- unshift(@ISA, $pkg);
- eval qq{ require $pkg; $pkg->import();};
- _error_reason($me, $@) if $@;
}
else {
if ($map =~ /file:(\S+)/ || $map =~ m@^(/\S+)@) {
+ $pkg = 'IO::Adapter::File';
$me->{_file} = $1;
$me->{_type} = 'file';
}
elsif ($map =~ /unix\.group:(\S+)/) {
+ $pkg = 'IO::Adapter::Array';
$me->{_name} = $1;
$me->{_type} = 'unix.group';
-
- my $pkg = 'IO::Adapter::Array';
- unshift(@ISA, $pkg);
- eval qq{ require $pkg; $pkg->import();};
- _error_reason($me, $@) if $@;
}
elsif ($map =~ /(ldap|mysql|postgresql):(\S+)/) {
$me->{_type} = $1;
$me->{_schema} = $2;
-
- # lowercase the '_type' syntax
- $me->{_type} =~ tr/A-Z/a-z/;
+ $me->{_type} =~ tr/A-Z/a-z/; # lowercase the '_type' syntax
}
else {
my $s = "IO::MapAdapter::new: args='$map' is unknown.";
@@ -127,6 +119,10 @@ sub new
}
}
+ unshift(@ISA, $pkg);
+ eval qq{ require $pkg; $pkg->import();};
+ _error_reason($me, $@) if $@;
+
return bless $me, $type;
}
@@ -163,18 +159,7 @@ sub open
$flag ||= 'r';
if ($self->{'_type'} eq 'file') {
- my $file = $self->{_file};
- my $fh;
- use FileHandle;
- $fh = new FileHandle $file, $flag;
- if (defined $fh) {
- $self->{_fh} = $fh;
- return $fh;
- }
- else {
- $self->_error_reason("Error: cannot open $file $flag");
- return undef;
- }
+ $self->SUPER::open( { file => $self->{_file}, flag => $flag } );
}
elsif ($self->{'_type'} eq 'unix.group') {
my (@x) = getgrnam( $self->{_name} );
@@ -197,11 +182,6 @@ sub open
}
-my $c = 0;
-my $ec = 0;
-sub line_count { my ($self) = @_; return "${ec}/${c}";}
-
-
# aliases for convenience
sub get_member { my ($self) = @_; $self->_get_address;}
sub get_active { my ($self) = @_; $self->_get_address;}
@@ -211,31 +191,7 @@ sub _get_address
my ($self) = @_;
if ($self->{'_type'} eq 'file') {
- my ($buf) = '';
- my $fh = $self->{_fh};
-
- if (defined $fh) {
- INPUT:
- while ($buf = <$fh>) {
- $c++; # for benchmark (debug)
- next INPUT if not defined $buf;
- next INPUT if $buf =~ /^\s*$/o;
- next INPUT if $buf =~ /^\#/o;
- next INPUT if $buf =~ /\sm=/o;
- next INPUT if $buf =~ /\sr=/o;
- next INPUT if $buf =~ /\ss=/o;
- last INPUT;
- }
-
- if (defined $buf) {
- my @buf = split(/\s+/, $buf);
- $buf = $buf[0];
- $buf =~ s/[\r\n]*$//o;
- $ec++;
- }
- return $buf;
- }
- return undef;
+ $self->SUPER::get_next_value();
}
elsif ($self->{'_type'} eq 'unix.group' ||
$self->{'_type'} eq 'array_reference') {
@@ -286,12 +242,9 @@ sub getpos
{
my ($self) = @_;
- if ($self->{'_type'} eq 'file') {
- my $fh = $self->{_fh};
- defined $fh ? tell($fh) : undef;
- }
- elsif ($self->{'_type'} eq 'unix.group' ||
- $self->{'_type'} eq 'array_reference') {
+ if ($self->{'_type'} eq 'file' ||
+ $self->{'_type'} eq 'unix.group' ||
+ $self->{'_type'} eq 'array_reference') {
$self->SUPER::getpos();
}
else {
@@ -304,12 +257,9 @@ sub setpos
{
my ($self, $pos) = @_;
- if ($self->{'_type'} eq 'file') {
- my $fh = $self->{_fh};
- seek($fh, $pos, 0);
- }
- elsif ($self->{'_type'} eq 'unix.group' ||
- $self->{'_type'} eq 'array_reference') {
+ if ($self->{'_type'} eq 'file' ||
+ $self->{'_type'} eq 'unix.group' ||
+ $self->{'_type'} eq 'array_reference') {
$self->SUPER::getpos($pos);
}
else {
@@ -322,12 +272,9 @@ sub eof
{
my ($self) = @_;
- if ($self->{'_type'} eq 'file') {
- my $fh = $self->{_fh};
- $fh->eof if defined $fh;
- }
- elsif ($self->{'_type'} eq 'unix.group' ||
- $self->{'_type'} eq 'array_reference') {
+ if ($self->{'_type'} eq 'file' ||
+ $self->{'_type'} eq 'unix.group' ||
+ $self->{'_type'} eq 'array_reference') {
$self->SUPER::eof();
}
else {
@@ -340,12 +287,10 @@ sub close
{
my ($self) = @_;
- if ($self->{'_type'} eq 'file') {
- $self->{_fh}->close if defined $self->{_fh};
- }
- elsif ($self->{'_type'} eq 'unix.group' ||
- $self->{'_type'} eq 'array_reference') {
- ;
+ if ($self->{'_type'} eq 'file' ||
+ $self->{'_type'} eq 'unix.group' ||
+ $self->{'_type'} eq 'array_reference') {
+ $self->SUPER::close();
}
else {
$self->_error_reason("Error: type=$self->{_type} is unknown type.");