Revision:
4:520664d73063
Parent:
3:fb54b479a1b7
Child:
5:d5d16fd31139
--- a/Shoutcast.cpp	Thu Jan 27 20:21:08 2011 +0000
+++ b/Shoutcast.cpp	Fri Jan 28 11:13:38 2011 +0000
@@ -5,22 +5,14 @@
 #include "FileDownloader.h"
 
 
-const char* FOLDER = "/sd/mydir/";
-const char* FILECHANNEL = "/sd/mydir/channel.xml";
-const char* FILECHANNELMOD = "/sd/mydir/cm.xml";
-const char* FILETOP500 = "/sd/mydir/top500.xml";
-const char* FILEPLS = "/sd/mydir/currChan.pls";
-const char* FILEPLSMOD = "/sd/mydir/ChanM";
+#define FOLDER          "/sd/mydir/"
+#define FILECHANNEL     "/sd/mydir/channel.xml"
+#define FILECHANNELMOD  "/sd/mydir/cm.xml"
+#define FILETOP500      "/sd/mydir/top500.xml"
+#define FILEPLS         "/sd/mydir/currChan.pls"
+#define FILEPLSMOD      "/sd/mydir/ChanM"
 
-const char*  GENRE[25] = {"Alternative","Blues","Classical","Country","Decades","Easy Listening","Electronic","Folk","Inspirational","International","Jazz","Latin","Metal","Misc","New Age","Pop","Public Radio","R&B/Urban","Rap","Reggae","Rock","Seasonal/Holiday","Soundtracks","Talk","Themes"};
-
-unsigned int genreCounter = 0;
-unsigned int maxChannel = 0;
-unsigned int currentChannel = 0;
-unsigned int maxAddress = 0;
-unsigned int currentAddress = 0;
-FILE *fpin;
-FILE *fpout;
+const char* const GENRE[25] = {"Alternative","Blues","Classical","Country","Decades","Easy Listening","Electronic","Folk","Inspirational","International","Jazz","Latin","Metal","Misc","New Age","Pop","Public Radio","R&B/Urban","Rap","Reggae","Rock","Seasonal/Holiday","Soundtracks","Talk","Themes" };
 
 int completed_dns;
 
@@ -31,47 +23,53 @@
     _big_buffer_size = big_buffer_size;
     _small_buffer = small_buffer;
     _small_buffer_size = small_buffer_size;
