Use unique_ptr for hmp_track data

This commit is contained in:
Kp 2014-08-05 02:31:03 +00:00
parent 81cb86f2e3
commit efb8c495f6
2 changed files with 5 additions and 17 deletions

View file

@ -55,7 +55,7 @@ struct event
struct hmp_track
{
unsigned char *data;
std::unique_ptr<uint8_t[]> data;
unsigned char *loop;
unsigned int len;
unsigned char *cur;

View file

@ -38,21 +38,15 @@ void hmp_stop(hmp_file *hmp);
hmp_file::~hmp_file()
{
int i;
#ifdef _WIN32
hmp_stop(this);
#endif
for (i = 0; i < num_trks; i++)
if (trks[i].data)
d_free(trks[i].data);
}
std::unique_ptr<hmp_file> hmp_open(const char *filename) {
int i, data, num_tracks, tempo;
char buf[256];
PHYSFS_file *fp;
unsigned char *p;
if (!(fp = PHYSFSX_openReadBuffered(filename)))
return NULL;
@ -111,15 +105,9 @@ std::unique_ptr<hmp_file> hmp_open(const char *filename) {
data -= 12;
hmp->trks[i].len = data;
MALLOC(p, unsigned char, data);
if (!(hmp->trks[i].data = p))
{
PHYSFS_close(fp);
return NULL;
}
hmp->trks[i].data.reset(new uint8_t[data]);
/* finally, read track data */
if ((PHYSFSX_fseek(fp, 4, SEEK_CUR)) || (PHYSFS_read(fp, p, data, 1) != 1))
if ((PHYSFSX_fseek(fp, 4, SEEK_CUR)) || (PHYSFS_read(fp, hmp->trks[i].data.get(), data, 1) != 1))
{
PHYSFS_close(fp);
return NULL;
@ -366,7 +354,7 @@ static void reset_tracks(struct hmp_file *hmp)
if (hmp->trks[i].loop_set)
hmp->trks[i].cur = hmp->trks[i].loop;
else
hmp->trks[i].cur = hmp->trks[i].data;
hmp->trks[i].cur = hmp->trks[i].data.get();
hmp->trks[i].left = hmp->trks[i].len;
hmp->trks[i].cur_time = 0;
}
@ -755,7 +743,7 @@ void hmp2mid(const char *hmp_name, unsigned char **midbuf, unsigned int *midlen)
mi = 0;
*midbuf = (unsigned char *) d_realloc(*midbuf, *midlen + sizeof(mi));
*midlen += sizeof(mi);
mi = hmptrk2mid(hmp->trks[i].data, hmp->trks[i].len, midbuf, midlen);
mi = hmptrk2mid(hmp->trks[i].data.get(), hmp->trks[i].len, midbuf, midlen);
mi = MIDIINT(mi);
memcpy(&(*midbuf)[midtrklenpos], &mi, 4);
}