# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
##
## Package to read/write on ASCII data connections
##

package Net::FTP::A;
use strict;
use vars qw(@ISA $buf $VERSION);
use Carp;

require Net::FTP::dataconn;

@ISA = qw(Net::FTP::dataconn);
$VERSION = "1.13"; # $Id$

sub read {
  my    $data 	 = shift;
  local *buf 	 = \$_[0]; shift;
  my    $size 	 = shift || croak 'read($buf,$size,[$offset])';
  my    $timeout = @_ ? shift : $data->timeout;

  if (length(${*$data}) < $size && !${*$data}{'net_ftp_eof'}) {
    my $blksize = ${*$data}{'net_ftp_blksize'};
    $blksize = $size if $size > $blksize;

    my $l = 0;
    my $n;

    READ:
    {
      my $readbuf = defined(${*$data}{'net_ftp_cr'}) ? "\015" : '';

      $data->can_read($timeout) or
	   croak "Timeout";

      if ($n = sysread($data, $readbuf, $blksize, length $readbuf)) {
        ${*$data}{'net_ftp_bytesread'} += $n;
	${*$data}{'net_ftp_cr'} = substr($readbuf,-1) eq "\015"
					? chop($readbuf)
					: undef;
      }
      else {
        return undef
	  unless defined $n;

        ${*$data}{'net_ftp_eof'} = 1;
      }

      $readbuf =~ s/\015\012/\n/sgo;
      ${*$data} .= $readbuf;

      unless (length(${*$data})) {

        redo READ
	  if($n > 0);

        $size = length(${*$data})
          if($n == 0);
      }
    }
  }

  $buf = substr(${*$data},0,$size);
  substr(${*$data},0,$size) = '';

  length $buf;
}

sub write {
  my    $data 	= shift;
  local *buf 	= \$_[0]; shift;
  my    $size 	= shift || croak 'write($buf,$size,[$timeout])';
  my    $timeout = @_ ? shift : $data->timeout;

  $data->can_write($timeout) or
	 croak "Timeout";

  (my $tmp = substr($buf,0,$size)) =~ s/\n/\015\012/sg;

  # If the remote server has closed the connection we will be signal'd
  # when we write. This can happen if the disk on the remote server fills up

  local $SIG{PIPE} = 'IGNORE';

  my $len = length($tmp);
  my $off = 0;
  my $wrote = 0;

  while($len) {
    $off += $wrote;
    $wrote = syswrite($data, substr($tmp,$off), $len);
    return undef
      unless defined($wrote);
    $len -= $wrote;
  }

  $size;
}

1;
