add pl_load and pl_eval for smart-playlist loading and evaluating
This commit is contained in:
parent
e6673b0f5d
commit
c8e62c0357
155
src/playlist.c
155
src/playlist.c
|
@ -5,15 +5,22 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "err.h"
|
||||
#include "mp3-scanner.h"
|
||||
#include "playlist.h"
|
||||
#include "parser.h"
|
||||
|
||||
/* Globals */
|
||||
SMART_PLAYLIST pl_smart = { NULL, NULL, NULL };
|
||||
int pl_error=0;
|
||||
|
||||
/* Forwards */
|
||||
void pl_dump(void);
|
||||
void pl_dump_node(PL_NODE *pnode, int indent);
|
||||
int pl_load(char *file);
|
||||
int pl_eval_node(MP3FILE *pmp3, PL_NODE *pnode);
|
||||
|
||||
extern FILE *yyin;
|
||||
|
||||
/*
|
||||
* pl_dump
|
||||
|
@ -44,26 +51,26 @@ void pl_dump_node(PL_NODE *pnode, int indent) {
|
|||
printf(" ");
|
||||
}
|
||||
|
||||
if(pnode->op == TOK_AND) {
|
||||
if(pnode->op == AND) {
|
||||
printf("AND\n");
|
||||
} else if (pnode->op == TOK_OR) {
|
||||
} else if (pnode->op == OR) {
|
||||
printf("OR\n");
|
||||
}
|
||||
|
||||
if((pnode->op == TOK_AND) || (pnode->op == TOK_OR)) {
|
||||
if((pnode->op == AND) || (pnode->op == OR)) {
|
||||
pl_dump_node(pnode->arg1.plval,indent+1);
|
||||
pl_dump_node(pnode->arg2.plval,indent+1);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(pnode->arg1.ival) {
|
||||
case TOK_ARTIST:
|
||||
case ARTIST:
|
||||
printf("ARTIST ");
|
||||
break;
|
||||
case TOK_ALBUM:
|
||||
case ALBUM:
|
||||
printf("ALBUM ");
|
||||
break;
|
||||
case TOK_GENRE:
|
||||
case GENRE:
|
||||
printf("GENRE ");
|
||||
break;
|
||||
default:
|
||||
|
@ -76,11 +83,11 @@ void pl_dump_node(PL_NODE *pnode, int indent) {
|
|||
not=1;
|
||||
|
||||
switch(boolarg) {
|
||||
case TOK_IS:
|
||||
printf("%s",not? "IS " : "IS NOT ");
|
||||
case IS:
|
||||
printf("%s",not? "IS NOT " : "IS ");
|
||||
break;
|
||||
case TOK_INCLUDES:
|
||||
printf("%s",not? "INCLUDES " : "DOES NOT INCLUDE ");
|
||||
case INCLUDES:
|
||||
printf("%s",not? "DOES NOT INCLUDE " : "INCLUDES ");
|
||||
break;
|
||||
default:
|
||||
printf("<unknown boolop> ");
|
||||
|
@ -90,3 +97,131 @@ void pl_dump_node(PL_NODE *pnode, int indent) {
|
|||
printf("%s\n",pnode->arg2.cval);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* pl_load
|
||||
*
|
||||
* Load a smart playlist
|
||||
*/
|
||||
int pl_load(char *file) {
|
||||
FILE *fin;
|
||||
SMART_PLAYLIST *pcurrent;
|
||||
int result;
|
||||
|
||||
fin=fopen(file,"r");
|
||||
if(!fin) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
yyin=fin;
|
||||
result=yyparse();
|
||||
fclose(fin);
|
||||
|
||||
if(pl_error) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* register the playlists */
|
||||
DPRINTF(ERR_INFO,"Finished loading smart playlists\n");
|
||||
pcurrent=pl_smart.next;
|
||||
while(pcurrent) {
|
||||
DPRINTF(ERR_INFO,"Adding smart playlist %s as %d\n",pcurrent->name,pcurrent->id)
|
||||
db_add_playlist(pcurrent->id, pcurrent->name);
|
||||
pcurrent=pcurrent->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* pl_eval
|
||||
*
|
||||
* Run each MP3 file through the smart playlists
|
||||
*/
|
||||
void pl_eval(MP3FILE *pmp3) {
|
||||
SMART_PLAYLIST *pcurrent;
|
||||
|
||||
pcurrent=pl_smart.next;
|
||||
while(pcurrent) {
|
||||
if(pl_eval_node(pmp3,pcurrent->root)) {
|
||||
DPRINTF(ERR_DEBUG,"Matched song to playlist %s (%d)\n",pcurrent->name,pcurrent->id);
|
||||
db_add_playlist_song(pcurrent->id, pmp3->id);
|
||||
}
|
||||
|
||||
pcurrent=pcurrent->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pl_eval_node
|
||||
*
|
||||
* Test node status
|
||||
*/
|
||||
int pl_eval_node(MP3FILE *pmp3, PL_NODE *pnode) {
|
||||
int r_arg,r_arg2;
|
||||
int argtypec;
|
||||
char *argc;
|
||||
int boolarg;
|
||||
int not=0;
|
||||
int retval=0;
|
||||
|
||||
if((pnode->op == AND) || (pnode->op == OR)) {
|
||||
r_arg=pl_eval_node(pmp3,pnode->arg1.plval);
|
||||
if((pnode->op == AND) && !r_arg)
|
||||
return 0;
|
||||
if((pnode->op == OR) && r_arg)
|
||||
return 1;
|
||||
|
||||
r_arg2=pl_eval_node(pmp3,pnode->arg2.plval);
|
||||
if(pnode->op == AND)
|
||||
return r_arg && r_arg2;
|
||||
|
||||
return r_arg || r_arg2;
|
||||
}
|
||||
|
||||
/* Not an AND/OR node, so let's eval */
|
||||
switch(pnode->arg1.ival) {
|
||||
case ALBUM:
|
||||
argtypec=1;
|
||||
argc=pmp3->album;
|
||||
break;
|
||||
case ARTIST:
|
||||
argtypec=1;
|
||||
argc=pmp3->artist;
|
||||
break;
|
||||
case GENRE:
|
||||
argtypec=1;
|
||||
argc=pmp3->genre;
|
||||
break;
|
||||
}
|
||||
|
||||
boolarg=(pnode->op) & 0x7FFFFFFF;
|
||||
if(pnode->op & 0x80000000)
|
||||
not=1;
|
||||
|
||||
if(argtypec) {
|
||||
if(!argc)
|
||||
return not;
|
||||
|
||||
DPRINTF(ERR_DEBUG,"Matching %s to %s\n",argc,pnode->arg2.cval);
|
||||
|
||||
switch(boolarg) {
|
||||
case IS:
|
||||
r_arg=strcasecmp(argc,pnode->arg2.cval);
|
||||
retval = not ? r_arg : !r_arg;
|
||||
break;
|
||||
case INCLUDES:
|
||||
r_arg=strcasestr(argc,pnode->arg2.cval);
|
||||
retval = not ? !r_arg : r_arg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* can't get here */
|
||||
DPRINTF(ERR_DEBUG,"Returning %d\n",retval);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#ifndef _PL_H_
|
||||
#define _PL_H_
|
||||
|
||||
#include "mp3-scanner.h"
|
||||
|
||||
typedef struct tag_pl_node {
|
||||
int op;
|
||||
union {
|
||||
|
@ -20,12 +22,17 @@ typedef struct tag_pl_node {
|
|||
|
||||
typedef struct tag_smart_playlist {
|
||||
char *name;
|
||||
unsigned int id;
|
||||
PL_NODE *root;
|
||||
struct tag_smart_playlist *next;
|
||||
} SMART_PLAYLIST;
|
||||
|
||||
extern SMART_PLAYLIST pl_smart;
|
||||
extern int pl_error;
|
||||
|
||||
extern void pl_dump(void);
|
||||
extern int pl_load(char *file);
|
||||
extern void pl_eval(MP3FILE *pmp3);
|
||||
|
||||
#endif /* _PL_H_ */
|
||||
|
||||
|
|
Loading…
Reference in New Issue