473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Web pages extraction & Libcurl

Good morning.

I've tried to extract, using libcurl, web pages but it failed. There
is no compilation error concerning the class I wrote, but the problems
appear when I compile a main method which calls this class.

My class :

**********************************libcurl_tools.h

#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};


class libcurl_tools {
CURL * curl_handle;
MemoryStruct chunk;
public:

libcurl_tools();
void init();
bool close();
void set_referrer(std::string, CURL *);
std::string perform(std::string, CURL *);

};

**********************************libcurl_tools.cp p

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#include <iostream// pour std::cout
#include <string // pour std::string

#include "libcurl_tools.h"

libcurl_tools::libcurl_tools() {}

static void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}

static size_t WriteMemoryCallback(void *ptr, size_t size, size_t
nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;

mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize +
1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}


void libcurl_tools::set_referrer(std::string referer, CURL
*curl_handle ) {

curl_easy_setopt(curl_handle, CURLOPT_REFERER, referer.c_str());

}


//initialise la connection
void libcurl_tools::init() {


chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */
curl_handle = curl_easy_init();


}
//télécharge l'url en argument
std::string libcurl_tools::perform(std::string url , CURL
*curl_handle) {

/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

/* some servers don't like requests that are made without a user-
agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/
1.0");

/* get it! */
curl_easy_perform(curl_handle);

/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);

/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*
* You should be aware of the fact that at this point we might have
an
* allocated data block, and nothing has yet deallocated that data.
So when
* you're done with it, you should free() it as a nice application.
*/


//return chunk.memory; // les 2 ont l'air de marcher (pour le
moment...)
return std::string(chunk.memory);

}
bool libcurl_tools::close() {

if(chunk.memory)
free(chunk.memory);

return 0;
}

*********************************** test.cpp which includes the main()
#include <iostream>
#include <string>
#include <sstream>
#include "libcurl_tools.h"
//#include <stdio.h>

using namespace std;
int main()
{

libcurl_tools e();

e.init();

std::string chaine;
std:: string s = "www.yahoo.com";
chaine = e.perform(s);

e.close();

}

There are 3 compilation errors concerning the calls of init(),
perform() and close() methods, and I really don't know why it happens.
I wish you could help me guys...

Thanks

Nov 9 '07 #1
4 2986
Choi wrote:
Good morning.

I've tried to extract, using libcurl, web pages but it failed. There
is no compilation error concerning the class I wrote, but the problems
appear when I compile a main method which calls this class.

My class :
[snip]
>
int main()
{

libcurl_tools e();
did you mean:
libcurl_tools e;

instead?
>
e.init();

std::string chaine;
std:: string s = "www.yahoo.com";
chaine = e.perform(s);

e.close();

}

There are 3 compilation errors concerning the calls of init(),
perform() and close() methods, and I really don't know why it happens.
I wish you could help me guys...
Might be helpful in the future if you could post the errors too...

Alan
Nov 9 '07 #2
On Nov 9, 11:50 am, Choi <Evil.C...@gmail.comwrote:
Good morning.

I've tried to extract, using libcurl, web pages but it failed. There
is no compilation error concerning the class I wrote, but the problems
appear when I compile a main method which calls this class.

My class :

**********************************libcurl_tools.h

#include <curl/curl.h>

struct MemoryStruct {
char *memory;
size_t size;

};

class libcurl_tools {

CURL * curl_handle;
MemoryStruct chunk;

public:

libcurl_tools();
void init();
bool close();
void set_referrer(std::string, CURL *);
std::string perform(std::string, CURL *);

};

**********************************libcurl_tools.cp p

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#include <iostream// pour std::cout
#include <string // pour std::string

#include "libcurl_tools.h"

libcurl_tools::libcurl_tools() {}

static void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);

}

static size_t WriteMemoryCallback(void *ptr, size_t size, size_t
nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;

mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize +
1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;

}

void libcurl_tools::set_referrer(std::string referer, CURL
*curl_handle ) {

curl_easy_setopt(curl_handle, CURLOPT_REFERER, referer.c_str());

}

//initialise la connection
void libcurl_tools::init() {

chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */

curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */
curl_handle = curl_easy_init();

}

//télécharge l'url en argument
std::string libcurl_tools::perform(std::string url , CURL
*curl_handle) {

/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

/* some servers don't like requests that are made without a user-
agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/
1.0");

/* get it! */
curl_easy_perform(curl_handle);

/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);

/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*
* You should be aware of the fact that at this point we might have
an
* allocated data block, and nothing has yet deallocated that data.
So when
* you're done with it, you should free() it as a nice application.
*/

//return chunk.memory; // les 2 ont l'air de marcher (pour le
moment...)
return std::string(chunk.memory);

}

bool libcurl_tools::close() {

if(chunk.memory)
free(chunk.memory);

return 0;

}

*********************************** test.cpp which includes the main()

#include <iostream>
#include <string>
#include <sstream>
#include "libcurl_tools.h"

//#include <stdio.h>

using namespace std;

int main()
{

libcurl_tools e();

e.init();

std::string chaine;
std:: string s = "www.yahoo.com";

chaine = e.perform(s);

e.close();

}

There are 3 compilation errors concerning the calls of init(),
perform() and close() methods, and I really don't know why it happens.
I wish you could help me guys...

Thanks
If it can help, the errors are similar to :

test.cpp:91: erreur: request for member «init" in «e", which is of
non-class type «libcurl_tools ()()"

