char * slurp_gamedat(const char *name) { FILE *f; ssize_t len; char *p = NULL; size_t buflen = 0; f = open_gamedat(name); if (!f) return NULL; len = fread_dyn(&p, &buflen, f); if (len < 0) { CRITICAL2("could not read file `%s'", name); exit(EXIT_FAILURE); } fclose(f); return p; }
// Ensure that the specified music track is in memory, loading it if required void music_load(enum music_track track) { char name[80] = ""; FILE * midi_f; char * midi_data = NULL; size_t fileLength; int i; OSStatus rv; // Don't (try to) load the track twice if (music_files[track].sequence_loaded || music_files[track].unplayable) return; // Find the name for this track for (i = 0; music_key[i].track != M_MAX_MUSIC; i++) { if (music_key[i].track == track && music_key[i].name) { snprintf(name, sizeof(name), "%s.MID", music_key[track].name); break; } } // Bail out if this track isn't known if (strlen(name) == 0) { music_files[track].unplayable = 1; return; } // Read the MIDI file into memory midi_f = sOpen(name, "rb", FT_MIDI); if (midi_f != NULL) { fileLength = fread_dyn(&midi_data, &fileLength, midi_f); fclose(midi_f); } if (midi_data == NULL) { WARNING2("could not read MIDI file `%s'", name); music_files[track].unplayable = 1; return; } // Initialize the sequence rv = NewMusicSequence(&music_files[track].sequence); if (rv == 0) { // Try to load the sequence out of this buffer #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 /* this is deprecated, but works back to 10.3 */ rv = MusicSequenceLoadSMFDataWithFlags(music_files[track].sequence, CFDataCreate(NULL, (UInt8*)midi_data, fileLength), 0); #else /* not deprecated, but requires 10.5 or later */ rv = MusicSequenceFileLoadData(music_files[track].sequence, CFDataCreate(NULL, (UInt8*)midi_data, fileLength), 0, 0); #endif } // Regardless, free the buffer we read off disk, if any free(midi_data); // Did it work? if (rv) { WARNING2("could not understand MIDI file `%s'", name); music_files[track].unplayable = 1; } else { music_files[track].sequence_loaded = 1; } }