Logo Search packages:      
Sourcecode: pwlib version File versions

BOOL PFile::Open ( OpenMode  mode = ReadWrite,
int  opts = ModeDefault 
) [virtual, inherited]

Open the current file in the specified mode and with the specified options. If the file object already has an open file then it is closed.

If there has not been a filename attached to the file object (via SetFilePath()#, the name# parameter or a previous open) then a new unique temporary filename is generated.

Returns:
TRUE if the file was successfully opened.

Reimplemented in PWAVFile.

Definition at line 1088 of file ptlib.cxx.

References PFile::Close(), PChannel::ConvertOSError(), PFile::Create, PFile::DenySharedRead, PFile::DenySharedWrite, PFile::Exclusive, PString::IsEmpty(), PFile::ModeDefault, PFile::MustExist, PChannel::os_handle, PFile::ReadOnly, PFile::ReadWrite, PFile::removeOnClose, PFile::Temporary, PFile::Truncate, and PFile::WriteOnly.

Referenced by PWAVFile::Open(), PFile::PFile(), and PTextFile().

{
  Close();
  clear();

  if (path.IsEmpty())
    path = PFilePath("PWL", NULL);

  int oflags = IsTextFile() ? _O_TEXT : _O_BINARY;
  switch (mode) {
    case ReadOnly :
      oflags |= O_RDONLY;
      if (opts == ModeDefault)
        opts = MustExist;
      break;

    case WriteOnly :
      oflags |= O_WRONLY;
      if (opts == ModeDefault)
        opts = Create|Truncate;
      break;

    case ReadWrite :
      oflags |= O_RDWR;
      if (opts == ModeDefault)
        opts = Create;
      break;

    default :
      PAssertAlways(PInvalidParameter);
  }

  if ((opts&Create) != 0)
    oflags |= O_CREAT;
  if ((opts&Exclusive) != 0)
    oflags |= O_EXCL;
  if ((opts&Truncate) != 0)
    oflags |= O_TRUNC;

  if ((opts&Temporary) != 0)
    removeOnClose = TRUE;

  int sflags = _SH_DENYNO;
  if ((opts&DenySharedRead) == DenySharedRead)
    sflags = _SH_DENYRD;
  else if ((opts&DenySharedWrite) == DenySharedWrite)
    sflags = _SH_DENYWR;
  else if ((opts&(DenySharedRead|DenySharedWrite)) != 0)
    sflags = _SH_DENYWR;

  os_handle = _sopen(path, oflags, sflags, S_IREAD|S_IWRITE);

  // As ConvertOSError tests for < 0 and some return values _sopen may be
  // negative, only pass -1 through.
  return ConvertOSError(os_handle == -1 ? -1 : 0);
}


Generated by  Doxygen 1.6.0   Back to index