+    _genreCounter = 0;
+    _maxChannel = 0;
+    _currentChannel = 0;
+    _maxAddress = 0;
+    _currentAddress = 0;
+
     mkdir(FOLDER, 0777);
 
 }
 
 const char* Shoutcast::GetNextGenre() {
-    if (genreCounter < 24) {
-        genreCounter++;
+    if (_genreCounter < 24) {
+        _genreCounter++;
     } else {
-        genreCounter = 0;
+        _genreCounter = 0;
     }
     
-    maxChannel = 0;
-    currentChannel = 0;
-    maxAddress = 0;
-    currentAddress = 0;
+    _maxChannel = 0;
+    _currentChannel = 0;
+    _maxAddress = 0;
+    _currentAddress = 0;
 
-    return GENRE[genreCounter];
+    return GENRE[_genreCounter];
    
 }
 
 const char* Shoutcast::GetPrevGenre() {
-    if (genreCounter > 0) {
-        genreCounter--;
+    if (_genreCounter > 0) {
+        _genreCounter--;
     } else {
-        genreCounter = 24;
+        _genreCounter = 24;
     }
 
-    maxChannel = 0;
-    currentChannel = 0;
-    maxAddress = 0;
-    currentAddress = 0;
+    _maxChannel = 0;
+    _currentChannel = 0;
+    _maxAddress = 0;
+    _currentAddress = 0;
 
-    return GENRE[genreCounter];
+    return GENRE[_genreCounter];
 }
 const char* Shoutcast::GetCurrGenre() {
-    return GENRE[genreCounter];
+    return GENRE[_genreCounter];
 }
 
 int Shoutcast::GetChannel(char* name, int length) {
     DEBUGOUT("Shoutcast:GetChannel started\r\n");
-    DEBUGOUT("Shoutcast: Download channellist:%s started\r\n",GENRE[genreCounter]);
+    DEBUGOUT("Shoutcast: Download channellist:%s started\r\n",GENRE[_genreCounter]);
     strcpy (_big_buffer,"http://api.shoutcast.com/legacy/genresearch?k=");
             
     DEBUGOUT("strcpy ok\r\n");
@@ -79,22 +77,22 @@
     DEBUGOUT("strcat1 ok\r\n");
     strcat (_big_buffer,"&genre=");
     DEBUGOUT("strcat1 ok\r\n");
-    strcat (_big_buffer,GENRE[genreCounter]);
+    strcat (_big_buffer,GENRE[_genreCounter]);
     DEBUGOUT("strcat1 ok\r\n");
     strcat (_big_buffer,"&f=xml");
     DEBUGOUT("strcat1 ok\r\n");
     DEBUGOUT("%s\r\n",_big_buffer);
 
-    HTTPResult result = GetFile(_big_buffer,FILECHANNEL,fpout,_small_buffer,_small_buffer_size);
+    HTTPResult result = GetFile(_big_buffer,FILECHANNEL,_fpout,_small_buffer,_small_buffer_size);
     if(result != HTTP_OK)
     {
-        DEBUGOUT("Shoutcast: Download channellist:%s ended with error %i.\r\n",GENRE[genreCounter],result);
+        DEBUGOUT("Shoutcast: Download channellist:%s ended with error %i.\r\n",GENRE[_genreCounter],result);
         return 1;
     }
-    DEBUGOUT("Shoutcast: Download channellist:%s successfully completed.\r\n",GENRE[genreCounter]);
+    DEBUGOUT("Shoutcast: Download channellist:%s successfully completed.\r\n",GENRE[_genreCounter]);
     ParseChannelList(FILECHANNEL);
 
-    currentChannel = 0;
+    _currentChannel = 0;
     return GetChannel(name, length, 0);
 }
 
@@ -104,7 +102,7 @@
     strcpy (_big_buffer,"http://api.shoutcast.com/legacy/Top500?k=");
     strcat (_big_buffer,_devId);
     DEBUGOUT("%s\n",_big_buffer);
-     HTTPResult result = GetFile(_big_buffer,FILETOP500,fpout,_small_buffer,_small_buffer_size);
+     HTTPResult result = GetFile(_big_buffer,FILETOP500,_fpout,_small_buffer,_small_buffer_size);
     if(result != HTTP_OK)
     {
         DEBUGOUT("Shoutcast: Download Top500 ended with error %i.\r\n",result);
@@ -112,43 +110,62 @@
     }
     DEBUGOUT("Shoutcast: Download Top500 successfully completed.\r\n");
     ParseChannelList(FILETOP500);
-    currentChannel = 0;
+    _currentChannel = 0;
     return GetChannel(name, length, 0);
 }
-int Shoutcast::GetPrevChannel(char*name, int length) {
-    if(currentChannel > 0){
-        currentChannel--;
+int Shoutcast::GetPrevChannel(char* name, int length) {
+    
+    if(_maxChannel == 0){
+    return -2;
+    }
+    
+    if(_currentChannel > 0){
+        _currentChannel--;
     }
     else {
-    currentChannel = maxChannel - 1;
+    _currentChannel = _maxChannel - 1;
+    }
+    
+    if((length == 0) || (name == NULL)) {
+        return 0;
     }
-  
-    maxAddress = 0;
-    currentAddress = 0;
+    
+    _maxAddress = 0;
+    _currentAddress = 0;
 
-    return GetChannel(name, length, currentChannel);
+    return GetChannel(name, length, _currentChannel);
 }
 int Shoutcast::GetNextChannel(char* name, int length) {
-    if(currentChannel < maxChannel - 1) {
-        currentChannel++;
+    
+    if(_maxChannel == 0){
+        return -2;
+    }
+    
+    
+    if(_currentChannel < _maxChannel - 1) {
+        _currentChannel++;
     }
     else {
-    currentChannel=0;
+    _currentChannel=0;
     }
+    
+    if((length == 0) || (name == NULL)) {
+        return 0;
+    }
+    
+    _maxAddress = 0;
+    _currentAddress = 0;
 
-    maxAddress = 0;
-    currentAddress = 0;
-
-    return GetChannel(name, length, currentChannel);
+    return GetChannel(name, length, _currentChannel);
 }
 
 int Shoutcast::GetCurrChannel(char* name, int length) {
-    return GetChannel(name, length, currentChannel);
+    return GetChannel(name, length, _currentChannel);
 }
 
 int Shoutcast::TuneIn(IpAddr* address, int* port) {
     char buf[10];
-    sprintf(buf,"%d",GetChannelId(currentChannel));
+    sprintf(buf,"%d",GetChannelId(_currentChannel));
      
     strcpy (_big_buffer,"http://yp.shoutcast.com/sbin/tunein-station.pls?&k=");
     strcat (_big_buffer,_devId);
@@ -156,14 +173,14 @@
     strcat (_big_buffer,buf); //todo: int to string
     strcat (_big_buffer,"&f=xml");
     DEBUGOUT("Shoutcast: %s\r\n",_big_buffer);
-    HTTPResult result = GetFile(_big_buffer,FILEPLS,fpout,_small_buffer,_small_buffer_size);
+    HTTPResult result = GetFile(_big_buffer,FILEPLS,_fpout,_small_buffer,_small_buffer_size);
     DEBUGOUT("Shoutcast: result PLS: %d\n", result);
-    //TODO: PLS Parsen, IP und Port zur&#65533;ckgeben
+    
     if(ParsePls() != 0x00)
     {   
         return 1;
     }
-    currentAddress = 0;
+    _currentAddress = 0;
     return GetAddressData(address,port,0);
 }
 
@@ -172,15 +189,15 @@
 int Shoutcast::ParseChannelList(const char* channelList) {
     
     DEBUGOUT("Shoutcast: Parsing started\r\n");
-    maxChannel = 0;
+    _maxChannel = 0;
     
-    fpin = fopen(channelList, "r");
-    if (fpin == NULL) {
+    _fpin = fopen(channelList, "r");
+    if (_fpin == NULL) {
         error("Could not open file for write\n");
     } 
     
-    fpout = fopen(FILECHANNELMOD, "w");
-    if (fpout == NULL) {
+    _fpout = fopen(FILECHANNELMOD, "w");
+    if (_fpout == NULL) {
         error("Could not open file for write\n");
     }
     
@@ -189,7 +206,7 @@
     char * station1; char * station2 = NULL;
 
     int i=0;
-    bool finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+    bool finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
     _big_buffer[_big_buffer_size-1] = 0x00;
     cp = _big_buffer;
 
@@ -199,12 +216,12 @@
                 int x = (_big_buffer + _big_buffer_size) - cp2 - 1;
                 int read_size = _big_buffer_size-(x-1);
 
-                finished = (fgets(_big_buffer+x-1, read_size,fpin) == NULL);
+                finished = (fgets(_big_buffer+x-1, read_size,_fpin) == NULL);
                 _big_buffer[_big_buffer_size-1] = 0x00;
                 cp=_big_buffer;
 
             } else {
-                finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+                finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
                 _big_buffer[_big_buffer_size-1] = 0x00;
                 cp = _big_buffer;
             }
@@ -229,8 +246,8 @@
                   if(station2 != NULL)
                   {
                     station2[0]=0x00;
-                    fprintf(fpout,"%s<>",station1);
-                    maxChannel++;
+                    fprintf(_fpout,"%s<>",station1);
+                    _maxChannel++;
                   }
                 }
                 //id suchen
@@ -243,7 +260,7 @@
                   if(station2 != NULL)
                   {
                     station2[0]=0x00;
-                    fprintf(fpout,"%s\r\n",station1);
+                    fprintf(_fpout,"%s\r\n",station1);
                   }
                 }
                 
@@ -253,26 +270,26 @@
                 strcpy(_big_buffer,cp);
                 int x = (_big_buffer + _big_buffer_size) - cp - 1;
                 int read_size = _big_buffer_size-(x-1);
-                finished = (fgets(_big_buffer+x-1, read_size,fpin) == NULL);
+                finished = (fgets(_big_buffer+x-1, read_size,_fpin) == NULL);
                 _big_buffer[_big_buffer_size-1] = 0x00;
                 cp = _big_buffer;
             }
         }
 
     }
-    fclose(fpin);
-    fclose(fpout);
+    fclose(_fpin);
+    fclose(_fpout);
     DEBUGOUT("Shoutcast: Parsing completed\r\n");
     return true;
 }
 
 int Shoutcast::GetChannel(char* name, int length, int channelNumber)
 {   
-    if(maxChannel == 0) return -1;
-    if(channelNumber > maxChannel) return -1;
+    if(_maxChannel == 0) return -1;
+    if(channelNumber > _maxChannel) return -1;
 
-    fpin = fopen(FILECHANNELMOD, "r");
-    if (fpin == NULL) {
+    _fpin = fopen(FILECHANNELMOD, "r");
+    if (_fpin == NULL) {
         error("Could not open file for write\n");
     }
     bool finished = false;
@@ -280,7 +297,7 @@
     char * delimiter;
     
     while(!finished) {
-        finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+        finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
         if(channelNumber == line)
         {
             delimiter=strstr(_big_buffer,"<>");
@@ -296,14 +313,14 @@
             }
             else
             {
-                fclose(fpin);
+                fclose(_fpin);
                 return 1;
             }
         }
         line++;
     }
     
-    fclose(fpin);
+    fclose(_fpin);
     return 0;
 }
 
