From 75aa8dad4ccf1bb942c327d78b0ddad821c4abaa Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 7 Sep 2008 13:35:01 +0200 Subject: song: moved code to song_print.c, song_save.c Move everything which dumps song information (via tag_print.c) to a separate source file. song_print.c gets code which writes song data to the client; song_save.c is responsible for serializing songs from the tag cache. --- src/Makefile.am | 4 ++ src/dbUtils.c | 1 + src/directory.c | 2 + src/playlist.c | 1 + src/song.c | 173 +------------------------------------------------------ src/song.h | 13 +---- src/song_print.c | 54 +++++++++++++++++ src/song_print.h | 30 ++++++++++ src/song_save.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/song_save.h | 29 ++++++++++ 10 files changed, 284 insertions(+), 183 deletions(-) create mode 100644 src/song_print.c create mode 100644 src/song_print.h create mode 100644 src/song_save.c create mode 100644 src/song_save.h diff --git a/src/Makefile.am b/src/Makefile.am index 2c3905414..ba7b7c3a3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -79,6 +79,8 @@ mpd_headers = \ sig_handlers.h \ sllist.h \ song.h \ + song_print.h \ + song_save.h \ state_file.h \ stats.h \ tag.h \ @@ -144,6 +146,8 @@ mpd_SOURCES = \ signal_check.c \ sllist.c \ song.c \ + song_print.c \ + song_save.c \ state_file.c \ stats.c \ tag.c \ diff --git a/src/dbUtils.c b/src/dbUtils.c index 26e727377..73078293c 100644 --- a/src/dbUtils.c +++ b/src/dbUtils.c @@ -23,6 +23,7 @@ #include "utils.h" #include "playlist.h" #include "song.h" +#include "song_print.h" #include "tag.h" #include "tagTracker.h" #include "log.h" diff --git a/src/directory.c b/src/directory.c index 55ae560cc..154547ce5 100644 --- a/src/directory.c +++ b/src/directory.c @@ -33,6 +33,8 @@ #include "ack.h" #include "myfprintf.h" #include "dbUtils.h" +#include "song_print.h" +#include "song_save.h" #define DIRECTORY_DIR "directory: " #define DIRECTORY_MTIME "mtime: " diff --git a/src/playlist.c b/src/playlist.c index f128af1b6..743a67d87 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -21,6 +21,7 @@ #include "command.h" #include "ls.h" #include "tag.h" +#include "song_print.h" #include "conf.h" #include "directory.h" #include "log.h" diff --git a/src/song.c b/src/song.c index c6ef97b53..a26603735 100644 --- a/src/song.c +++ b/src/song.c @@ -20,21 +20,14 @@ #include "ls.h" #include "directory.h" #include "utils.h" -#include "tag.h" #include "log.h" #include "path.h" #include "playlist.h" #include "decoder_list.h" #include "decoder_api.h" -#include "myfprintf.h" -#include "tag_print.h" - -#define SONG_KEY "key: " -#define SONG_MTIME "mtime: " - #include "os_compat.h" -static Song *newNullSong(void) +Song *newNullSong(void) { Song *song = xmalloc(sizeof(Song)); @@ -134,170 +127,6 @@ void freeSongList(SongList * list) freeList(list); } -void printSongUrl(int fd, Song * song) -{ - if (song->parentDir && song->parentDir->path) { - fdprintf(fd, "%s%s/%s\n", SONG_FILE, - getDirectoryPath(song->parentDir), song->url); - } else { - fdprintf(fd, "%s%s\n", SONG_FILE, song->url); - } -} - -int printSongInfo(int fd, Song * song) -{ - printSongUrl(fd, song); - - if (song->tag) - tag_print(fd, song->tag); - - return 0; -} - -int printSongInfoFromList(int fd, SongList * list) -{ - ListNode *tempNode = list->firstNode; - - while (tempNode != NULL) { - printSongInfo(fd, (Song *) tempNode->data); - tempNode = tempNode->nextNode; - } - - return 0; -} - -void writeSongInfoFromList(FILE * fp, SongList * list) -{ - ListNode *tempNode = list->firstNode; - - fprintf(fp, "%s\n", SONG_BEGIN); - - while (tempNode != NULL) { - fprintf(fp, "%s%s\n", SONG_KEY, tempNode->key); - fflush(fp); - printSongInfo(fileno(fp), (Song *) tempNode->data); - fprintf(fp, "%s%li\n", SONG_MTIME, - (long)((Song *) tempNode->data)->mtime); - tempNode = tempNode->nextNode; - } - - fprintf(fp, "%s\n", SONG_END); -} - -static void insertSongIntoList(SongList * list, ListNode ** nextSongNode, - char *key, Song * song) -{ - ListNode *nodeTemp; - int cmpRet = 0; - - while (*nextSongNode - && (cmpRet = strcmp(key, (*nextSongNode)->key)) > 0) { - nodeTemp = (*nextSongNode)->nextNode; - deleteNodeFromList(list, *nextSongNode); - *nextSongNode = nodeTemp; - } - - if (!(*nextSongNode)) { - insertInList(list, song->url, (void *)song); - } else if (cmpRet == 0) { - Song *tempSong = (Song *) ((*nextSongNode)->data); - if (tempSong->mtime != song->mtime) { - tag_free(tempSong->tag); - tag_end_add(song->tag); - tempSong->tag = song->tag; - tempSong->mtime = song->mtime; - song->tag = NULL; - } - freeJustSong(song); - *nextSongNode = (*nextSongNode)->nextNode; - } else { - insertInListBeforeNode(list, *nextSongNode, -1, song->url, - (void *)song); - } -} - -static int matchesAnMpdTagItemKey(char *buffer, int *itemType) -{ - int i; - - for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) { - if (0 == strncmp(mpdTagItemKeys[i], buffer, - strlen(mpdTagItemKeys[i]))) { - *itemType = i; - return 1; - } - } - - return 0; -} - -void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) -{ - char buffer[MPD_PATH_MAX + 1024]; - int bufferSize = MPD_PATH_MAX + 1024; - Song *song = NULL; - ListNode *nextSongNode = list->firstNode; - ListNode *nodeTemp; - int itemType; - - while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) { - if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) { - if (song) { - insertSongIntoList(list, &nextSongNode, - song->url, song); - if (song->tag != NULL) - tag_end_add(song->tag); - } - - song = newNullSong(); - song->url = xstrdup(buffer + strlen(SONG_KEY)); - song->type = SONG_TYPE_FILE; - song->parentDir = parentDir; - } else if (*buffer == 0) { - /* ignore empty lines (starting with '\0') */ - } else if (song == NULL) { - FATAL("Problems reading song info\n"); - } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { - /* we don't need this info anymore - song->url = xstrdup(&(buffer[strlen(SONG_FILE)])); - */ - } else if (matchesAnMpdTagItemKey(buffer, &itemType)) { - if (!song->tag) { - song->tag = tag_new(); - tag_begin_add(song->tag); - } - - tag_add_item(song->tag, itemType, - &(buffer - [strlen(mpdTagItemKeys[itemType]) + - 2])); - } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) { - if (!song->tag) { - song->tag = tag_new(); - tag_begin_add(song->tag); - } - - song->tag->time = atoi(&(buffer[strlen(SONG_TIME)])); - } else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) { - song->mtime = atoi(&(buffer[strlen(SONG_MTIME)])); - } - else - FATAL("songinfo: unknown line in db: %s\n", buffer); - } - - if (song) { - insertSongIntoList(list, &nextSongNode, song->url, song); - if (song->tag != NULL) - tag_end_add(song->tag); - } - - while (nextSongNode) { - nodeTemp = nextSongNode->nextNode; - deleteNodeFromList(list, nextSongNode); - nextSongNode = nodeTemp; - } -} - int updateSongInfo(Song * song) { if (song->type == SONG_TYPE_FILE) { diff --git a/src/song.h b/src/song.h index 0f271aa83..0ea3ec72b 100644 --- a/src/song.h +++ b/src/song.h @@ -42,6 +42,8 @@ typedef struct _Song { typedef List SongList; +Song *newNullSong(void); + Song *newSong(const char *url, int songType, struct _Directory *parentDir); void freeSong(Song *); @@ -55,19 +57,8 @@ void freeSongList(SongList * list); Song *addSongToList(SongList * list, const char *url, const char *utf8path, int songType, struct _Directory *parentDir); -int printSongInfo(int fd, Song * song); - -int printSongInfoFromList(int fd, SongList * list); - -void writeSongInfoFromList(FILE * fp, SongList * list); - -void readSongInfoIntoList(FILE * fp, SongList * list, - struct _Directory *parent); - int updateSongInfo(Song * song); -void printSongUrl(int fd, Song * song); - /* * get_song_url - Returns a path of a song in UTF8-encoded form * path_max_tmp is the argument that the URL is written to, this diff --git a/src/song_print.c b/src/song_print.c new file mode 100644 index 000000000..d7bca662a --- /dev/null +++ b/src/song_print.c @@ -0,0 +1,54 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com) + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "song_print.h" +#include "directory.h" +#include "tag_print.h" +#include "myfprintf.h" + +void printSongUrl(int fd, Song * song) +{ + if (song->parentDir && song->parentDir->path) { + fdprintf(fd, "%s%s/%s\n", SONG_FILE, + getDirectoryPath(song->parentDir), song->url); + } else { + fdprintf(fd, "%s%s\n", SONG_FILE, song->url); + } +} + +int printSongInfo(int fd, Song * song) +{ + printSongUrl(fd, song); + + if (song->tag) + tag_print(fd, song->tag); + + return 0; +} + +int printSongInfoFromList(int fd, SongList * list) +{ + ListNode *tempNode = list->firstNode; + + while (tempNode != NULL) { + printSongInfo(fd, (Song *) tempNode->data); + tempNode = tempNode->nextNode; + } + + return 0; +} diff --git a/src/song_print.h b/src/song_print.h new file mode 100644 index 000000000..8a97759bf --- /dev/null +++ b/src/song_print.h @@ -0,0 +1,30 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com) + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SONG_PRINT_H +#define SONG_PRINT_H + +#include "song.h" + +int printSongInfo(int fd, Song * song); + +int printSongInfoFromList(int fd, SongList * list); + +void printSongUrl(int fd, Song * song); + +#endif diff --git a/src/song_save.c b/src/song_save.c new file mode 100644 index 000000000..a7b398041 --- /dev/null +++ b/src/song_save.c @@ -0,0 +1,160 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com) + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "song_save.h" +#include "song_print.h" +#include "directory.h" +#include "path.h" +#include "utils.h" +#include "tag.h" +#include "log.h" + +#define SONG_KEY "key: " +#define SONG_MTIME "mtime: " + +void writeSongInfoFromList(FILE * fp, SongList * list) +{ + ListNode *tempNode = list->firstNode; + + fprintf(fp, "%s\n", SONG_BEGIN); + + while (tempNode != NULL) { + fprintf(fp, "%s%s\n", SONG_KEY, tempNode->key); + fflush(fp); + printSongInfo(fileno(fp), (Song *) tempNode->data); + fprintf(fp, "%s%li\n", SONG_MTIME, + (long)((Song *) tempNode->data)->mtime); + tempNode = tempNode->nextNode; + } + + fprintf(fp, "%s\n", SONG_END); +} + +static void insertSongIntoList(SongList * list, ListNode ** nextSongNode, + char *key, Song * song) +{ + ListNode *nodeTemp; + int cmpRet = 0; + + while (*nextSongNode + && (cmpRet = strcmp(key, (*nextSongNode)->key)) > 0) { + nodeTemp = (*nextSongNode)->nextNode; + deleteNodeFromList(list, *nextSongNode); + *nextSongNode = nodeTemp; + } + + if (!(*nextSongNode)) { + insertInList(list, song->url, (void *)song); + } else if (cmpRet == 0) { + Song *tempSong = (Song *) ((*nextSongNode)->data); + if (tempSong->mtime != song->mtime) { + tag_free(tempSong->tag); + tag_end_add(song->tag); + tempSong->tag = song->tag; + tempSong->mtime = song->mtime; + song->tag = NULL; + } + freeJustSong(song); + *nextSongNode = (*nextSongNode)->nextNode; + } else { + insertInListBeforeNode(list, *nextSongNode, -1, song->url, + (void *)song); + } +} + +static int matchesAnMpdTagItemKey(char *buffer, int *itemType) +{ + int i; + + for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) { + if (0 == strncmp(mpdTagItemKeys[i], buffer, + strlen(mpdTagItemKeys[i]))) { + *itemType = i; + return 1; + } + } + + return 0; +} + +void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) +{ + char buffer[MPD_PATH_MAX + 1024]; + int bufferSize = MPD_PATH_MAX + 1024; + Song *song = NULL; + ListNode *nextSongNode = list->firstNode; + ListNode *nodeTemp; + int itemType; + + while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) { + if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) { + if (song) { + insertSongIntoList(list, &nextSongNode, + song->url, song); + if (song->tag != NULL) + tag_end_add(song->tag); + } + + song = newNullSong(); + song->url = xstrdup(buffer + strlen(SONG_KEY)); + song->type = SONG_TYPE_FILE; + song->parentDir = parentDir; + } else if (*buffer == 0) { + /* ignore empty lines (starting with '\0') */ + } else if (song == NULL) { + FATAL("Problems reading song info\n"); + } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { + /* we don't need this info anymore + song->url = xstrdup(&(buffer[strlen(SONG_FILE)])); + */ + } else if (matchesAnMpdTagItemKey(buffer, &itemType)) { + if (!song->tag) { + song->tag = tag_new(); + tag_begin_add(song->tag); + } + + tag_add_item(song->tag, itemType, + &(buffer + [strlen(mpdTagItemKeys[itemType]) + + 2])); + } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) { + if (!song->tag) { + song->tag = tag_new(); + tag_begin_add(song->tag); + } + + song->tag->time = atoi(&(buffer[strlen(SONG_TIME)])); + } else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) { + song->mtime = atoi(&(buffer[strlen(SONG_MTIME)])); + } + else + FATAL("songinfo: unknown line in db: %s\n", buffer); + } + + if (song) { + insertSongIntoList(list, &nextSongNode, song->url, song); + if (song->tag != NULL) + tag_end_add(song->tag); + } + + while (nextSongNode) { + nodeTemp = nextSongNode->nextNode; + deleteNodeFromList(list, nextSongNode); + nextSongNode = nodeTemp; + } +} diff --git a/src/song_save.h b/src/song_save.h new file mode 100644 index 000000000..84acae39e --- /dev/null +++ b/src/song_save.h @@ -0,0 +1,29 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com) + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SONG_SAVE_H +#define SONG_SAVE_H + +#include "song.h" + +void writeSongInfoFromList(FILE * fp, SongList * list); + +void readSongInfoIntoList(FILE * fp, SongList * list, + struct _Directory *parent); + +#endif -- cgit v1.2.3