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.
This commit is contained in:
Max Kellermann 2008-09-07 13:35:01 +02:00
parent 386c303121
commit 75aa8dad4c
10 changed files with 284 additions and 183 deletions

View File

@ -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 \

View File

@ -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"

View File

@ -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: "

View File

@ -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"

View File

@ -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) {

View File

@ -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

54
src/song_print.c Normal file
View File

@ -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;
}

30
src/song_print.h Normal file
View File

@ -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

160
src/song_save.c Normal file
View File

@ -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;
}
}

29
src/song_save.h Normal file
View File

@ -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