Downloading a Text File

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ChrisMims
Posts: 7
Joined: Tue Dec 29, 2009 4:50 am

Downloading a Text File

Post by ChrisMims »

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
gotmilk065
Posts: 21
Joined: Sat Nov 28, 2009 10:56 am

Post by gotmilk065 »

Use libcurl if you want it easy :D
ChaoticXSinZ
Posts: 3
Joined: Mon Nov 30, 2009 7:11 am

libCurl Example

Post by ChaoticXSinZ »

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:

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;

}
You would then do:

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

}
This assumes you're using C++ and have already setup curl, included the headers etc.
ChrisMims
Posts: 7
Joined: Tue Dec 29, 2009 4:50 am

Post by ChrisMims »

Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib?
ChrisMims
Posts: 7
Joined: Tue Dec 29, 2009 4:50 am

Post by ChrisMims »

Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib?
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

ChrisMims wrote:(...)Also, is there a way to do this with OSLib?
just because I was looking at OSLib in the past days...
OSLib is just a graphic layer, has nothing to do with Curl... so what's the point?

A.
ChrisMims
Posts: 7
Joined: Tue Dec 29, 2009 4:50 am

Post by ChrisMims »

Ok. How do I get the Curl libraries?
whistler
Posts: 39
Joined: Tue Mar 04, 2008 7:08 am

Post by whistler »

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/
Post Reply