Frank Weissenborn / Shoutcast
Revision:
0:d9d38ebc0f69
Child:
1:1b5ed0fa82f0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FileDownloader.cpp	Mon Jan 24 13:32:52 2011 +0000
@@ -0,0 +1,53 @@
+#include "FileDownloader.h"
+
+
+#include "SDFileSystem.h"
+
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
+
+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)
+{
+completed = false;
+    
+     fp = fopen(filename, "w");
+     if(fp == NULL) {
+         error("Could not open file for write\n");
+     }
+     else {
+      printf("Opened file");
+     }
+
+
+    char BigBuf[512 + 1] = {0};
+    stream.readNext((byte*)BigBuf, 512); //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
+            
+            int p=0;
+            while (BigBuf[p] != 0)
+            {
+                fprintf(fp,"%c",BigBuf[p]);
+                p++;
+            }
+            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
+        }
+    }
+    
+   fclose(fp);
+   return result;
+}
\ No newline at end of file