song_save: return value pointer from matchesAnMpdTagItemKey()

The matchesAnMpdTagItemKey() API becomes more powerful and flexible if
the return value is the value pointer instead of a boolean.  It also
removes (invalid and dangerous) assumptions about the string from its
caller.
This commit is contained in:
Max Kellermann 2009-01-14 13:43:57 +01:00
parent 7cc9ba45a9
commit 3c6a85d8f7

View File

@ -86,7 +86,8 @@ insertSongIntoList(struct songvec *sv, struct song *newsong)
} }
} }
static int matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType) static char *
matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType)
{ {
int i; int i;
@ -96,11 +97,11 @@ static int matchesAnMpdTagItemKey(char *buffer, enum tag_type *itemType)
if (0 == strncmp(mpdTagItemKeys[i], buffer, len) && if (0 == strncmp(mpdTagItemKeys[i], buffer, len) &&
buffer[len] == ':' && buffer[len + 1] == ' ') { buffer[len] == ':' && buffer[len + 1] == ' ') {
*itemType = i; *itemType = i;
return 1; return buffer + len + 2;
} }
} }
return 0; return NULL;
} }
void readSongInfoIntoList(FILE *fp, struct songvec *sv, void readSongInfoIntoList(FILE *fp, struct songvec *sv,
@ -109,6 +110,7 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv,
char buffer[MPD_PATH_MAX + 1024]; char buffer[MPD_PATH_MAX + 1024];
struct song *song = NULL; struct song *song = NULL;
enum tag_type itemType; enum tag_type itemType;
const char *value;
while (fgets(buffer, sizeof(buffer), fp) && while (fgets(buffer, sizeof(buffer), fp) &&
!g_str_has_prefix(buffer, SONG_END)) { !g_str_has_prefix(buffer, SONG_END)) {
@ -126,16 +128,14 @@ void readSongInfoIntoList(FILE *fp, struct songvec *sv,
FATAL("Problems reading song info\n"); FATAL("Problems reading song info\n");
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
/* we don't need this info anymore */ /* we don't need this info anymore */
} else if (matchesAnMpdTagItemKey(buffer, &itemType)) { } else if ((value = matchesAnMpdTagItemKey(buffer,
&itemType)) != NULL) {
if (!song->tag) { if (!song->tag) {
song->tag = tag_new(); song->tag = tag_new();
tag_begin_add(song->tag); tag_begin_add(song->tag);
} }
tag_add_item(song->tag, itemType, tag_add_item(song->tag, itemType, value);
&(buffer
[strlen(mpdTagItemKeys[itemType]) +
2]));
} else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) { } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) {
if (!song->tag) { if (!song->tag) {
song->tag = tag_new(); song->tag = tag_new();