LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2009, 12:20 PM   #1
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321
Blog Entries: 1

Rep: Reputation: 31
Newbie to libcurl API in C++ - Basic example


Hello!
I'm new to libcurl and quite newbie to C++. I'm trying to undestand how to send files to a FTP remote host.
I basically understood the example provided with libcurl-dev package (/usr/share/doc/libcurl-devel-7.18.2/ftpupload.c).
Trying to implement libcurl with my C++ program I found difficults.
As it is said in the libcurl doc, to implement Easy API with C++ requires some modifications:


libcurl with C++

There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl:

The callbacks CANNOT be non-static class member functions

Example C++ code:

class AClass {
static size_t write_data(void *ptr, size_t size, size_t nmemb,
void *ourpointer)
{
/* do what you want with the data */
}
}



Does anybody knows where I can get a C++ basic example on how to use libcurl's easy API to upload files?

Thanks in advance,
Matías
 
Old 02-27-2009, 05:31 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Your libcurl package should have come with an examples directory, have you looked there?
ftpupload.c is a good place to start.
As for the static callback, it just means that any callback function that you pass to curl must be declared as a static member function if it is encapsulated in a class.
That prevents moveable classes from being yanked out from under the write process.
Global functions would work just as well, if you didn't want to make the callback a member function.
Other than that, I think all of the curl_easy* functions are the same in C as C++.
If there's something there that isn't clear to you, please ask...
 
1 members found this post helpful.
Old 02-28-2009, 09:31 AM   #3
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321

Original Poster
Blog Entries: 1

Rep: Reputation: 31
Thanks for your answer Raconteur.
I left my example code at job. But I'll see if I can try to write uploadftp program in C++ this weekend at home. I'll let you know my results or doubts...

Thank you very much,
Matías
 
Old 03-02-2009, 07:00 PM   #4
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321

Original Poster
Blog Entries: 1

Rep: Reputation: 31
Well, I finally was able to do it work, with C++.

My guide was g++ warnings / errors. I found that I had to do some little change in the callback function. This way:


Code:
class UploadFtp
{

        static size_t read_callback(void *ptr, size_t size, size_t nmemb, FILE* stream)
        {
          /* in real-world cases, this would probably get this data differently
             as this fread() stuff is exactly what the library already would do
             by default internally */
          size_t retcode = fread(ptr, size, nmemb, stream);

          fprintf(stderr, "*** We read %d bytes from file\n", retcode);
          return retcode;
        }


        private:

            char user [20];
            char pwd [20];
            char remote_url [128];
            char local_file [128];
            char remote_filename [128];


        public:
        (...)
Based on g++ I changed the void pointer "void * stream" to "FILE * stream". Code is working. But I'd like to know more details on how the callback function works and why is needed? Do you have some clue where I can find more info about it?

Regards,
Matías
 
Old 03-03-2009, 11:30 AM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
The stream pointer in the default implementation is declared as a void pointer to allow it to be cast as necessary. It could be an ostream class or file descriptor, etc. Your implementation is good, there are many different ways to do it.

When you make a call to a function in a dynamically linked library, it is sometimes necessary for that library to "call back" into the calling application.
In this case, libcurl needs to know what to do with the data it is recieving or transmitting (where and how to store it or where and how to get it).
The library needs that to be a rather open interface to be flexible, so it provides a framework for the library to call back to user-defined functions.
 
Old 03-03-2009, 07:30 PM   #6
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321

Original Poster
Blog Entries: 1

Rep: Reputation: 31
Thank you very much for your explanation. It's much clearer for me now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java API for basic remote file system operations foampile Linux - Newbie 1 02-26-2009 11:25 PM
Complete newbie - some basic help yoghurt Linux - Newbie 7 04-14-2005 10:47 AM
Basic newbie help with dselect emgee Debian 2 01-11-2004 02:51 PM
Your basic newbie question... rcarr Linux - Newbie 1 09-27-2003 02:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration