forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Downloading a Text File

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ChrisMims



Joined: 29 Dec 2009
Posts: 7

PostPosted: Tue Dec 29, 2009 5:07 am    Post subject: Downloading a Text File Reply with quote

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
Back to top
View user's profile Send private message
gotmilk065



Joined: 28 Nov 2009
Posts: 25

PostPosted: Tue Dec 29, 2009 11:26 am    Post subject: Reply with quote

Use libcurl if you want it easy :D
Back to top
View user's profile Send private message
ChaoticXSinZ



Joined: 30 Nov 2009
Posts: 4

PostPosted: Tue Dec 29, 2009 1:54 pm    Post subject: libCurl Example Reply with quote

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:
/**
 * 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:
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.
Back to top
View user's profile Send private message
ChrisMims



Joined: 29 Dec 2009
Posts: 7

PostPosted: Thu Dec 31, 2009 1:11 am    Post subject: Reply with quote

Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib?
Back to top
View user's profile Send private message
ChrisMims



Joined: 29 Dec 2009
Posts: 7

PostPosted: Sun Jan 10, 2010 3:43 am    Post subject: Reply with quote

Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib?
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Sun Jan 10, 2010 7:09 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
ChrisMims



Joined: 29 Dec 2009
Posts: 7

PostPosted: Sun Jan 10, 2010 7:44 am    Post subject: Reply with quote

Ok. How do I get the Curl libraries?
Back to top
View user's profile Send private message
whistler



Joined: 04 Mar 2008
Posts: 40

PostPosted: Mon Jan 11, 2010 1:53 am    Post subject: Reply with quote

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/
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group