1
0
Fork 0

Codechange: replace grow() usage for MidiFile

pull/7578/head
glx 2019-05-03 18:11:14 +02:00 committed by Charles Pigott
parent 212140b88b
commit 9184a62958
1 changed files with 26 additions and 16 deletions

View File

@ -113,7 +113,7 @@ public:
/** /**
* Read bytes into a buffer. * Read bytes into a buffer.
* @param[out] dest buffer to copy info * @param[out] dest buffer to copy into
* @param length number of bytes to read * @param length number of bytes to read
* @return true if the requested number of bytes were available * @return true if the requested number of bytes were available
*/ */
@ -126,6 +126,21 @@ public:
return true; return true;
} }
/**
* Read bytes into a MidiFile::DataBlock.
* @param[out] dest DataBlock to copy into
* @param length number of bytes to read
* @return true if the requested number of bytes were available
*/
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);
this->pos += length;
return true;
}
/** /**
* Skip over a number of bytes in the buffer. * Skip over a number of bytes in the buffer.
* @param count number of bytes to skip over * @param count number of bytes to skip over
@ -208,7 +223,6 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
/* Regular channel message */ /* Regular channel message */
last_status = status; last_status = status;
running_status: running_status:
byte *data;
switch (status & 0xF0) { switch (status & 0xF0) {
case MIDIST_NOTEOFF: case MIDIST_NOTEOFF:
case MIDIST_NOTEON: case MIDIST_NOTEON:
@ -216,20 +230,19 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
case MIDIST_CONTROLLER: case MIDIST_CONTROLLER:
case MIDIST_PITCHBEND: case MIDIST_PITCHBEND:
/* 3 byte messages */ /* 3 byte messages */
data = grow(block->data, 3); block->data.push_back(status);
data[0] = status; if (!chunk.ReadDataBlock(block, 2)) {
if (!chunk.ReadBuffer(&data[1], 2)) {
return false; return false;
} }
break; break;
case MIDIST_PROGCHG: case MIDIST_PROGCHG:
case MIDIST_CHANPRESS: case MIDIST_CHANPRESS:
/* 2 byte messages */ /* 2 byte messages */
data = grow(block->data, 2); block->data.push_back(status);
data[0] = status; if (!chunk.ReadByte(buf[0])) {
if (!chunk.ReadByte(data[1])) {
return false; return false;
} }
block->data.push_back(buf[0]);
break; break;
default: default:
NOT_REACHED(); NOT_REACHED();
@ -266,12 +279,11 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
if (!chunk.ReadVariableLength(length)) { if (!chunk.ReadVariableLength(length)) {
return false; return false;
} }
byte *data = grow(block->data, length + 1); block->data.push_back(0xF0);
data[0] = 0xF0; if (!chunk.ReadDataBlock(block, length)) {
if (!chunk.ReadBuffer(data + 1, length)) {
return false; return false;
} }
if (data[length] != 0xF7) { if (block->data.back() != 0xF7) {
/* Engage Casio weirdo mode - convert to normal sysex */ /* Engage Casio weirdo mode - convert to normal sysex */
running_sysex = true; running_sysex = true;
block->data.push_back(0xF7); block->data.push_back(0xF7);
@ -284,8 +296,7 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
if (!chunk.ReadVariableLength(length)) { if (!chunk.ReadVariableLength(length)) {
return false; return false;
} }
byte *data = grow(block->data, length); if (!chunk.ReadDataBlock(block, length)) {
if (!chunk.ReadBuffer(data, length)) {
return false; return false;
} }
} else { } else {
@ -335,8 +346,7 @@ static bool FixupMidiData(MidiFile &target)
merged_blocks.push_back(block); merged_blocks.push_back(block);
last_ticktime = block.ticktime; last_ticktime = block.ticktime;
} else { } else {
byte *datadest = grow(merged_blocks.back().data, block.data.size()); merged_blocks.back().data.insert(merged_blocks.back().data.end(), block.data.begin(), block.data.end());
memcpy(datadest, block.data.data(), block.data.size());
} }
} }
std::swap(merged_blocks, target.blocks); std::swap(merged_blocks, target.blocks);