Can anyone tell me how to download "file1.txt" from "examplesite.com/file1.txt", and have it placed in "ms0/psp/game/MyGame"?
Please help, and thanks in advance,
-ChrisMims
Downloading a Text File
-
- Posts: 3
- Joined: Mon Nov 30, 2009 7:11 am
libCurl Example
As the above poster said, you can use libCurl to easily download files.
First you have to make sure you're actually connected to the internet. You can manually connect to Access points or use the net dialog. You can find examples in the pspsdk samples folder.
Here is an example download function:
You would then do:
This assumes you're using C++ and have already setup curl, included the headers etc.
First you have to make sure you're actually connected to the internet. You can manually connect to Access points or use the net dialog. You can find examples in the pspsdk samples folder.
Here is an example download function:
Code: Select all
/**
* Write for cUrl response.
*/
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData) {
if (writerData == NULL)
return false;
writerData->append(data, size * nmemb);
return size * nmemb;
}
/**
* Download a file using cURL.
*
* @param std::string url The url to download from.
* @param std::string &response Pointer to string object to populate with response. (Which can also be error message.)
*
* @return int If -1 then error, other wise your good.
*/
int downloadFile(std::string url, std::string &response) {
// Error buffer
char errorBuffer[CURL_ERROR_SIZE];
// cURL Handle
CURL *curl = curl_easy_init();
// cURL Code
CURLcode res;
// HTTP Header list
struct curl_slist *headersList = NULL;
// Make sure we have a valid handle
if (curl != NULL) {
// Setup error buffer
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
// Set Options:
// Allow following 'Location' headers
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// Set URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Add User Agent to headers list
headersList = curl_slist_append(headersList, "MyPSPApp/1.0");
// Set Connection to close
headersList = curl_slist_append(headersList, "Connection: Close");
// Set encoding to ALL
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
// Set HTTP Headers
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headersList);
// Set writer function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
// Set write buffer
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Perform request
res = curl_easy_perform(curl);
// Error
if (res != CURLE_OK) {
// Set response to error message
response.assign(errorBuffer);
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return -1;
}
} else {
// Unable to create curl connection
// Set response to error message
response.assign("Unable to create cURL connection.");
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return -1;
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return 1;
}
Code: Select all
std::string data;
int res = downloadFile("http://example.com/file.txt", data);
if (data != -1) {
// Use data, save to file etc
} else {
// Handle error
}
svn checkout svn://svn.ps2dev.org/pspware/trunk/libcurl
that should do it and if you want to have a browse at whats available
http://svn.ps2dev.org/
that should do it and if you want to have a browse at whats available
http://svn.ps2dev.org/