diff options
| author | fukachan <fukachan> | 2002-08-21 14:31:05 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2002-08-21 14:31:05 +0000 |
| commit | c42d3df14ba47caead72122e630392e805f1f3b4 (patch) | |
| tree | 4c09d441706bb231b80afc492b9dacb11b819522 | |
| parent | ee16c2cd520c4b1c0e98ab4801b7e19dca4db3ef (diff) | |
| download | fml8-c42d3df14ba47caead72122e630392e805f1f3b4.tar.gz fml8-c42d3df14ba47caead72122e630392e805f1f3b4.tar.bz2 fml8-c42d3df14ba47caead72122e630392e805f1f3b4.zip | |
enhance add()
implement get_next_value()
define get_value_as_str($key), get_value_as_array_ref($key)
| -rw-r--r-- | fml/lib/IO/Adapter/File.pm | 135 |
1 files changed, 109 insertions, 26 deletions
diff --git a/fml/lib/IO/Adapter/File.pm b/fml/lib/IO/Adapter/File.pm index 33c33f91..fa0eb07e 100644 --- a/fml/lib/IO/Adapter/File.pm +++ b/fml/lib/IO/Adapter/File.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: File.pm,v 1.37 2002/08/19 15:06:35 fukachan Exp $ +# $FML: File.pm,v 1.38 2002/08/19 15:19:46 fukachan Exp $ # package IO::Adapter::File; @@ -14,6 +14,10 @@ use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD); use Carp; use IO::Adapter::ErrorStatus qw(error_set error error_clear); + +my $debug = 0; + + =head1 NAME IO::Adapter::File - IO functions for a file @@ -185,11 +189,6 @@ sub line_count return one line. It is the same as usual getline() call for a file. -=head2 C<get_next_value()> - -return one line suitable with C<fml> IO design. -This is used in C<fml5>. - =cut @@ -222,17 +221,6 @@ sub get_next_key } -# Descriptions: return value(s) for the next key -# Arguments: OBJ($self) -# Side Effects: none -# Return Value: STR -sub get_next_value -{ - my ($self) = @_; - $self->_get_next_xxx('value'); -} - - # Descriptions: get data and return key or value by $mode. # Arguments: OBJ($self) STR($mode) # Side Effects: none @@ -240,10 +228,9 @@ sub get_next_value sub _get_next_xxx { my ($self, $mode) = @_; - my ($buf) = ''; - my $fh = $self->{_fh}; + my $fh = $self->{_fh}; if (defined $fh) { INPUT: while ($buf = <$fh>) { @@ -260,17 +247,95 @@ sub _get_next_xxx if ($mode eq 'key') { $buf = $key; } - elsif ($mode eq 'value') { + elsif ($mode eq 'value_as_str') { $buf = $value; } + elsif ($mode eq 'value_as_array_ref') { + $value =~ s/^\s*//; + $value =~ s/\s*$//; + my (@buf) = split(/\s+/, $value); + print STDERR "[ @buf ]\n"; + $buf = \@buf; + } $ec++; } return $buf; } + return undef; } +=head2 get_value_as_str($key) + +return value(s) for the next key as STR. + +=head2 get_value_as_array_ref($key) + +return value(s) for the next key as ARRAY_REF. + +=cut + + +# Descriptions: return value(s) for the next key +# Arguments: OBJ($self) STR($key) +# Side Effects: none +# Return Value: STR +sub get_value_as_str +{ + my ($self, $key) = @_; + $self->_get_value($key, 'value_as_array_str'); +} + + +# Descriptions: return value(s) for the next key +# Arguments: OBJ($self) STR($key) +# Side Effects: none +# Return Value: ARRAY_REF +sub get_value_as_array_ref +{ + my ($self, $key) = @_; + $self->_get_value($key, 'value_as_array_ref'); +} + + +# Descriptions: return value(s) for the next key. +# XXX "key" should be uniq since "key" is used as a primary key. +# Arguments: OBJ($self) STR($key) STR($style) +# Side Effects: none +# Return Value: STR or ARRAY_REF +sub _get_value +{ + my ($self, $key, $style) = @_; + my $xkey = ''; + my $buf = ''; + + my $fh = $self->{_fh}; + if (defined $fh) { + LOOP: + while (<$fh>) { + ($xkey, $buf) = split(/\s+/, $_, 2); + if ($key eq $xkey) { last LOOP;} + } + + $fh->close(); + + if ($style eq 'value_as_str') { + return $buf + } + + if ($style eq 'value_as_array_ref') { + $buf =~ s/^\s*//; + $buf =~ s/\s*$//; + my @a = split(/\s+/, $buf); + return \@a; + } + } + + return $buf; +} + + =head2 C<getpos()> get the position in the opened file. @@ -340,7 +405,7 @@ sub close } -=head2 C<add($address)> +=head2 C<add($address, ... )> add (append) $address to this map. @@ -348,12 +413,12 @@ add (append) $address to this map. # Descriptions: add $addr into map -# Arguments: OBJ($self) STR($addr) +# Arguments: OBJ($self) STR($addr) VARARGS($argv) # Side Effects: update map # Return Value: same as close() sub add { - my ($self, $addr) = @_; + my ($self, $addr, $argv) = @_; $self->open("w"); @@ -366,14 +431,32 @@ sub add print $wh $_; } $fh->close; + + print STDERR "add: argv=$argv\tref=<", ref($argv), ">\n" if $debug; + + if (defined $argv) { + if (ref($argv) eq 'ARRAY') { + print $wh $addr, "\t", join("\t", @$argv), "\n"; + } + elsif (not ref($argv)) { + print $wh $addr, "\t", $argv, "\n"; + } + else { + $self->error_set("Error: add: invalid args"); + $wh->close; + return undef; + } + } + else { + print $wh $addr, "\n"; + } + + $wh->close; } else { $self->error_set("Error: cannot open file=$self->{ _file }"); return undef; } - - print $wh $addr, "\n"; - $wh->close; } |
