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

  1. /*[]------------------------------------------------------------[]*/
  2. /*|                                                              |*/
  3. /*|     fsbopen.cpp                                              |*/
  4. /*|                                                              |*/
  5. /*|     Class filebuf                                            |*/
  6. /*|          filebuf* filebuf::open( const char *, int, int )    |*/
  7. /*|                                                              |*/
  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. /*
  23.  * Open named file with mode and protection, attach to this filebuf.
  24.  * We make no assumptions about the mode bits and what ::open() wants to
  25.  * see, except we assume that some mapping is possible.
  26.  * "prot" is supplied by user, assumed consistent with operating system.
  27.  * Note checks against OS_err, assumed to be operating system error return.
  28.  */
  29.  
  30. filebuf* filebuf::open(const char* name, int m, int prot)
  31. {
  32.     if( opened  || ! m )
  33.         return 0;
  34.  
  35.     // set up "how" parameter to system ::open()
  36.     // we let the OS decide whether the combination is legal
  37.     int how;
  38.     if( m & ios::out )
  39.         {
  40.         if( m & ios::in )
  41.             how = O_rdwr;
  42.         else
  43.             how = O_wronly;
  44.         if( ! (m & ios::nocreate) )
  45.             {
  46.             how |= O_create;
  47.             if( m & ios::noreplace )
  48.                 how |= O_excl;
  49.             }
  50.         if( m & ios::trunc )
  51.             how |= O_trunc;
  52.         }
  53.     else if( m & ios::in )
  54.         how = O_rdonly;
  55.     else
  56.         return 0;   // must specfify in, out, or in/out
  57.     if( m & ios::binary )
  58.         how |= O_BINARY;
  59.     else
  60.         how |= O_TEXT;
  61.     if( m & ios::app )
  62.         how |= O_append;
  63.  
  64.     // now try to open
  65.     int f = ::open(name, how, prot);
  66.     if( f == OS_err )
  67.         return 0;
  68.  
  69.     // finish up
  70.     xfd = f;
  71.     opened = 1;
  72.     mode = m;
  73.     last_seek = ::lseek(f, 0L, (m & ios::ate) ? L_end : L_set);
  74.     if( last_seek == long(OS_err) )
  75.         return 0;
  76.  
  77.     char *b = base();       // buffer address
  78.     int pb = b ? ((blen() > 8) ? 4 : 1) : 0;    // putback area size
  79.     setp(b+pb, b+pb);
  80.     setg(b, b+pb, b+pb);
  81.  
  82.     return this;
  83. }
  84.  
  85.  
  86.