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

This commit is contained in:
Max Kellermann 2014-12-04 21:11:33 +01:00
parent 3400398230
commit 110589c0f3

View File

@ -22,6 +22,7 @@
#include "../DecoderAPI.hxx" #include "../DecoderAPI.hxx"
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/FormatString.hxx" #include "util/FormatString.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "system/ByteOrder.hxx" #include "system/ByteOrder.hxx"
@ -40,7 +41,6 @@
static constexpr Domain sidplay_domain("sidplay"); static constexpr Domain sidplay_domain("sidplay");
static GPatternSpec *path_with_subtune;
static const char *songlength_file; static const char *songlength_file;
static GKeyFile *songlength_database; static GKeyFile *songlength_database;
@ -99,9 +99,6 @@ sidplay_init(const config_param &param)
all_files_are_containers = all_files_are_containers =
param.GetBlockValue("all_files_are_containers", true); param.GetBlockValue("all_files_are_containers", true);
path_with_subtune=g_pattern_spec_new(
"*/" SUBTUNE_PREFIX "???.sid");
filter_setting = param.GetBlockValue("filter", true); filter_setting = param.GetBlockValue("filter", true);
return true; return true;
@ -110,52 +107,46 @@ sidplay_init(const config_param &param)
static void static void
sidplay_finish() sidplay_finish()
{ {
g_pattern_spec_free(path_with_subtune);
if(songlength_database) if(songlength_database)
g_key_file_free(songlength_database); g_key_file_free(songlength_database);
} }
/** struct SidplayContainerPath {
* returns the file path stripped of any /tune_xxx.sid subtune AllocatedPath path;
* suffix unsigned track;
*/ };
static char *
get_container_name(Path path_fs) gcc_pure
static unsigned
ParseSubtuneName(const char *base)
{ {
char *path_container = strdup(path_fs.c_str()); if (memcmp(base, SUBTUNE_PREFIX, sizeof(SUBTUNE_PREFIX) - 1) != 0)
return 0;
if(!g_pattern_match(path_with_subtune, base += sizeof(SUBTUNE_PREFIX) - 1;
strlen(path_container), path_container, nullptr))
return path_container;
char *ptr=g_strrstr(path_container, "/" SUBTUNE_PREFIX); char *endptr;
if(ptr) *ptr='\0'; auto track = strtoul(base, &endptr, 10);
if (endptr == base || *endptr != '.')
return 0;
return path_container; return track;
} }
/** /**
* returns tune number from file.sid/tune_xxx.sid style path or 1 if * returns the file path stripped of any /tune_xxx.* subtune suffix
* no subtune is appended * and the track number (or 1 if no "tune_xxx" suffix is present).
*/ */
static unsigned static SidplayContainerPath
get_song_num(const char *path_fs) ParseContainerPath(Path path_fs)
{ {
if(g_pattern_match(path_with_subtune, const Path base = path_fs.GetBase();
strlen(path_fs), path_fs, nullptr)) { unsigned track;
char *sub=g_strrstr(path_fs, "/" SUBTUNE_PREFIX); if (base.IsNull() ||
if(!sub) return 1; (track = ParseSubtuneName(base.c_str())) < 1)
return { AllocatedPath(path_fs), 1 };
sub+=strlen("/" SUBTUNE_PREFIX); return { path_fs.GetDirectoryName(), track };
int song_num=strtol(sub, nullptr, 10);
if (errno == EINVAL)
return 1;
else
return song_num;
} else
return 1;
} }
/* get the song length in seconds */ /* get the song length in seconds */
@ -165,10 +156,9 @@ get_song_length(Path path_fs)
if (songlength_database == nullptr) if (songlength_database == nullptr)
return SignedSongTime::Negative(); return SignedSongTime::Negative();
char *sid_file = get_container_name(path_fs); const auto container = ParseContainerPath(path_fs);
SidTuneMod tune(sid_file); SidTuneMod tune(container.path.c_str());
free(sid_file); if(!tune) {
if(!tune) {
LogWarning(sidplay_domain, LogWarning(sidplay_domain,
"failed to load file for calculating md5 sum"); "failed to load file for calculating md5 sum");
return SignedSongTime::Negative(); return SignedSongTime::Negative();
@ -176,7 +166,7 @@ get_song_length(Path path_fs)
char md5sum[SIDTUNE_MD5_LENGTH+1]; char md5sum[SIDTUNE_MD5_LENGTH+1];
tune.createMD5(md5sum); tune.createMD5(md5sum);
const unsigned song_num = get_song_num(path_fs.c_str()); const unsigned song_num = container.track;
gsize num_items; gsize num_items;
gchar **values=g_key_file_get_string_list(songlength_database, gchar **values=g_key_file_get_string_list(songlength_database,
@ -209,15 +199,14 @@ sidplay_file_decode(Decoder &decoder, Path path_fs)
/* load the tune */ /* load the tune */
char *path_container=get_container_name(path_fs); const auto container = ParseContainerPath(path_fs);
SidTune tune(path_container, nullptr, true); SidTune tune(container.path.c_str(), nullptr, true);
free(path_container);
if (!tune) { if (!tune) {
LogWarning(sidplay_domain, "failed to load file"); LogWarning(sidplay_domain, "failed to load file");
return; return;
} }
const int song_num = get_song_num(path_fs.c_str()); const int song_num = container.track;
tune.selectSong(song_num); tune.selectSong(song_num);
auto duration = get_song_length(path_fs); auto duration = get_song_length(path_fs);
@ -347,11 +336,10 @@ static bool
sidplay_scan_file(Path path_fs, sidplay_scan_file(Path path_fs,
const struct tag_handler *handler, void *handler_ctx) const struct tag_handler *handler, void *handler_ctx)
{ {
const int song_num = get_song_num(path_fs.c_str()); const auto container = ParseContainerPath(path_fs);
char *path_container=get_container_name(path_fs); const unsigned song_num = container.track;
SidTune tune(path_container, nullptr, true); SidTune tune(container.path.c_str(), nullptr, true);
free(path_container);
if (!tune) if (!tune)
return false; return false;