cURL
Haxx ad
libcurl

curl's project page on SourceForge.net

Sponsors:
Haxx

cURL > Mailing List > Monthly Index > Single Mail

curlpp mailing list Archives

[cURLpp] Problems downloading binary files to memory

From: Nazario Cipriani <NazarioCipriani_at_web.de>
Date: Thu, 21 Apr 2005 14:42:00 +0200

Hi ALL!

I've got a real problem but can't figure out what the problem is.

Basically I want to download a binary file from a remote site.
For ASCII files my code works but won't work for binary ones.
I've tried curl itself (not the cURLlib!)... And it works. So it should work for cURLpp too :-)

This is my class to "store" the result of the action and my callback function:

        class WriterMemoryClass
        {
        public:
                // Helper Class for reading result from remote host
                WriterMemoryClass()
                {
                        this->m_pBuffer = NULL;
                        this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
                        this->m_Size = 0;
                };
                ~WriterMemoryClass()
                {
                        if (this->m_pBuffer)
                                free(this->m_pBuffer);
                };
                static void* Realloc(void* ptr, size_t size)
                {
                        if(ptr)
                                return realloc(ptr, size);
                        else
                                return malloc(size);
                };
                // Callback must be declared static, otherwise it won't link...
                static size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb, void* data)
                {
                        // Calculate the real size of the incoming buffer
                        size_t realsize = size * nmemb;

                        // Cast the void* data pointer to WriterMemoryClass...
                        WriterMemoryClass* Memory = (WriterMemoryClass*) data;

                        // (Re)Allocate memory for the buffer
                        Memory->m_pBuffer = (char*) Memory->Realloc(Memory->m_pBuffer, Memory->m_Size + realsize + 1);

                        // Test if Buffer is initialized correctly & copy memory
                        if (Memory->m_pBuffer)
                        {
                                memcpy(&(Memory->m_pBuffer[Memory->m_Size]), ptr, realsize);
                                Memory->m_Size += realsize;
                                Memory->m_pBuffer[Memory->m_Size] = 0;
                        }

                        // return the real size of the buffer...
                        return realsize;
                };

                // Public member vars
                char* m_pBuffer;
                size_t m_Size;
        };

Now the following situation.
I set the WriteFunction and WriteData in this way:

                // Set the writer callback to enable cURL to write result in a memory area
                this->m_pRequest->setOpt(new cURLpp::Options::WriteFunction(CConnection::WriterMemoryClass::WriteMemoryCallback));

                // The class (WriterMemoryClass) passed to out callback function which holds our data received from the server
                this->m_pRequest->setOpt(new cURLpp::Options::WriteData((void*) (this->m_pWriterChunk)));

All ok so far...

When I step in the callback function ("WriteMemoryCallback") the following situation is given:

"size" is 1.
"nmemb" gives the length of the incoming buffer (measured in chars ??) and it seems to be right (in my case a value over 1000).
"data" is my WriterMemoryClass. Should be all ok since the local buffer must be filled by the "WriterMemoryCallback".
"ptr" is the incoming buffer containing the file I want to get.

But "ptr" has only a length of 5 (!!!) which is not right since the "nmemb" gives me the real count (??).

Now my question is, what am I doing wrong? Perhaps I've forgotten to set an option?
Must the download of binary files be done in some other way?

Hoping to hear from you soon :-)

Thank you in advance!

Kind regards,
Nazario Cipriani

-- 
Die DUMMHEIT drängt sich vor, um gesehen zu werden;
die Klugheit steht zurück, um zu sehen.
'Carmen Sylva'
______________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt bei WEB.DE FreeMail: http://f.web.de/?mc=021193
_______________________________________________
cURLpp mailing list
cURLpp_at_rrette.com
http://www.rrette.com/mailman/listinfo/curlpp
Received on 2005-04-21

These mail archives are generated by hypermail.

donate! Page updated November 12, 2010.
web site info

File upload with ASP.NET