home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-1.ZIP / GCOMM / _ASCIIS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-14  |  8.8 KB  |  312 lines

  1. /*
  2.  * _ASCIIS.C
  3.  *
  4.  * Contains:    _AsciiSend()
  5.  *              _AsciiSendExecuteLineDelay()
  6.  *              _AsciiSendLoadUpBuffer()
  7.  *              _AsciiSendTheBuffer()
  8.  *              _AsciiSendEchoSomeCharacters()
  9.  *
  10.  *
  11.  * The Greenleaf Comm Library
  12.  *
  13.  * Copyright (C) 1989-90 Greenleaf Software Inc.  All Rights Reserved.
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include "gf.h"
  19. #include "asiports.h"
  20. #include "xfer.h"
  21. #include "_xfer.h"
  22.  
  23. /*
  24.  * Local prototypes for static routines.
  25.  */
  26.  
  27. static int GF_CONV _AsciiSendExecuteLineDelay(XFER *ascii);
  28. static int GF_CONV _AsciiSendLoadUpBuffer(XFER *ascii);
  29. static int GF_CONV _AsciiSendTheBuffer(XFER *ascii);
  30. static int GF_CONV _AsciiSendEchoSomeCharacters(XFER *ascii);
  31.  
  32.  
  33. /*
  34.  * int _AsciiSend( XFER *ascii )
  35.  *
  36.  * ARGUMENTS
  37.  *
  38.  *  XFER *ascii:                       This is a pointer to an XFER block that
  39.  *                                     keeps track of how things are set up
  40.  *                                     for the file transfer.
  41.  *
  42.  * DESCRIPTION
  43.  *
  44.  * This routine is the private driver for performing an ascii file send.
  45.  * The logic for the routine is fairly simple.  The routine sits in a
  46.  * big loop where it first fills up the buffer with a line of data.  A line
  47.  * is ended by either the first CR or LF, or when the line gets too long.
  48.  * The line is then stuffed out into the transmit buffer.  This continues
  49.  * until the entire file has been transmitted.  Although there is no
  50.  * handshaking in place, a user defined delay is executed after each line
  51.  * is transmitted.
  52.  *
  53.  * SIDE EFFECTS
  54.  *
  55.  * File may have been transfered.
  56.  *
  57.  * RETURNS
  58.  *
  59.  * The routine returns one of the defined XFER_RETURN codes defined
  60.  * in XFER.H.
  61.  *
  62.  * AUTHOR
  63.  *  Mark Nelson          29-Aug-1989  20:57:40.92
  64.  *
  65.  * MODIFICATIONS
  66.  *
  67.  */
  68. int GF_CONV _AsciiSend( XFER *ascii )
  69. {
  70.     ascii -> sending = TRUE;
  71.     if ( !_XferInitialize( ascii ) )/* The initialization routine sets up the */
  72.         return( FALSE );            /* buffers and some other stuff.          */
  73.  
  74.     if ( !_XferOpenFile( ascii ) )  /* Here is where I open the file to send. */
  75.         return( FALSE );
  76. /*
  77.  * This big loop is real easy.  Load a buffer up with file data, send it, then
  78.  * execute a fixed length delay.  Do it enough times and the whole file is gone.
  79.  */
  80.  
  81.     for ( ; ; ) {
  82.         if ( !_AsciiSendLoadUpBuffer( ascii ) )
  83.             return( TRUE );
  84.         if ( !_AsciiSendTheBuffer( ascii ) )
  85.             return( FALSE );
  86.         if ( !_AsciiSendExecuteLineDelay( ascii ) )
  87.             return( FALSE );
  88.     }
  89. }
  90.  
  91. /*
  92.  * static int _AsciiSendExecuteLineDelay( XFER *ascii )
  93.  *
  94.  * ARGUMENTS
  95.  *
  96.  *  XFER *ascii:                       This is a pointer to an XFER block that
  97.  *                                     keeps track of how things are set up
  98.  *                                     for the file transfer.
  99.  *
  100.  * DESCRIPTION
  101.  *
  102.  * This routine is a private subroutine for the _AsciiSend driver. Its job
  103.  * is to execute a fixed length delay that is specified in the parameter
  104.  * block.  All it has to do while this is going on is to make sure nobody
  105.  * presses the abort key.
  106.  *
  107.  * SIDE EFFECTS
  108.  *
  109.  *
  110.  * RETURNS
  111.  *
  112.  * This routine returns FALSE if an abort has occurred, otherwise it
  113.  * returns a TRUE.
  114.  *
  115.  * AUTHOR
  116.  *  Mark Nelson          27-Sep-1989  20:57:40.92
  117.  *
  118.  * MODIFICATIONS
  119.  *
  120.  */
  121. static int GF_CONV _AsciiSendExecuteLineDelay( XFER *ascii )
  122. {
  123.     int seconds_left;
  124.  
  125.     seconds_left = ascii->x.ascii.delay_ticks_per_line;
  126.     while ( seconds_left > 0 ) {
  127.         if ( _XferAbortKeyPressed( ascii ) )
  128.             return( FALSE );
  129.         timer( 1 );
  130.         seconds_left--;
  131.     }
  132.     return( TRUE );
  133. }
  134.  
  135. /*
  136.  * static int _AsciiSendLoadUpBuffer( XFER *ascii )
  137.  *
  138.  *
  139.  * ARGUMENTS
  140.  *
  141.  *  XFER *ascii:                       This is a pointer to an XFER block that
  142.  *                                     keeps track of how things are set up
  143.  *                                     for the file transfer.
  144.  *
  145.  * DESCRIPTION
  146.  *
  147.  * This routine is a private subroutine for the _AsciiSend driver. Its job
  148.  * is to fill up the buffer in the parameter block with a line full of data.
  149.  * A line is defined as a sequence of characters terminated by a '\n', or a
  150.  * '\r', or 81 characters.
  151.  *
  152.  * SIDE EFFECTS
  153.  *
  154.  * The file pointer has been advanced, and the data moved into the buffer.
  155.  *
  156.  * RETURNS
  157.  *
  158.  * In the usual sense, this guy returns a FALSE if it is time to stop
  159.  * processing.  However, in this case the FALSE means that a normal exit
  160.  * needs to occur because we have reached an EOF.  There is no error exit
  161.  * from this routine.
  162.  *
  163.  * AUTHOR
  164.  *  Mark Nelson          27-Sep-1989  20:57:40.92
  165.  *
  166.  * MODIFICATIONS
  167.  *
  168.  */
  169. static int GF_CONV _AsciiSendLoadUpBuffer( XFER *ascii )
  170. {
  171.     int count;
  172.     int c;
  173. /*
  174.  * This is another forever loop.  The only way out is when the line is
  175.  * full.  This happens when an EOF is hit, or an end of line condition
  176.  * occurs.
  177.  */
  178.  
  179.     count = 0;
  180.     for ( ; ; ) {
  181.         c = getc( ascii->file );
  182.         if ( c == EOF ) {
  183.             if ( count == 0 ) {
  184.                 _XferCleanup( ascii );
  185.                 return( FALSE );
  186.             }
  187.             else
  188.                 break;
  189.         }
  190.         ascii->byte_count++;
  191.         if ( !ascii->x.ascii.strip_lf || c != '\n' )
  192.             ascii->buffer[ count++ ] = ( char ) c;
  193.         else
  194.             continue;
  195.         if ( count > 81 )
  196.             break;
  197.         if ( c == '\r' )
  198.             break;
  199.     }
  200.     ascii->current_block_size = count;
  201.     return( TRUE );
  202. }
  203.  
  204. /*
  205.  * static int _AsciiSendTheBuffer( XFER *ascii )
  206.  *
  207.  *
  208.  * ARGUMENTS
  209.  *
  210.  *  XFER *ascii:                       This is a pointer to an XFER block that
  211.  *                                     keeps track of how things are set up
  212.  *                                     for the file transfer.
  213.  *
  214.  * DESCRIPTION
  215.  *
  216.  * This routine is a private subroutine for the _AsciiSend driver. Its job
  217.  * is to send the data in the buffer out through the com port.  While it is
  218.  * doing that, it also echoes any data that comes back from the remote end,
  219.  * using the user specified echo routine.  In addition, it does the usual
  220.  * checking for error conditions.
  221.  *
  222.  * SIDE EFFECTS
  223.  *
  224.  *
  225.  * RETURNS
  226.  *
  227.  * In the usual sense, this guy returns a FALSE if it is time to stop
  228.  * processing.  This can be because any one of several abort conditions
  229.  * have occurred. A TRUE means everything is okay, and processing should
  230.  * continue.
  231.  *
  232.  * AUTHOR
  233.  *  Mark Nelson          27-Sep-1989  20:57:40.92
  234.  *
  235.  * MODIFICATIONS
  236.  *
  237.  */
  238.  
  239. static int GF_CONV _AsciiSendTheBuffer( XFER *ascii )
  240. {
  241.     int count;
  242.  
  243.     count = 0;
  244.     while ( count < ascii->current_block_size ) {
  245.         if ( _XferAbortKeyPressed( ascii ) )
  246.             return( FALSE );
  247. /*
  248.  * While the buffer isn't full, output characters from the buffer.  While
  249.  * I am doing it I am still checking for the abort key.  If the buffer gets
  250.  * fill, I fall down to the next part of this loop where I read characters in.
  251.  */
  252.        while ( !istxfull( ascii->port ) ) {
  253.           if ( _XferAbortKeyPressed( ascii ) )
  254.              return( FALSE );
  255.           if ( asiputc( ascii->port, ascii->buffer[ count++ ] ) != ASSUCCESS ) {
  256.              ascii->return_status = XFER_RETURN_CANT_PUT_CHAR;
  257.              _XferCleanup( ascii );
  258.              return( FALSE );
  259.           }
  260.           if ( count >= ascii->current_block_size )
  261.              break;
  262.        }
  263.        if ( !_AsciiSendEchoSomeCharacters( ascii ) )
  264.           return( FALSE );
  265.     }
  266.     return( TRUE );
  267. }
  268.  
  269. /*
  270.  * static int _AsciiSendEchoSomeCharacters( XFER *ascii )
  271.  *
  272.  *
  273.  * ARGUMENTS
  274.  *
  275.  *  XFER *ascii:                       This is a pointer to an XFER block that
  276.  *                                     keeps track of how things are set up
  277.  *                                     for the file transfer.
  278.  *
  279.  * DESCRIPTION
  280.  *
  281.  * This routine is a private subroutine for the _AsciiSend driver. Its job
  282.  * is to echo any data coming back from the remote end.  It does this by
  283.  * repeatedly calling the user specified echo routine.
  284.  *
  285.  * SIDE EFFECTS
  286.  *
  287.  *
  288.  * RETURNS
  289.  *
  290.  * In the usual sense, this guy returns a FALSE if it is time to stop
  291.  * processing.  This is usually because of a keyboard abort key.
  292.  * A TRUE means everything is okay, and processing should
  293.  * continue.
  294.  *
  295.  * AUTHOR
  296.  *  Mark Nelson          27-Sep-1989  20:57:40.92
  297.  *
  298.  * MODIFICATIONS
  299.  *
  300.  */
  301. static int GF_CONV _AsciiSendEchoSomeCharacters( XFER *ascii )
  302. {
  303.     while ( !isrxempty( ascii->port ) ) {
  304.         if ( _XferAbortKeyPressed( ascii ) )
  305.             return( FALSE );
  306.         if ( ascii->x.ascii.received_character_printer != NULL )
  307.             ascii->x.ascii.received_character_printer( asigetc( ascii->port ) );
  308.     }
  309.     return( TRUE );
  310. }
  311.  
  312.