Frank Weissenborn / Shoutcast

FileDownloader.cpp

Committer:
FrankWeissenborn
Date:
2011-01-24
Revision:
0:d9d38ebc0f69
Child:
1:1b5ed0fa82f0

File content as of revision 0:d9d38ebc0f69:

#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;
}