Support for fetching the content of a stored playlist (patch by qball), this adds listPlaylist and listPlaylistInfo

git-svn-id: https://svn.musicpd.org/mpd/trunk@3947 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Qball Cow 2006-03-26 13:46:05 +00:00
parent 19021e9cc6
commit 09929b6f82
3 changed files with 164 additions and 114 deletions

View File

@ -53,6 +53,8 @@
#define COMMAND_CLEAR "clear"
#define COMMAND_SAVE "save"
#define COMMAND_LOAD "load"
#define COMMAND_LISTPLAYLIST "listPlaylist"
#define COMMAND_LISTPLAYLISTINFO "listPlaylistInfo"
#define COMMAND_LSINFO "lsinfo"
#define COMMAND_RM "rm"
#define COMMAND_PLAYLISTINFO "playlistinfo"
@ -367,6 +369,16 @@ int handleLoad(FILE * fp, unsigned int * permission, int argArrayLength,
{
return loadPlaylist(fp,argArray[1]);
}
int handleListPlaylist(FILE * fp, unsigned int * permission, int argArrayLength,
char ** argArray)
{
return PlaylistInfo(fp,argArray[1],0);
}
int handleListPlaylistInfo(FILE * fp, unsigned int * permission, int argArrayLength,
char ** argArray)
{
return PlaylistInfo(fp,argArray[1], 1);
}
int handleLsInfo(FILE * fp, unsigned int * permission, int argArrayLength,
char ** argArray)
@ -928,6 +940,8 @@ void initCommands() {
addCommand(COMMAND_CLEAR ,PERMISSION_CONTROL, 0, 0,handleClear,NULL);
addCommand(COMMAND_SAVE ,PERMISSION_CONTROL, 1, 1,handleSave,NULL);
addCommand(COMMAND_LOAD ,PERMISSION_ADD, 1, 1,handleLoad,NULL);
addCommand(COMMAND_LISTPLAYLIST,PERMISSION_READ, 1, 1,handleListPlaylist,NULL);
addCommand(COMMAND_LISTPLAYLISTINFO,PERMISSION_READ, 1, 1,handleListPlaylistInfo,NULL);
addCommand(COMMAND_LSINFO ,PERMISSION_READ, 0, 1,handleLsInfo,NULL);
addCommand(COMMAND_RM ,PERMISSION_CONTROL, 1, 1,handleRm,NULL);
addCommand(COMMAND_PLAYLISTINFO,PERMISSION_READ, 0, 1,handlePlaylistInfo,NULL);

View File

@ -1337,120 +1337,6 @@ int savePlaylist(FILE * fp, char * utf8file) {
return 0;
}
int loadPlaylist(FILE * fp, char * utf8file) {
FILE * fileP;
char s[MAXPATHLEN+1];
int slength = 0;
char * temp = strdup(utf8ToFsCharset(utf8file));
char * rfile = malloc(strlen(temp)+strlen(".")+
strlen(PLAYLIST_FILE_SUFFIX)+1);
char * actualFile;
char * parent = parentPath(temp);
int parentlen = strlen(parent);
char * erroredFile = NULL;
int tempInt;
int commentCharFound = 0;
strcpy(rfile,temp);
strcat(rfile,".");
strcat(rfile,PLAYLIST_FILE_SUFFIX);
free(temp);
if((actualFile = rpp2app(rfile)) && isPlaylist(actualFile)) free(rfile);
else {
free(rfile);
commandError(fp, ACK_ERROR_NO_EXIST,
"playlist \"%s\" not found", utf8file);
return -1;
}
while(!(fileP = fopen(actualFile,"r")) && errno==EINTR);
if(fileP==NULL) {
commandError(fp, ACK_ERROR_SYSTEM,
"problems opening file \"%s\"", utf8file);
return -1;
}
while((tempInt = fgetc(fileP))!=EOF) {
s[slength] = tempInt;
if(s[slength]=='\n' || s[slength]=='\0') {
commentCharFound = 0;
s[slength] = '\0';
if(s[0]==PLAYLIST_COMMENT) {
commentCharFound = 1;
}
if(strncmp(s,musicDir,strlen(musicDir))==0) {
strcpy(s,&(s[strlen(musicDir)]));
}
else if(parentlen) {
temp = strdup(s);
memset(s,0,MAXPATHLEN+1);
strcpy(s,parent);
strncat(s,"/",MAXPATHLEN-parentlen);
strncat(s,temp,MAXPATHLEN-parentlen-1);
if(strlen(s)>=MAXPATHLEN) {
commandError(fp,
ACK_ERROR_PLAYLIST_LOAD,
"\"%s\" too long",
temp);
free(temp);
while(fclose(fileP) && errno==EINTR);
if(erroredFile) free(erroredFile);
return -1;
}
free(temp);
}
slength = 0;
temp = fsCharsetToUtf8(s);
if(!temp) continue;
temp = strdup(temp);
if(commentCharFound && !getSongFromDB(temp)
&& !isRemoteUrl(temp))
{
}
else if((addToPlaylist(stderr, temp, 0))<0) {
/* for windows compatibilit, convert slashes */
char * temp2 = strdup(temp);
char * p = temp2;
while(*p) {
if(*p=='\\') *p = '/';
p++;
}
if((addToPlaylist(stderr, temp2, 0))<0) {
if(!erroredFile) {
erroredFile = strdup(temp);
}
}
free(temp2);
}
free(temp);
}
else if(slength==MAXPATHLEN) {
s[slength] = '\0';
commandError(fp, ACK_ERROR_PLAYLIST_LOAD,
"line in \"%s\" is too long", utf8file);
ERROR("line \"%s\" in playlist \"%s\" is too long\n",
s, utf8file);
while(fclose(fileP) && errno==EINTR);
if(erroredFile) free(erroredFile);
return -1;
}
else if(s[slength]!='\r') slength++;
}
while(fclose(fileP) && errno==EINTR);
if(erroredFile) {
commandError(fp, ACK_ERROR_PLAYLIST_LOAD,
"can't add file \"%s\"", erroredFile);
free(erroredFile);
return -1;
}
return 0;
}
int getPlaylistCurrentSong() {
if(playlist.current >= 0 && playlist.current < playlist.length) {
return playlist.order[playlist.current];
@ -1507,3 +1393,150 @@ int seekSongInPlaylistById(FILE * fp, int id, float time) {
int getPlaylistSongId(int song) {
return playlist.positionToId[song];
}
static int PlaylistIterFunc(FILE * fp, char * utf8file, void (*IterFunc)(FILE *fp, char *utf8_file, char **errored_File)) {
FILE * fileP;
char s[MAXPATHLEN+1];
int slength = 0;
char * temp = strdup(utf8ToFsCharset(utf8file));
char * rfile = malloc(strlen(temp)+strlen(".")+
strlen(PLAYLIST_FILE_SUFFIX)+1);
char * actualFile;
char * parent = parentPath(temp);
int parentlen = strlen(parent);
char * erroredFile = NULL;
int tempInt;
int commentCharFound = 0;
strcpy(rfile,temp);
strcat(rfile,".");
strcat(rfile,PLAYLIST_FILE_SUFFIX);
free(temp);
if((actualFile = rpp2app(rfile)) && isPlaylist(actualFile)) free(rfile);
else {
free(rfile);
commandError(fp, ACK_ERROR_NO_EXIST,
"playlist \"%s\" not found", utf8file);
return -1;
}
while(!(fileP = fopen(actualFile,"r")) && errno==EINTR);
if(fileP==NULL) {
commandError(fp, ACK_ERROR_SYSTEM,
"problems opening file \"%s\"", utf8file);
return -1;
}
while((tempInt = fgetc(fileP))!=EOF) {
s[slength] = tempInt;
if(s[slength]=='\n' || s[slength]=='\0') {
commentCharFound = 0;
s[slength] = '\0';
if(s[0]==PLAYLIST_COMMENT) {
commentCharFound = 1;
}
if(strncmp(s,musicDir,strlen(musicDir))==0) {
strcpy(s,&(s[strlen(musicDir)]));
}
else if(parentlen) {
temp = strdup(s);
memset(s,0,MAXPATHLEN+1);
strcpy(s,parent);
strncat(s,"/",MAXPATHLEN-parentlen);
strncat(s,temp,MAXPATHLEN-parentlen-1);
if(strlen(s)>=MAXPATHLEN) {
commandError(fp,
ACK_ERROR_PLAYLIST_LOAD,
"\"%s\" too long",
temp);
free(temp);
while(fclose(fileP) && errno==EINTR);
if(erroredFile) free(erroredFile);
return -1;
}
free(temp);
}
slength = 0;
temp = fsCharsetToUtf8(s);
if(!temp) continue;
/* Needed to make a copy? */
if(!commentCharFound)
{
temp = strdup(temp);
IterFunc(fp, temp, &erroredFile);
free(temp);
}
}
else if(slength==MAXPATHLEN) {
s[slength] = '\0';
commandError(fp, ACK_ERROR_PLAYLIST_LOAD,
"line in \"%s\" is too long", utf8file);
ERROR("line \"%s\" in playlist \"%s\" is too long\n",
s, utf8file);
while(fclose(fileP) && errno==EINTR);
if(erroredFile) free(erroredFile);
return -1;
}
else if(s[slength]!='\r') slength++;
}
while(fclose(fileP) && errno==EINTR);
if(erroredFile) {
commandError(fp, ACK_ERROR_PLAYLIST_LOAD,
"can't add file \"%s\"", erroredFile);
free(erroredFile);
return -1;
}
return 0;
}
static void PlaylistInfoPrintInfo(FILE *fp, char *utf8file, char **erroredfile) {
Song * song = getSongFromDB(utf8file);
if(song) {
printSongInfo(fp, song);
}
else {
myfprintf(fp,"file:%s\n",utf8file);
}
}
static void PlaylistInfoPrint(FILE *fp, char *utf8file, char **erroredfile) {
myfprintf(fp,"file:%s\n",utf8file);
}
static void PlaylistLoadIterFunc(FILE *fp, char *temp, char **erroredFile) {
if(!getSongFromDB(temp) && !isRemoteUrl(temp))
{
}
else if((addToPlaylist(stderr, temp, 0))<0) {
/* for windows compatibilit, convert slashes */
char * temp2 = strdup(temp);
char * p = temp2;
while(*p) {
if(*p=='\\') *p = '/';
p++;
}
if((addToPlaylist(stderr, temp2, 0))<0) {
if(!*erroredFile) {
*erroredFile = strdup(temp);
}
}
free(temp2);
}
}
int PlaylistInfo(FILE * fp, char * utf8file, int detail) {
if(detail) {
return PlaylistIterFunc(fp, utf8file, PlaylistInfoPrintInfo);
}
return PlaylistIterFunc(fp, utf8file, PlaylistInfoPrint) ;
}
int loadPlaylist(FILE * fp, char * utf8file) {
return PlaylistIterFunc(fp, utf8file, PlaylistLoadIterFunc);
}

View File

@ -114,4 +114,7 @@ void playlistVersionChange();
int playlistChanges(FILE * fp, mpd_uint32 version);
int PlaylistInfo(FILE * fp, char * utf8file, int detail);
#endif