decoder/gme: merge get_container_name() and get_song_num()

There is duplicate code in the two.
This commit is contained in:
Max Kellermann 2014-12-02 06:25:11 +01:00
parent b227fddec7
commit 27b4c62bc1

View File

@ -47,12 +47,17 @@ static constexpr unsigned GME_BUFFER_FRAMES = 2048;
static constexpr unsigned GME_BUFFER_SAMPLES = static constexpr unsigned GME_BUFFER_SAMPLES =
GME_BUFFER_FRAMES * GME_CHANNELS; GME_BUFFER_FRAMES * GME_CHANNELS;
struct GmeContainerPath {
char *path;
unsigned track;
};
/** /**
* returns the file path stripped of any /tune_xxx.* subtune * returns the file path stripped of any /tune_xxx.* subtune suffix
* suffix * and the track number (or 0 if no "tune_xxx" suffix is present).
*/ */
static char * static GmeContainerPath
get_container_name(Path path_fs) ParseContainerPath(Path path_fs)
{ {
const char *subtune_suffix = uri_get_suffix(path_fs.c_str()); const char *subtune_suffix = uri_get_suffix(path_fs.c_str());
char *path_container = xstrdup(path_fs.c_str()); char *path_container = xstrdup(path_fs.c_str());
@ -65,50 +70,22 @@ get_container_name(Path path_fs)
if (!g_pattern_match(path_with_subtune, if (!g_pattern_match(path_with_subtune,
strlen(path_container), path_container, nullptr)) { strlen(path_container), path_container, nullptr)) {
g_pattern_spec_free(path_with_subtune); g_pattern_spec_free(path_with_subtune);
return path_container; return { path_container, 0 };
} }
char *ptr = g_strrstr(path_container, "/" SUBTUNE_PREFIX); unsigned track = 0;
if (ptr != nullptr)
*ptr='\0';
g_pattern_spec_free(path_with_subtune);
return path_container;
}
/**
* returns tune number from file.nsf/tune_xxx.* style path or 0 if no subtune
* is appended.
*/
gcc_pure
static unsigned
get_song_num(Path path_fs)
{
const char *subtune_suffix = uri_get_suffix(path_fs.c_str());
char pat[64];
snprintf(pat, sizeof(pat), "%s%s",
"*/" SUBTUNE_PREFIX "???.",
subtune_suffix);
GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
if (g_pattern_match(path_with_subtune,
path_fs.length(), path_fs.data(), nullptr)) {
char *sub = g_strrstr(path_fs.c_str(), "/" SUBTUNE_PREFIX);
g_pattern_spec_free(path_with_subtune);
if (!sub)
return 0;
char *sub = g_strrstr(path_container, "/" SUBTUNE_PREFIX);
if (sub != nullptr) {
*sub = '\0';
sub += strlen("/" SUBTUNE_PREFIX); sub += strlen("/" SUBTUNE_PREFIX);
int song_num = strtol(sub, nullptr, 10); int song_num = strtol(sub, nullptr, 10);
if (song_num < 1) if (song_num >= 1)
return 0; track = song_num - 1;
return song_num - 1;
} else {
g_pattern_spec_free(path_with_subtune);
return 0;
} }
g_pattern_spec_free(path_with_subtune);
return { path_container, track };
} }
static char * static char *
@ -139,20 +116,19 @@ gme_container_scan(Path path_fs, const unsigned int tnum)
static void static void
gme_file_decode(Decoder &decoder, Path path_fs) gme_file_decode(Decoder &decoder, Path path_fs)
{ {
char *path_container = get_container_name(path_fs); const auto container = ParseContainerPath(path_fs);
Music_Emu *emu; Music_Emu *emu;
const char *gme_err = const char *gme_err =
gme_open_file(path_container, &emu, GME_SAMPLE_RATE); gme_open_file(container.path, &emu, GME_SAMPLE_RATE);
free(path_container); free(container.path);
if (gme_err != nullptr) { if (gme_err != nullptr) {
LogWarning(gme_domain, gme_err); LogWarning(gme_domain, gme_err);
return; return;
} }
gme_info_t *ti; gme_info_t *ti;
const unsigned song_num = get_song_num(path_fs); gme_err = gme_track_info(emu, &ti, container.track);
gme_err = gme_track_info(emu, &ti, song_num);
if (gme_err != nullptr) { if (gme_err != nullptr) {
LogWarning(gme_domain, gme_err); LogWarning(gme_domain, gme_err);
gme_delete(emu); gme_delete(emu);
@ -178,7 +154,7 @@ gme_file_decode(Decoder &decoder, Path path_fs)
decoder_initialized(decoder, audio_format, true, song_len); decoder_initialized(decoder, audio_format, true, song_len);
gme_err = gme_start_track(emu, song_num); gme_err = gme_start_track(emu, container.track);
if (gme_err != nullptr) if (gme_err != nullptr)
LogWarning(gme_domain, gme_err); LogWarning(gme_domain, gme_err);
@ -276,20 +252,18 @@ static bool
gme_scan_file(Path path_fs, gme_scan_file(Path path_fs,
const struct tag_handler *handler, void *handler_ctx) const struct tag_handler *handler, void *handler_ctx)
{ {
char *path_container = get_container_name(path_fs); const auto container = ParseContainerPath(path_fs);
Music_Emu *emu; Music_Emu *emu;
const char *gme_err = const char *gme_err =
gme_open_file(path_container, &emu, GME_SAMPLE_RATE); gme_open_file(container.path, &emu, GME_SAMPLE_RATE);
free(path_container); free(container.path);
if (gme_err != nullptr) { if (gme_err != nullptr) {
LogWarning(gme_domain, gme_err); LogWarning(gme_domain, gme_err);
return false; return false;
} }
const unsigned song_num = get_song_num(path_fs); const bool result = ScanMusicEmu(emu, container.track, handler, handler_ctx);
const bool result = ScanMusicEmu(emu, song_num, handler, handler_ctx);
gme_delete(emu); gme_delete(emu);