Nov 9 '07 #3
On Nov 9, 12:34 pm, Alan Woodland <aj...@aber.ac.ukwrote:
Choi wrote:
Good morning.
I've tried to extract, using libcurl, web pages but it failed. There
is no compilation error concerning the class I wrote, but the problems
appear when I compile a main method which calls this class.
My class :

[snip]
int main()
{
libcurl_tools e();

did you mean:
libcurl_tools e;

instead?


e.init();
std::string chaine;
std:: string s = "www.yahoo.com";
chaine = e.perform(s);
e.close();
}
There are 3 compilation errors concerning the calls of init(),
perform() and close() methods, and I really don't know why it happens.
I wish you could help me guys...

Might be helpful in the future if you could post the errors too...

Alan
Thanks Alan, I modified libcurl_tools e(); -libcurl_tools e; I
didn't see that stupid mistake !!!! I also modified the class. Now it
works !!!!

Thanks a lot

Nov 9 '07 #4
On Nov 9, 12:54 pm, Choi <Evil.C...@gmail.comwrote:
On Nov 9, 11:50 am, Choi <Evil.C...@gmail.comwrote:
Good morning.
I've tried to extract, using libcurl, web pages but it failed. There
is no compilation error concerning the class I wrote, but the problems
appear when I compile a main method which calls this class.
My class :
**********************************libcurl_tools.h
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
class libcurl_tools {
CURL * curl_handle;
MemoryStruct chunk;
public:
libcurl_tools();
void init();
bool close();
void set_referrer(std::string, CURL *);
std::string perform(std::string, CURL *);
};
**********************************libcurl_tools.cp p
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <iostream// pour std::cout
#include <string // pour std::string
#include "libcurl_tools.h"
libcurl_tools::libcurl_tools() {}
static void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t
nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize +
1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
void libcurl_tools::set_referrer(std::string referer, CURL
*curl_handle ) {
curl_easy_setopt(curl_handle, CURLOPT_REFERER, referer.c_str());
}
//initialise la connection
void libcurl_tools::init() {
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
}
//télécharge l'url en argument
std::string libcurl_tools::perform(std::string url , CURL
*curl_handle) {
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-
agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/
1.0");
/* get it! */
curl_easy_perform(curl_handle);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
/*
* Now, our chunk.memory points to a memory block that is chunk..size
* bytes big and contains the remote file.
*
* Do something nice with it!
*
* You should be aware of the fact that at this point we might have
an
* allocated data block, and nothing has yet deallocated that data.
So when
* you're done with it, you should free() it as a nice application.
*/
//return chunk.memory; // les 2 ont l'air de marcher (pour le
moment...)
return std::string(chunk.memory);
}
bool libcurl_tools::close() {
if(chunk.memory)
free(chunk.memory);
return 0;
}
*********************************** test.cpp which includes the main()
#include <iostream>
#include <string>
#include <sstream>
#include "libcurl_tools.h"
//#include <stdio.h>
using namespace std;
int main()
{
libcurl_tools e();
e.init();
std::string chaine;
std:: string s = "www.yahoo.com";
chaine = e.perform(s);
e.close();
}
There are 3 compilation errors concerning the calls of init(),
perform() and close() methods, and I really don't know why it happens.
I wish you could help me guys...
Thanks

If it can help, the errors are similar to :

test.cpp:91: erreur: request for member «init" in «e", which is of
non-class type «libcurl_tools ()()"
Thanks Alan, I modified libcurl_tools e(); -libcurl_tools e; I
didn't see that stupid mistake !!!! I also modified the class. Now it
works !!!!

Thanks a lot

Nov 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael T. Peterson | last post by:
Hi, What are the differences, if any, between these two libraries? The curl website doesn't mention php_curl.dll and php websites often use libcurl and php_curl interchangeably. Thanks, ...
3
by: Yannick Turgeon | last post by:
Hello all, I've got a Visual Basic program (personal use) that downlaod and process some web pages. I'd like to rebuild it in C++ and use it under linux. Anybody can point me out where I can...
0
by: loris128 | last post by:
A port of the famous libCURL for the .net platform http://sourceforge.net/projects/libcurl-net/ Has anyone used this? Is it good? >From a search in the newsgroops it seems like noone has...
0
by: Yandos | last post by:
Hello all, I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might help me here... I need to feed form like the following using libcurl: <form...
1
by: Sylvain/11XX | last post by:
Dear all, In order to retrieve and parse a XML document over internet (through http) I use libcurl and expat in my C program. For some long item I have 2 callback, that's ok for the moment....
1
by: Steven Nagy | last post by:
Howdy folks. So I have an App from 1.1 that I converted to 2.0. I now want to refactor a bit. One of the things I want to do is create master page for the header, footer and menu that exists on...
2
by: borucik | last post by:
I am trying to use libcurl with Visual C++ 2005 Express Edition. I downloaded the file from here: http://curl.haxx.se/dlwiz/?type=lib&os=Win32&flav=-&ver=2000%2FXP It is the 7.16.0 version of the...
0
by: MikeY | last post by:
I'm having trouble extaction my image from my resource file using DictionaryEnumerator & ResourceReader. String extractions are good. I've been trying to look online for exact info on this, but...
3
by: Jake | last post by:
Hi, I am trying to make an application in C which must establish a telnet session with a remote server, execute some scripts on the remote server and close down the telnet session. I have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.