From fc2510e2ba6a7114d9fd035aba44ee146e343b00 Mon Sep 17 00:00:00 2001 From: PeterN Date: Fri, 16 Jun 2023 17:04:46 +0100 Subject: [PATCH] Codechange: Use std::vector for midifile's ByteBuffer. (#11019) --- src/music/midifile.cpp | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 060f5393e4..6f849247c0 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -60,8 +60,7 @@ const byte *MidiGetStandardSysexMessage(MidiSysexMessage msg, size_t &length) * RAII-compliant to make teardown in error situations easier. */ class ByteBuffer { - byte *buf; - size_t buflen; + std::vector buf; size_t pos; public: /** @@ -73,31 +72,22 @@ public: */ ByteBuffer(FILE *file, size_t len) { - this->buf = MallocT(len); - if (fread(this->buf, 1, len, file) == len) { - this->buflen = len; + this->buf.resize(len); + if (fread(this->buf.data(), 1, len, file) == len) { this->pos = 0; } else { /* invalid state */ - this->buflen = 0; + this->buf.clear(); } } - /** - * Destructor, frees the buffer. - */ - ~ByteBuffer() - { - free(this->buf); - } - /** * Return whether the buffer was constructed successfully. * @return true is the buffer contains data */ bool IsValid() const { - return this->buflen > 0; + return this->buf.size() > 0; } /** @@ -106,7 +96,7 @@ public: */ 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) { if (this->IsEnd()) return false; - if (this->buflen - this->pos < length) return false; - memcpy(dest, this->buf + this->pos, length); + if (this->buf.size() - this->pos < length) return false; + std::copy(std::begin(this->buf) + this->pos, std::begin(this->buf) + this->pos + length, dest); this->pos += length; return true; } @@ -164,8 +154,8 @@ public: bool ReadDataBlock(MidiFile::DataBlock *dest, size_t length) { if (this->IsEnd()) return false; - if (this->buflen - this->pos < length) return false; - dest->data.insert(dest->data.end(), this->buf + this->pos, this->buf + this->pos + length); + if (this->buf.size() - this->pos < length) return false; + dest->data.insert(dest->data.end(), std::begin(this->buf) + this->pos, std::begin(this->buf) + this->pos + length); this->pos += length; return true; } @@ -178,7 +168,7 @@ public: bool Skip(size_t count) { 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; return true; }