00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "Playlist.hpp"
00019 #include "Globals.hpp"
00020
00021 #include <cstdlib>
00022 #include <cstdio>
00023 #include <libintl.h>
00024 #define _(string) gettext(string)
00025
00026 int CPlaylist::init(std::string filename)
00027 {
00028 bool extended;
00029 m_Songs.clear();
00030 char buffer[255];
00031 std::string Buffer;
00032 int count = 0;
00033 CPlaylist::SSongInfo tmp;
00034 FILE* the_file = fopen(filename.c_str(), "r");
00035 if(the_file == NULL)
00036 {
00037 return 0;
00038 }
00039 fgets(buffer,255,the_file);
00040 Buffer = buffer;
00041 if (Buffer == "#EXTM3U\n")
00042 {
00043 while(fgets(buffer, 255, the_file) != 0)
00044 {
00045 Buffer = buffer;
00046 std::string test(Buffer, 0, 8);
00047 if (test != "#EXTINF:")
00048 {
00049 LOG4CXX_ERROR(log_, test.c_str());
00050 LOG4CXX_ERROR(log_, _("Error while reading the Playlist!"));
00051 return 0;
00052 }
00053 sscanf(Buffer.c_str(), "#EXTINF:%d", &(tmp.length));
00054 int i = Buffer.find(",");
00055 std::string info(Buffer, i + 1, (Buffer.size() - (i+2)));
00056 if (fgets(buffer, 255, the_file) == NULL)
00057 {
00058 LOG4CXX_ERROR(log_, _("Error while reading the Playlist!"));
00059 return 0;
00060 }
00061 Buffer = buffer;
00062 std::string sfilename(Buffer, 0, (Buffer[Buffer.size()-1]== '\n') ? Buffer.size() - 1 : Buffer.size());
00063 tmp.filename = sfilename;
00064 tmp.title = info;
00065 m_Songs.push_back(tmp);
00066 ++count;
00067 }
00068 }
00069 else
00070 {
00071 tmp.filename = Buffer;
00072 std::string info(Buffer, 0, Buffer.size() - 4);
00073 tmp.title = info;
00074 tmp.length = 0;
00075 m_Songs.push_back(tmp);
00076 while (fgets(buffer, 255, the_file) != 0)
00077 {
00078 Buffer = buffer;
00079 tmp.filename = Buffer;
00080 std::string info(Buffer, 0, Buffer.size() - 4);
00081 tmp.title = info;
00082 tmp.length = 0;
00083 m_Songs.push_back(tmp);
00084 ++count;
00085 }
00086 }
00087 ++count;
00088
00089 std::fclose(the_file);
00090 return count;
00091 }
00092
00093 std::string CPlaylist::getRandSong()
00094 {
00095 unsigned count = m_Songs.size();
00096 if (count == 0) return "";
00097 m_actualSong = std::rand()%count;
00098
00099 if(p_HUD)
00100 {
00101 char tmp[255];
00102 if (m_Songs[m_actualSong].length)
00103 {
00104 sprintf(tmp, "%s (%d:%d)", m_Songs[m_actualSong].title.c_str(), m_Songs[m_actualSong].length/60, m_Songs[m_actualSong].length%60);
00105 }
00106 else
00107 {
00108 sprintf(tmp, "%s (unbekannt)", m_Songs[m_actualSong].title.c_str());
00109 }
00110 *p_HUD = tmp;
00111 }
00112
00113 return m_Songs[m_actualSong].filename;
00114 }