Frank Weissenborn / Shoutcast
Revision:
2:0e9a10f2b8c5
Parent:
1:1b5ed0fa82f0
--- a/FileDownloader.cpp	Mon Jan 24 13:54:12 2011 +0000
+++ b/FileDownloader.cpp	Thu Jan 27 20:19:24 2011 +0000
@@ -4,47 +4,49 @@
 extern SDFileSystem sd;
 HTTPResult result;
 bool completed = false;
-FILE *fp;
 HTTPClient http;
 HTTPStream stream;
 
+
 void request_callback(HTTPResult r) {
     result = r;
     completed = true;
 }
 
-HTTPResult GetFile(const char *uri, const char *filename)
+HTTPResult GetFile(const char *uri, const char *filename, FILE *fp, char * buffer, int buffer_size)
 {
-completed = false;
     
+    completed = false;
+
      fp = fopen(filename, "w");
      if(fp == NULL) {
          error("Could not open file for write\n");
      }
      else {
-      printf("Opened file");
+      printf("Opened file\r\n");
      }
 
 
-    char BigBuf[512 + 1] = {0};
-    stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
+    buffer[buffer_size-1] = 0x00;  
+    stream.readNext((byte*)buffer, buffer_size-2); //Point to buffer for the first read
     HTTPResult r = http.get(uri, &stream, request_callback);
     
     while (!completed) {
         Net::poll(); //Polls the Networking stack
         if (stream.readable()) {
-            BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
+            buffer[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
             
             int p=0;
-            while (BigBuf[p] != 0)
+            while (buffer[p] != 0)
             {
-                fprintf(fp,"%c",BigBuf[p]);
+                fprintf(fp,"%c",buffer[p]);
                 p++;
             }
-            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
+            stream.readNext((byte*)buffer, buffer_size-2); //Buffer has been read, now we can put more data in it
         }
     }
     
    fclose(fp);
+
    return result;
 }
\ No newline at end of file