1
0
Fork 0

Codechange: Use std::vector for midifile's ByteBuffer. (#11019)

pull/11020/head
PeterN 2023-06-16 17:04:46 +01:00 committed by GitHub
parent 1a3d1bca59
commit fc2510e2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 21 deletions

View File

@ -60,8 +60,7 @@ const byte *MidiGetStandardSysexMessage(MidiSysexMessage msg, size_t &length)
* RAII-compliant to make teardown in error situations easier. * RAII-compliant to make teardown in error situations easier.
*/ */
class ByteBuffer { class ByteBuffer {
byte *buf; std::vector<byte> buf;
size_t buflen;
size_t pos; size_t pos;
public: public:
/** /**
@ -73,31 +72,22 @@ public:
*/ */
ByteBuffer(FILE *file, size_t len) ByteBuffer(FILE *file, size_t len)
{ {
this->buf = MallocT<byte>(len); this->buf.resize(len);
if (fread(this->buf, 1, len, file) == len) { if (fread(this->buf.data(), 1, len, file) == len) {
this->buflen = len;
this->pos = 0; this->pos = 0;
} else { } else {
/* invalid state */ /* invalid state */
this->buflen = 0; this->buf.clear();
} }
} }
/**
* Destructor, frees the buffer.
*/
~ByteBuffer()
{
free(this->buf);
}
/** /**
* Return whether the buffer was constructed successfully. * Return whether the buffer was constructed successfully.
* @return true is the buffer contains data * @return true is the buffer contains data
*/ */
bool IsValid() const bool IsValid() const
{ {
return this->buflen > 0; return this->buf.size() > 0;
} }
/** /**
@ -106,7 +96,7 @@ public:
*/ */
bool IsEnd() const bool IsEnd() const
{ {
return this->pos >= this->buflen; return this->pos >= this->buf.size();
} }
/** /**
@ -149,8 +139,8 @@ public:
bool ReadBuffer(byte *dest, size_t length) bool ReadBuffer(byte *dest, size_t length)
{ {
if (this->IsEnd()) return false; if (this->IsEnd()) return false;
if (this->buflen - this->pos < length) return false; if (this->buf.size() - this->pos < length) return false;
memcpy(dest, this->buf + this->pos, length); std::copy(std::begin(this->buf) + this->pos, std::begin(this->buf) + this->pos + length, dest);
this->pos += length; this->pos += length;
return true; return true;
} }
@ -164,8 +154,8 @@ public:
bool ReadDataBlock(MidiFile::DataBlock *dest, size_t length) bool ReadDataBlock(MidiFile::DataBlock *dest, size_t length)
{ {
if (this->IsEnd()) return false; if (this->IsEnd()) return false;
if (this->buflen - this->pos < length) return false; if (this->buf.size() - this->pos < length) return false;
dest->data.insert(dest->data.end(), this->buf + this->pos, this->buf + this->pos + length); dest->data.insert(dest->data.end(), std::begin(this->buf) + this->pos, std::begin(this->buf) + this->pos + length);
this->pos += length; this->pos += length;
return true; return true;
} }
@ -178,7 +168,7 @@ public:
bool Skip(size_t count) bool Skip(size_t count)
{ {
if (this->IsEnd()) return false; if (this->IsEnd()) return false;
if (this->buflen - this->pos < count) return false; if (this->buf.size() - this->pos < count) return false;
this->pos += count; this->pos += count;
return true; return true;
} }