home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / U4WRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  2.0 KB  |  89 lines

  1.  
  2. /* u4write.c   (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved. */
  3.  
  4. #include "p4misc.h"
  5. #include "d4all.h"
  6. #include "u4error.h"
  7.  
  8. #ifndef UNIX
  9. #include <io.h>
  10. #endif
  11.  
  12. static int   hand ;
  13. static unsigned int  len_buffer, tot_len_buffer ;
  14. static unsigned int  bytes_avail ; /* Write- # of bytes left in the buffer */
  15.  
  16. static char H_PTR buffer ;
  17.  
  18. int  u4write( char H_PTR ptr, unsigned int len_ptr )
  19. {
  20.    unsigned int  first_len ;
  21.    if ( len_buffer == 0 )
  22.    {
  23.       if ( u4huge_write( hand, ptr, (long) len_ptr ) < 0 )
  24.          return -1 ;
  25.       return 0 ;
  26.    }
  27.  
  28.    if ( bytes_avail > 0 )
  29.    {
  30.       if ( bytes_avail >= len_ptr )
  31.       {
  32.      u4huge_cpy( buffer+ (len_buffer-bytes_avail), ptr, (long) len_ptr ) ;
  33.      bytes_avail -= len_ptr ;
  34.      return 0 ;
  35.       }
  36.       else
  37.       {
  38.      if ( u4write(ptr, first_len = bytes_avail) < 0)  return -1 ;
  39.      if ( u4write(ptr+first_len, len_ptr-first_len) < 0)  return -1 ;
  40.  
  41.      return 0 ;
  42.       }
  43.    }
  44.  
  45.    /* bytes_avail is zero */
  46.    if (  u4huge_write( hand, buffer, (long) len_buffer ) < 0 )
  47.       return -1 ;
  48.  
  49.    bytes_avail =  len_buffer =  tot_len_buffer ;
  50.  
  51.    if ( u4write(ptr,len_ptr) < 0 )  return -1 ;
  52.    return  0 ;
  53. }
  54.  
  55. int  u4write_flush()
  56. {
  57.    if ( buffer == (char H_PTR) 0 || len_buffer == bytes_avail)  return 0 ;
  58.  
  59.    if ( u4huge_write( hand, buffer, (long) len_buffer-bytes_avail ) < 0 )
  60.       return -1 ;
  61.  
  62.    bytes_avail =  len_buffer =  tot_len_buffer ;
  63.    return 0 ;
  64. }
  65.  
  66.  
  67. int  u4write_init( int handle, char H_PTR ptr, unsigned int len_ptr, 
  68.                    long start_offset )
  69. {
  70.    len_ptr &= 0xFC00 ;  /* Make it a multiple of 1K */
  71.  
  72.    if ( lseek( handle, start_offset, 0) != start_offset )
  73.    {
  74.       u4error( E_LSEEK, (char *) 0 ) ;
  75.       return -1 ;
  76.    }
  77.  
  78.    hand  =  handle ;
  79.    buffer=  ptr ;
  80.    tot_len_buffer =  len_ptr ;
  81.    if ( len_ptr == 0 )
  82.       bytes_avail =  len_buffer = 0 ;
  83.    else
  84.       bytes_avail =  len_buffer =  len_ptr - (0x400 - (unsigned) (start_offset % 0x400)) ;
  85.  
  86.    return 0 ;
  87. }
  88.  
  89.