OpenTTD Source: ByteBuffer Class Reference

OpenTTD Source 12.0-beta1

Public Member Functions | Private Attributes
ByteBuffer Class Reference

Owning byte buffer readable as a stream. More...

## Public Member Functions
| ByteBuffer (FILE *file, size_t len)
| Construct buffer from data in a file. More...
| ~ByteBuffer ()
| Destructor, frees the buffer.
bool | IsValid () const
| Return whether the buffer was constructed successfully. More...
bool | IsEnd () const
| Return whether reading has reached the end of the buffer. More...
bool | ReadByte (byte &b)
| Read a single byte from the buffer. More...
bool | ReadVariableLength (uint32 &res)
| Read a MIDI file variable length value. More...
bool | ReadBuffer (byte *dest, size_t length)
| Read bytes into a buffer. More...
bool | ReadDataBlock (MidiFile::DataBlock *dest, size_t length)
| Read bytes into a MidiFile::DataBlock. More...
bool | Skip (size_t count)
| Skip over a number of bytes in the buffer. More...
bool | Rewind (size_t count)
| Go a number of bytes back to re-read. More...

## Private Attributes
byte * | buf
size_t | buflen
size_t | pos

## Detailed Description

Owning byte buffer readable as a stream.

RAII-compliant to make teardown in error situations easier.

Definition at line 61 of file midifile.cpp.

## Constructor & Destructor Documentation

## ◆ ByteBuffer()

ByteBuffer::ByteBuffer | ( | FILE * | file,
|  | size_t | len
| ) |  |
| inline

Construct buffer from data in a file.

If file does not have sufficient bytes available, the object is constructed in an error state, that causes all further function calls to fail.

Parameters

  file | file to read from at current position
  len | number of bytes to read

Definition at line 73 of file midifile.cpp.

## Member Function Documentation

## ◆ IsEnd()

bool ByteBuffer::IsEnd | ( |  | ) | const
| inline

Return whether reading has reached the end of the buffer.

Returns
  true if there are no more bytes available to read

Definition at line 106 of file midifile.cpp.

Referenced by ReadBuffer(), ReadByte(), ReadDataBlock(), ReadVariableLength(), and Skip().

## ◆ IsValid()

bool ByteBuffer::IsValid | ( |  | ) | const
| inline

Return whether the buffer was constructed successfully.

Returns
  true is the buffer contains data

Definition at line 97 of file midifile.cpp.

## ◆ ReadBuffer()

bool ByteBuffer::ReadBuffer | ( | byte * | dest,
|  | size_t | length
| ) |  |
| inline

Read bytes into a buffer.

Parameters

  [out] | dest | buffer to copy into
  | length | number of bytes to read

Returns
  true if the requested number of bytes were available

Definition at line 148 of file midifile.cpp.

References IsEnd().

## ◆ ReadByte()

bool ByteBuffer::ReadByte | ( | byte & | b | ) |
| inline

Read a single byte from the buffer.

Parameters

  [out] | b | returns the read value

Returns
  true if a byte was available for reading

Definition at line 116 of file midifile.cpp.

References IsEnd().

## ◆ ReadDataBlock()

bool ByteBuffer::ReadDataBlock | ( | MidiFile::DataBlock * | dest,
|  | size_t | length
| ) |  |
| inline

Read bytes into a MidiFile::DataBlock.

Parameters

  [out] | dest | DataBlock to copy into
  | length | number of bytes to read

Returns
  true if the requested number of bytes were available

Definition at line 163 of file midifile.cpp.

References MidiFile::DataBlock::data, and IsEnd().

## ◆ ReadVariableLength()

bool ByteBuffer::ReadVariableLength | ( | uint32 & | res | ) |
| inline

Read a MIDI file variable length value.

Each byte encodes 7 bits of the value, most-significant bits are encoded first. If the most significant bit in a byte is set, there are further bytes encoding the value.

Parameters

  [out] | res | returns the read value

Returns
  true if there was data available

Definition at line 130 of file midifile.cpp.

References IsEnd().

## ◆ Rewind()

bool ByteBuffer::Rewind | ( | size_t | count | ) |
| inline

Go a number of bytes back to re-read.

Parameters

  count | number of bytes to go back

Returns
  true if at least count bytes had been read previously

Definition at line 190 of file midifile.cpp.

## ◆ Skip()

bool ByteBuffer::Skip | ( | size_t | count | ) |
| inline

Skip over a number of bytes in the buffer.

Parameters

  count | number of bytes to skip over

Returns
  true if there were enough bytes available

Definition at line 177 of file midifile.cpp.

References IsEnd().

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

- src/music/midifile.cpp