2 Copyright 2004-2005 Chris Tallon, Andreas Vogel
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
30 Media::Media(int type, const char * filename, int time){
33 int len=strlen(filename)+1;
34 _filename=new char[len];
35 strcpy(_filename,filename);
46 const char * Media::getFilename() {
50 int Media::getType() {
53 int Media::getTime() {
59 const char* extension;
62 {".mp3",MEDIA_TYPE_AUDIO},
63 {".MP3",MEDIA_TYPE_AUDIO},
64 {".jpg",MEDIA_TYPE_PICTURE},
65 {".JPG",MEDIA_TYPE_PICTURE},
66 {".jpeg",MEDIA_TYPE_PICTURE},
67 {".JPEG",MEDIA_TYPE_PICTURE}
69 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
72 //helper from vdr tools.c
73 bool endswith(const char *s, const char *p)
75 const char *se = s + strlen(s) - 1;
76 const char *pe = p + strlen(p) - 1;
78 if (*pe-- != *se-- || (se < s && pe >= p))
85 MediaList * MediaList::readList(Config * cfg,const char * dirname, int type) {
87 if (dirname == NULL) {
88 //the configured Media List
89 //for the moment up to 10 entries [Media] Dir.1 ...Dir.10
90 for (int nr=1;nr<=10;nr++){
92 sprintf(buffer,"Dir.%d",nr);
93 const char * dn=cfg->getValueString("Media",buffer);
95 if (rt == NULL) rt=new MediaList();
96 Media *m=new Media(MEDIA_TYPE_DIR,dn,0);
98 Log::getInstance()->log("Media",Log::DEBUG,"added base dir %s",dn);
102 if (rt != NULL) return rt;
103 //if nothing is configured, we start at /
104 if (dirname == NULL) dirname="/";
106 //open the directory and read out the entries
107 DIR *d=opendir(dirname);
109 union { // according to "The GNU C Library Reference Manual"
111 char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
114 while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
116 const char * fname=e->d_name;
117 if ( fname == NULL) continue;
118 if (strcmp(fname,".") == 0) continue;
120 asprintf(&buffer, "%s/%s", dirname, e->d_name);
122 int mtype=MEDIA_TYPE_UNKNOWN;
123 if (stat(buffer, &st) == 0) {
124 if (S_ISDIR(st.st_mode)) {
125 mtype=MEDIA_TYPE_DIR;
128 for (int i=0;i<NUMTYPES;i++) {
129 if (endswith(fname,mediatypes[i].extension)) {
130 mtype=mediatypes[i].type;
137 //only consider entries we accept by type here
139 Media * m =new Media(mtype,fname,(int)(st.st_mtime));
141 Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,mtype);
145 if (d != NULL) closedir(d);
149 MediaList::~MediaList() {
150 MediaList::iterator it=this->begin();
151 while (it < this->end()) {