home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / FILEBUF.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.6 KB  |  122 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - filebuf.cpp
  3.  * C++ stream I/O functions for handling filebufs
  4.  *-----------------------------------------------------------------------*/
  5.  
  6. /*[]------------------------------------------------------------[]*/
  7. /*|                                                              |*/
  8. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  9. /*|                                                              |*/
  10. /*|                                                              |*/
  11. /*|     Copyright (c) 1990 by Borland International              |*/
  12. /*|     All Rights Reserved.                                     |*/
  13. /*|                                                              |*/
  14. /*[]------------------------------------------------------------[]*/
  15.  
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <stream.h>
  19.  
  20. // tie to existing stdio file
  21. _Cdecl filebuf::filebuf( FILE *f )
  22. {
  23.     if( f ) {
  24.     file = f;
  25.     isopen = 1;
  26.     fd = f->fd;
  27.     }
  28.     else {
  29.     isopen = 0;
  30.     fd = 0;
  31.     }
  32. }
  33.  
  34.  
  35. // given a file descriptor, convert to stdio FILE
  36. static FILE * _Cdecl fd_to_file( int fd )
  37. {
  38.     char *mode;
  39.     switch( _openfd[fd] & (O_RDONLY | O_WRONLY | O_RDWR) ) {
  40.     case O_RDONLY:
  41.         mode = "r";
  42.         break;
  43.     case O_RDWR: 
  44.     case O_WRONLY:
  45.         if( _openfd[fd] & O_APPEND ) mode = "a";
  46.         else mode = "w";
  47.         break;
  48.     default:
  49.         mode = 0;    // error
  50.         break;
  51.     }
  52.     return mode ? fdopen(fd, mode) : 0;
  53. }
  54.  
  55.  
  56. // tie to existing file
  57. _Cdecl filebuf::filebuf( int nfd )
  58. {
  59.     isopen = 0;
  60.     fd = 0;
  61.     file =  fd_to_file(nfd);
  62.     if( file ) {
  63.     isopen = 1;
  64.     fd = nfd;
  65.     }
  66. }
  67.  
  68.  
  69. // tie to file, user buffer
  70. _Cdecl filebuf::filebuf( int nfd, char *buf, int size )
  71. {
  72.     isopen = 0;
  73.     fd = 0;
  74.     file =  fd_to_file(nfd);
  75.     if( file ) {
  76.     isopen = 1;
  77.     fd = nfd;
  78.     (void) ::setvbuf(file, buf, _IOFBF, size);
  79.     }
  80. }
  81.  
  82.  
  83. // open a file
  84. filebuf * _Cdecl filebuf::open( char *name, int om )
  85. {
  86.     char *mode = 0;
  87.  
  88.     switch( om ) {
  89.     case input: mode = "r"; break;
  90.     case output: mode = "w"; break;
  91.     case append: mode = "a"; break;
  92.     default: break;    // error
  93.     }
  94.     if( mode ) {
  95.     file = fopen(name, mode);
  96.     if( file ) {
  97.         fd = file->fd;
  98.         return this;
  99.     }
  100.     }
  101.     return 0;
  102. }
  103.  
  104.  
  105. // close a file
  106. int _Cdecl filebuf::close()
  107. {
  108.     int ret;
  109.  
  110.     if( file ) {
  111.     ret = fclose(file);
  112.     file = 0;
  113.     }
  114.     else ret = ::close(fd);
  115.     isopen = 0;
  116.  
  117.     return ret;
  118. }
  119.  
  120.  
  121. // overflow and underflow do not get called, since Standard I/O is used.
  122.