Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FileDownloader.cpp
- Committer:
- FrankWeissenborn
- Date:
- 2011-01-30
- Revision:
- 7:f652d1e054e9
- Parent:
- 2:0e9a10f2b8c5
File content as of revision 7:f652d1e054e9:
#include "FileDownloader.h"
#include "SDFileSystem.h"
extern SDFileSystem sd;
HTTPResult result;
bool completed = false;
HTTPClient http;
HTTPStream stream;
void request_callback(HTTPResult r) {
result = r;
completed = true;
}
HTTPResult GetFile(const char *uri, const char *filename, FILE *fp, char * buffer, int buffer_size)
{
completed = false;
fp = fopen(filename, "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
else {
printf("Opened file\r\n");
}
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()) {
buffer[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
int p=0;
while (buffer[p] != 0)
{
fprintf(fp,"%c",buffer[p]);
p++;
}
stream.readNext((byte*)buffer, buffer_size-2); //Buffer has been read, now we can put more data in it
}
}
fclose(fp);
return result;
}