home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 9.ddi / IOSTRSR1.ZIP / FSBSKOFF.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.0 KB  |  63 lines

  1. /*[]------------------------------------------------------------[]*/
  2. /*|                                                              |*/
  3. /*|     fsbskoff.cpp                                             |*/
  4. /*|                                                              |*/
  5. /*|     Class filebuf                                            |*/
  6. /*|        streampos filebuf::seekoff( streamoff, ios::seek_dir, |*/
  7. /*|                                    int)                      |*/
  8. /*[]------------------------------------------------------------[]*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1990, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18. #include <ioconfig.h>
  19. #include "filesys.h"
  20. #include <fstream.h>
  21.  
  22. // Seek file to position.
  23. // We take a simple approach, and don't check for small position changes
  24. // within the current buffer.
  25.  
  26. streampos filebuf::seekoff(streamoff off, ios::seek_dir dir, int /* mode ignored */)
  27. {
  28.     long loff = off;
  29.     int count = out_waiting();
  30.     if( count )
  31.         {       // flush the output
  32.         if( ::write(xfd, pbase(), count) != count )
  33.             return EOF;
  34.         }
  35.     else if( dir == ios::cur )
  36.         if( (count = in_avail()) != 0)
  37.             {
  38.             loff -= count;
  39.  
  40.             //  if we're in text mode, need to allow for newlines
  41.             //  in the buffer
  42.             if( (mode & ios::binary) == 0 )
  43.                 {
  44.                 char *tptr = gptr();
  45.                 while( tptr != egptr() )
  46.                     if( *tptr++ == '\n' )
  47.                         loff--;
  48.                 }
  49.             }
  50.     int w = (dir == ios::beg) ? L_set : ((dir == ios::cur) ? L_cur : L_end);
  51.     last_seek = ::lseek(xfd, loff, w);
  52.     if( ! unbuffered()  &&  base() )
  53.         {      // set up get and put areas
  54.         int pb = (blen() > 8) ? 4 : 1;  // putback area size
  55.         char *b = base();
  56.         setp(b+pb, b+pb);
  57.         setg(b, b+pb, b+pb);
  58.         }
  59.     return (last_seek == long(OS_err)) ? EOF : last_seek;
  60. }
  61.  
  62.  
  63.