# PMemoryFile Class Reference

This class is used to allow a block of memory to substitute for a disk file. More...

`#include <memfile.h>`

Inheritance diagram for PMemoryFile:

List of all members.

## Public Member Functions

Construction
| PMemoryFile ()
| Create a new, empty, memory file.
| PMemoryFile (const PBYTEArray &data)
| Create a new memory file initialising to the specified content.
| ~PMemoryFile ()
| Destroy the memory file.
Overrides from class PObject
Comparison | Compare (const PObject &obj) const
| Determine the relative rank of the two objects.
Overrides from class PChannel
virtual PBoolean | Open (OpenMode mode=ReadWrite, int opts=ModeDefault)
| Open the current file in the specified mode and with the specified options.
virtual PBoolean | Open (const PFilePath &name, OpenMode mode=ReadWrite, int opts=ModeDefault)
| Open the specified file name in the specified mode and with the specified options.
virtual PBoolean | Close ()
| Close the channel, shutting down the link to the data source.
virtual PBoolean | Read (void *buf, PINDEX len)
| Low level read from the memory file channel.
virtual PBoolean | Write (const void *buf, PINDEX len)
| Low level write to the memory file channel.
Overrides from class PFile
virtual off_t | GetLength () const
| Get the current size of the file.
virtual PBoolean | SetLength (off_t len)
| Set the size of the file, padding with 0 bytes if it would require expanding the file, or truncating it if being made shorter.
virtual PBoolean | SetPosition (off_t pos, FilePositionOrigin origin=Start)
| Set the current active position in the file for the next read or write operation.
virtual off_t | GetPosition () const
| Get the current active position in the file for the next read or write operation.
Overrides from class PFile
const PBYTEArray & | GetData () const
| Get the memory data the file has operated with.

## Protected Attributes

PBYTEArray | m_data
off_t | m_position

## Detailed Description

This class is used to allow a block of memory to substitute for a disk file.

## Constructor & Destructor Documentation

PMemoryFile::PMemoryFile | ( |  | ) |

Create a new, empty, memory file.

PMemoryFile::PMemoryFile | ( | const PBYTEArray & | data | ) |

Create a new memory file initialising to the specified content.

Parameters:

  data | New content filr memory file.

PMemoryFile::~PMemoryFile | ( |  | ) |

Destroy the memory file.

## Member Function Documentation

virtual PBoolean PMemoryFile::Close | ( |  | ) | `[virtual]`

Close the channel, shutting down the link to the data source.

Returns:
  true if the channel successfully closed.

Reimplemented from PFile.

Comparison PMemoryFile::Compare | ( | const PObject & | obj | ) | const`[virtual]`

Determine the relative rank of the two objects.

This is essentially the string comparison of the `PFilePath` names of the files.

Returns:
  relative rank of the file paths.

Parameters:

  obj | Other file to compare against.

Reimplemented from PFile.

const PBYTEArray& PMemoryFile::GetData | ( |  | ) | const`[inline]`

Get the memory data the file has operated with.

virtual off_t PMemoryFile::GetLength | ( |  | ) | const`[virtual]`

Get the current size of the file.

The size of the file corresponds to the size of the data array.

Returns:
  length of file in bytes.

Reimplemented from PFile.

virtual off_t PMemoryFile::GetPosition | ( |  | ) | const`[virtual]`

Get the current active position in the file for the next read or write operation.

Returns:
  current file position relative to start of file.

Reimplemented from PFile.

virtual PBoolean PMemoryFile::Open | ( | const PFilePath & | name,
|  | OpenMode | mode = `ReadWrite`,
|  | int | opts = `ModeDefault` |
| ) |  |  | `[virtual]`

Open the specified file name in the specified mode and with the specified options.

If the file object already has an open file then it is closed.

Note: if `mode` is StandardInput, StandardOutput or StandardError, then the `name` parameter is ignored.

Returns:
  true if the file was successfully opened.

virtual PBoolean PMemoryFile::Open | ( | OpenMode | mode = `ReadWrite`,
|  | int | opts = `ModeDefault` |
| ) |  |  | `[virtual]`

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.

virtual PBoolean PMemoryFile::Read | ( | void * | buf,
|  | PINDEX | len |
| ) |  |  | `[virtual]`

Low level read from the memory file channel.

The read timeout is ignored. The GetLastReadCount() function returns the actual number of bytes read.

The GetErrorCode() function should be consulted after Read() returns false to determine what caused the failure.

Returns:
  true indicates that at least one character was read from the channel. false means no bytes were read due to timeout or some other I/O error.

Parameters:

  buf | Pointer to a block of memory to receive the read bytes.
  len | Maximum number of bytes to read into the buffer.

Reimplemented from PFile.

virtual PBoolean PMemoryFile::SetLength | ( | off_t | len | ) | `[virtual]`

Set the size of the file, padding with 0 bytes if it would require expanding the file, or truncating it if being made shorter.

Returns:
  true if the file size was changed to the length specified.

Parameters:

  len | New length of file.

Reimplemented from PFile.

virtual PBoolean PMemoryFile::SetPosition | ( | off_t | pos,
|  | FilePositionOrigin | origin = `Start` |
| ) |  |  | `[virtual]`

Set the current active position in the file for the next read or write operation.

The `pos` variable is a signed number which is added to the specified origin. For `origin == PFile::Start` only positive values for `pos` are meaningful. For `origin == PFile::End` only negative values for `pos` are meaningful.

Returns:
  true if the new file position was set.

Parameters:

  pos | New position to set.
  origin | Origin for position change.

virtual PBoolean PMemoryFile::Write | ( | const void * | buf,
|  | PINDEX | len |
| ) |  |  | `[virtual]`

Low level write to the memory file channel.

The write timeout is ignored. The GetLastWriteCount() function returns the actual number of bytes written.

The GetErrorCode() function should be consulted after Write() returns false to determine what caused the failure.

Returns:
  true if at least len bytes were written to the channel.

Parameters:

  buf | Pointer to a block of memory to write.
  len | Number of bytes to write.

Reimplemented from PFile.

## Member Data Documentation

PBYTEArray PMemoryFile::m_data `[protected]`

off_t PMemoryFile::m_position `[protected]`

The documentation for this class was generated from the following file:

- memfile.h