comparison src/midifile.c @ 30:26741527f3b7

Add midiFree() utility function.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Aug 2013 19:18:15 +0300
parents 4df6d9714314
children 416346c6dc74
comparison
equal deleted inserted replaced
29:4df6d9714314 30:26741527f3b7
91 #define IsChannelValid(_x) ((_x)>=1 && (_x)<=16) 91 #define IsChannelValid(_x) ((_x)>=1 && (_x)<=16)
92 #define IsNoteValid(_x) ((_x)>=0 && (_x)<128) 92 #define IsNoteValid(_x) ((_x)>=0 && (_x)<128)
93 #define IsMessageValid(_x) ((_x)>=msgNoteOff && (_x)<=msgMetaEvent) 93 #define IsMessageValid(_x) ((_x)>=msgNoteOff && (_x)<=msgMetaEvent)
94 94
95 95
96 static void midiFree(void *ptr)
97 {
98 if (ptr != NULL)
99 free(ptr);
100 }
101
96 static BOOL _midiValidateTrack(const _MIDI_FILE *pMF, int iTrack) 102 static BOOL _midiValidateTrack(const _MIDI_FILE *pMF, int iTrack)
97 { 103 {
98 if (!IsFilePtrValid(pMF)) 104 if (!IsFilePtrValid(pMF))
99 return FALSE; 105 return FALSE;
100 106
256 if (!bOverwriteIfExists) 262 if (!bOverwriteIfExists)
257 { 263 {
258 if ((pMF->pFile = fopen(pFilename, "r"))) 264 if ((pMF->pFile = fopen(pFilename, "r")))
259 { 265 {
260 fclose(pMF->pFile); 266 fclose(pMF->pFile);
261 free(pMF); 267 midiFree(pMF);
262 return NULL; 268 return NULL;
263 } 269 }
264 } 270 }
265 271
266 if ((pMF->pFile = fopen(pFilename, "wb+"))) 272 if ((pMF->pFile = fopen(pFilename, "wb+")))
267 { /*empty */ 273 { /*empty */
268 } 274 }
269 else 275 else
270 { 276 {
271 free((void *) pMF); 277 midiFree(pMF);
272 return NULL; 278 return NULL;
273 } 279 }
274 280
275 pMF->bOpenForWriting = TRUE; 281 pMF->bOpenForWriting = TRUE;
276 pMF->Header.PPQN = MIDI_PPQN_DEFAULT; 282 pMF->Header.PPQN = MIDI_PPQN_DEFAULT;
435 fclose(fp); 441 fclose(fp);
436 } 442 }
437 443
438 if (!bValidFile) 444 if (!bValidFile)
439 { 445 {
440 if (pMF) 446 midiFree(pMF);
441 free((void *) pMF);
442 return NULL; 447 return NULL;
443 } 448 }
444 449
445 return (MIDI_FILE *) pMF; 450 return (MIDI_FILE *) pMF;
446 } 451 }
530 ++i; 535 ++i;
531 bNoChanges = FALSE; 536 bNoChanges = FALSE;
532 } 537 }
533 } 538 }
534 539
535 free((void *) pEndPoints); 540 midiFree(pEndPoints);
541
536 /* 542 /*
537 ** Re-calc current position 543 ** Re-calc current position
538 */ 544 */
539 pMF->Track[iTrack].dt = dwEndTimePos - pMF->Track[iTrack].pos; 545 pMF->Track[iTrack].dt = dwEndTimePos - pMF->Track[iTrack].pos;
540 546
643 649
644 /* Write data */ 650 /* Write data */
645 fwrite(pMF->Track[i].pBase, sizeof(BYTE), dwData, pMF->pFile); 651 fwrite(pMF->Track[i].pBase, sizeof(BYTE), dwData, pMF->pFile);
646 652
647 /* Free memory */ 653 /* Free memory */
648 free((void *) pMF->Track[i].pBase); 654 midiFree(pMF->Track[i].pBase);
649 } 655 }
650 656
651 } 657 }
652 658
653 if (pMF->pFile) 659 if (pMF->pFile)
654 return fclose(pMF->pFile) ? FALSE : TRUE; 660 return fclose(pMF->pFile) ? FALSE : TRUE;
655 free((void *) pMF); 661
662 midiFree(pMF);
656 return TRUE; 663 return TRUE;
657 } 664 }
658 665
659 666
660 /* 667 /*
1273 pMsg->bImpliedMsg = FALSE; 1280 pMsg->bImpliedMsg = FALSE;
1274 } 1281 }
1275 1282
1276 void midiReadFreeMessage(MIDI_MSG * pMsg) 1283 void midiReadFreeMessage(MIDI_MSG * pMsg)
1277 { 1284 {
1278 if (pMsg->data) 1285 midiFree(pMsg->data);
1279 free((void *) pMsg->data);
1280 pMsg->data = NULL; 1286 pMsg->data = NULL;
1281 } 1287 }