home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / NTSTREAM.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  171 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /* --------------------------------------------------------------------
  4.    Module:     ntstream.cpp
  5.    Subject:    Share-Aware File Streams
  6.    Author:     Heinz Ozwirk
  7.    Started:    30.05.1993 15:30:32
  8.    Modified:   31.05.1993 10:33:35
  9.    --------------------------------------------------------------------
  10.    Description: public domain from the FidoNet C++ echo
  11.    --------------------------------------------------------------------
  12.    History:    (insert new entries at top of list)
  13.    dd.mm.yyyy/ho description
  14.    -------------------------------------------------------------------- */
  15.  
  16.  
  17. /* --- Includes ------------------------------------------------------- */
  18. #include "ntstream.h"
  19. #include <fcntl.h>
  20. #include <io.h>
  21.  
  22. /* --- Defines -------------------------------------------------------- */
  23. /* --- Constants ------------------------------------------------------ */
  24. /* --- Types ---------------------------------------------------------- */
  25. /* --- Prototypes ----------------------------------------------------- */
  26. /* --- Global Variables ----------------------------------------------- */
  27. /* --- Local Variables ------------------------------------------------ */
  28. /* --- Implementation ------------------------------------------------- */
  29.  
  30. static int newMode(int m)
  31. {
  32.    int how = 0;
  33.    if (m & nfstream::sh_compat)        how |= SH_COMPAT;
  34.    else if (m & nfstream::sh_none)     how |= SH_DENYRW;
  35.    else if (m & nfstream::sh_read)
  36.       {
  37.       if (m & nfstream::sh_write)      how |= SH_DENYNO;
  38.       else                             how |= SH_DENYWR;
  39.       }
  40.    else if (m & nfstream::sh_write)    how |= SH_DENYRD;
  41.    else                                how |= SH_DENYRW;
  42.  
  43.    if (m & ios::out)
  44.       {
  45.       if (m & ios::in)
  46.          how |= O_RDWR;
  47.       else
  48.          how |= O_WRONLY;
  49.       if (!(m & ios::nocreate))
  50.          {
  51.          how |= O_CREAT;
  52.          if (m & ios::noreplace)
  53.             how |= O_EXCL;
  54.          }
  55.          if (m & ios::trunc)
  56.             how |= O_TRUNC;
  57.       }
  58.     else
  59.       {
  60.       how |= O_RDONLY;
  61.       }
  62.  
  63.    if (m & ios::binary)
  64.       how |= O_BINARY;
  65.    else
  66.       how |= O_TEXT;
  67.  
  68.    if (m & ios::app)
  69.       how |= O_APPEND;
  70.  
  71.    return how;
  72. }
  73.  
  74. nfstream::nfstream(const signed char *name, int mode, int prot)
  75.    :  fstream()
  76. {
  77.    open(name, mode, prot);
  78. }
  79.  
  80. nfstream::nfstream(const unsigned char *name, int mode, int prot)
  81.    :  fstream()
  82. {
  83.    open(name, mode, prot);
  84. }
  85.  
  86. void nfstream::open(const signed char *name, int mode, int prot)
  87. {
  88.    int howM = newMode(mode);
  89.    fd = ::open((const char *)name, howM, prot);
  90.    if (fd == -1)
  91.       setstate(failbit);
  92.    else
  93.       attach(fd);
  94. }
  95.  
  96. void nfstream::close()
  97. {
  98.    if (fd != -1)
  99.       {
  100.       fstream::close();
  101.       ::close(fd);
  102.       fd = -1;
  103.       }
  104. }
  105.  
  106. nifstream::nifstream(const signed char *name, int mode, int prot)
  107.    :  ifstream()
  108. {
  109.    open(name, mode, prot);
  110. }
  111.  
  112. nifstream::nifstream(const unsigned char *name, int mode, int prot)
  113.    :  ifstream()
  114. {
  115.    open(name, mode, prot);
  116. }
  117.  
  118. void nifstream::open(const signed char *name, int mode, int prot)
  119. {
  120.    int howM = newMode(mode);
  121.    fd   = ::open((const char *)name, howM, prot);
  122.    if (fd == -1)
  123.       setstate(failbit);
  124.    else
  125.       attach(fd);
  126. }
  127.  
  128. void nifstream::close()
  129. {
  130.    if (fd != -1)
  131.       {
  132.       ifstream::close();
  133.       ::close(fd);
  134.       fd = -1;
  135.       }
  136. }
  137.  
  138. nofstream::nofstream(const signed char *name, int mode, int prot)
  139.    :  ofstream()
  140. {
  141.    open(name, mode, prot);
  142. }
  143.  
  144. nofstream::nofstream(const unsigned char *name, int mode, int prot)
  145.    :  ofstream()
  146. {
  147.    open(name, mode, prot);
  148. }
  149.  
  150. void nofstream::open(const signed char *name, int mode, int prot)
  151. {
  152.    int howM = newMode(mode);
  153.    fd   = ::open((const char *)name, howM, prot);
  154.    if (fd == -1)
  155.       setstate(failbit);
  156.    else
  157.       attach(fd);
  158. }
  159.  
  160. void nofstream::close()
  161. {
  162.    if (fd != -1)
  163.       {
  164.       ofstream::close();
  165.       ::close(fd);
  166.       fd = -1;
  167.       }
  168. }
  169.  
  170. /* --- End of File ---------------------------------------------------- */
  171.