@@ -311,8 +328,8 @@
 int Shoutcast::GetChannelId(int channelNumber)
 {   
     DEBUGOUT("Shoutcast: GetChannelId started\r\n");
-    fpin = fopen(FILECHANNELMOD, "r");
-    if (fpin == NULL) {
+    _fpin = fopen(FILECHANNELMOD, "r");
+    if (_fpin == NULL) {
         error("Could not open file for write\n");
     }
     bool finished = false;
@@ -320,7 +337,7 @@
     char * delimiter;
     
     while(!finished) {
-        finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+        finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
         if(channelNumber == line)
         {
             DEBUGOUT("Shoutcast: GetChannelId Line found\r\n");
@@ -330,30 +347,30 @@
             {
                  id = atoi(delimiter+strlen("<>"));
                 //id = 4;
-                fclose(fpin);
+                fclose(_fpin);
                 return id;  
             }
             else
             {
-                fclose(fpin);
+                fclose(_fpin);
                 return -1;
             }
         }
         line++;
     }
-    fclose(fpin);
+    fclose(_fpin);
     return -1;
 }
 
 int Shoutcast::ParsePls() {
     DEBUGOUT("Shoutcast: start parsing pls\r\n");
     
-fpin = fopen(FILEPLS, "r");
- if (fpin == NULL) {
+_fpin = fopen(FILEPLS, "r");
+ if (_fpin == NULL) {
         error("Could not open file for read\n");
     }
-fpout = fopen(FILEPLSMOD, "w");
- if (fpout == NULL) {
+_fpout = fopen(FILEPLSMOD, "w");
+ if (_fpout == NULL) {
         error("Could not open file for write\n");
     }
 bool finished = false;
@@ -362,7 +379,7 @@
     char * cp2;
     IpAddr addr;
     while(!finished) {
-        finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+        finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
         
         //suchen nach 
         cp = strstr(_big_buffer,"=http://");
@@ -378,21 +395,21 @@
                 if(dnserr != 0)
                 {   
                     DEBUGOUT("Shoutcast: DNS Error - Parsing Pls\r\n");
-                    fclose(fpin);
-                    fclose(fpout);
+                    fclose(_fpin);
+                    fclose(_fpout);
                     
                     return -1;
                 }
                 int port = atoi(cp2+1);
-                fprintf(fpout, "%d %d %d %d %d\r\n",
+                fprintf(_fpout, "%d %d %d %d %d\r\n",
                        addr[0],addr[1],addr[2],addr[3],port);
-                maxAddress++;
+                _maxAddress++;
             }
         }
         _big_buffer[_big_buffer_size-1] = 0x00;
         }
-    fclose(fpin);
-    fclose(fpout);
+    fclose(_fpin);
+    fclose(_fpout);
     
     DEBUGOUT("Shoutcast: end parsing pls\r\n");
     return 0;
@@ -424,16 +441,16 @@
 
 int Shoutcast::GetAddressData(IpAddr* address, int* port, int addressNumber) {
 
-    if(maxAddress == 0) return -1;
-    if(addressNumber > maxAddress) return -1;
+    if(_maxAddress == 0) return -1;
+    if(addressNumber > _maxAddress) return -1;
 
     DEBUGOUT("Shoutcast: start get address data\r\n");
-    fpin = fopen(FILEPLSMOD, "r");
+    _fpin = fopen(FILEPLSMOD, "r");
     bool finished = false;
     int line = 0;
     
     while(!finished) {
-        finished = (fgets(_big_buffer, _big_buffer_size-1, fpin) == NULL);
+        finished = (fgets(_big_buffer, _big_buffer_size-1, _fpin) == NULL);
         if(line == addressNumber){
             int ip1 = 0;int ip2 = 0;int ip3 = 0;int ip4 = 0;int p = 0;
             sscanf (_big_buffer,"%d %d %d %d %d",&ip1,&ip2,&ip3,&ip4,&p);
@@ -457,30 +474,30 @@
 
 int Shoutcast::GetNextAddress(IpAddr* address, int* port)
 {
-    if(currentAddress < maxAddress - 1 )
+    if(_currentAddress < _maxAddress - 1 )
     {
-        currentAddress++;
+        _currentAddress++;
     }
     else
     {
-        currentAddress = 0;
+        _currentAddress = 0;
     }
-    return GetAddressData(address,port,currentAddress);
+    return GetAddressData(address,port,_currentAddress);
     
 }
 int Shoutcast::GetCurrAddress(IpAddr* address, int* port)
 {
-    return GetAddressData(address,port,currentAddress);
+    return GetAddressData(address,port,_currentAddress);
 }
 int Shoutcast::GetPrevAddress(IpAddr* address, int* port)
 {
-    if(currentAddress > 0 )
+    if(_currentAddress > 0 )
     {
-        currentAddress--;
+        _currentAddress--;
     }
     else
     {
-        currentAddress = maxAddress - 1;
+        _currentAddress = _maxAddress - 1;
     }
-    return GetAddressData(address,port,currentAddress);
+    return GetAddressData(address,port,_currentAddress);
 }
\ No newline at end of file