diff --git a/mpdscribble/mpdscribble.c b/mpdscribble/mpdscribble.c index 6d5481a..9116a44 100644 --- a/mpdscribble/mpdscribble.c +++ b/mpdscribble/mpdscribble.c @@ -21,9 +21,12 @@ #include +#include #include #include #include +#include +#include #include "file.h" #include "misc.h" @@ -57,6 +60,9 @@ sigpipe_handler (int signum) warning ("broken pipe, disconnected from mpd"); } +static void +extract_track_from_filename(const char *filename, char **artist, char **title); + int main (int argc, char** argv) { @@ -107,6 +113,12 @@ main (int argc, char** argv) fflush (log); as_sleep (); elapsed = lmc_current (&song); + + if (song.artist == NULL && song.title == NULL) + { + extract_track_from_filename(song.file, &song.artist, &song.title); + } + max_skip_error = MAX_SKIP_ERROR + lmc_xfade_hack (); if (now () > next_save) @@ -204,3 +216,104 @@ main (int argc, char** argv) return 0; } + +#define SEPARATOR " - " + +static char * +extract_artist(const char *filename) +{ + assert(filename != NULL); + + char *end = NULL; + size_t size = 0; + + end = strstr(filename, SEPARATOR); + if (end == NULL) + return NULL; + + if (end == filename) + return NULL; + + /* end points to begin of separator, so move it to next symbol */ + end--; + + /* skip trailing spaces */ + while (*end == ' ') + --end; + + /* skip leading spaces */ + filename += strspn(filename, " "); + + size = end - filename + 1; + if (size == 0) + return NULL; + + return strndup2(filename, size); +} + +static char * +extract_title(const char *filename) +{ + assert(filename != NULL); + + char *p = NULL; + + p = strstr(filename, SEPARATOR); + if (p == NULL) + return NULL; + + /* skip separator */ + p += (sizeof(SEPARATOR) - 1); + + /* skip leading spaces */ + p += strspn(p, " "); + + if (*p == '\0') + return NULL; + + char *point = strrchr(p, '.'); + if (point != NULL && + (strcasecmp(point, ".mp3") == 0 || + strcasecmp(point, ".ogg") == 0 || + strcasecmp(point, ".wma") == 0)) + { + size_t size = point - p; + if (size == 0) + return NULL; + + return strndup2(p, size); + } + + return strdup2(p); +} + +static void +extract_track_from_filename(const char *filename, char **artist, char **title) +{ + assert(filename != NULL); + + /* skip path to file */ + char *p = strrchr(filename, '/'); + if (p == NULL) + p = (char *)filename; + else + p++; /* p points to slash, so move it to next symbol */ + + char *tmp_artist = extract_artist(p); + if (tmp_artist == NULL) + return; + + char *tmp_title = extract_title(p); + if (tmp_title == NULL) + { + free(tmp_artist); + return; + } + + *artist = tmp_artist; + *title = tmp_title; + + if (file_config.verbose > 1) + notice("extracted info from file: '%s' - '%s'", *artist, *title); +} +