1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

examples: make the C++ examples follow our code style too

At least mostly, not counting // comments.
This commit is contained in:
Daniel Stenberg 2017-01-08 16:38:54 +01:00
parent ed2fcd5428
commit a41e8592d6
2 changed files with 85 additions and 125 deletions

View File

@ -40,8 +40,8 @@
* Note:
* For the sake of simplicity, URL is hard coded to "www.google.com"
*
* This is purely a demo app, all retrieved data is simply discarded by the write
* callback.
* This is purely a demo app, all retrieved data is simply discarded by the
* write callback.
*/
@ -85,14 +85,12 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
/* cancel running timer */
timer.cancel();
if(timeout_ms > 0)
{
if(timeout_ms > 0) {
/* update timer */
timer.expires_from_now(boost::posix_time::millisec(timeout_ms));
timer.async_wait(boost::bind(&timer_cb, _1, g));
}
else
{
else {
/* call timeout function immediately */
boost::system::error_code error; /*success*/
timer_cb(error, g);
@ -104,11 +102,9 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
/* Die if we get a bad CURLMcode somewhere */
static void mcode_or_die(const char *where, CURLMcode code)
{
if(CURLM_OK != code)
{
if(CURLM_OK != code) {
const char *s;
switch(code)
{
switch(code) {
case CURLM_CALL_MULTI_PERFORM:
s = "CURLM_CALL_MULTI_PERFORM";
break;
@ -158,10 +154,8 @@ static void check_multi_info(GlobalInfo *g)
fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running);
while((msg = curl_multi_info_read(g->multi, &msgs_left)))
{
if(msg->msg == CURLMSG_DONE)
{
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
if(msg->msg == CURLMSG_DONE) {
easy = msg->easy_handle;
res = msg->data.result;
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
@ -177,43 +171,41 @@ static void check_multi_info(GlobalInfo *g)
/* Called by asio when there is an action on a socket */
static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
int action, const boost::system::error_code & error, int *fdp)
int action, const boost::system::error_code & error,
int *fdp)
{
fprintf(MSG_OUT, "\nevent_cb: action=%d", action);
/* make sure the event matches what are wanted */
if(*fdp == action || *fdp == CURL_POLL_INOUT)
{
if(*fdp == action || *fdp == CURL_POLL_INOUT) {
curl_socket_t s = tcp_socket->native_handle();
CURLMcode rc;
if(error)
action = CURL_CSELECT_ERR;
rc = curl_multi_socket_action(g->multi, s, action, &g->still_running);
mcode_or_die("event_cb: curl_multi_socket_action", rc);
check_multi_info(g);
if(g->still_running <= 0)
{
if(g->still_running <= 0) {
fprintf(MSG_OUT, "\nlast transfer done, kill timeout");
timer.cancel();
}
/* keep on watching.
* the socket may have been closed and/or fdp may have been changed
* in curl_multi_socket_action(), so check them both */
if(!error && socket_map.find(s) != socket_map.end() &&
(*fdp == action || *fdp == CURL_POLL_INOUT))
{
if(action == CURL_POLL_IN)
{
(*fdp == action || *fdp == CURL_POLL_INOUT)) {
if(action == CURL_POLL_IN) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, action, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
action, _1, fdp));
}
if(action == CURL_POLL_OUT)
{
if(action == CURL_POLL_OUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, action, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
action, _1, fdp));
}
}
}
@ -222,12 +214,12 @@ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
/* Called by asio when our timeout expires */
static void timer_cb(const boost::system::error_code & error, GlobalInfo *g)
{
if(!error)
{
if(!error) {
fprintf(MSG_OUT, "\ntimer_cb: ");
CURLMcode rc;
rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running);
rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
&g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
check_multi_info(g);
@ -239,22 +231,21 @@ static void remsock(int *f, GlobalInfo *g)
{
fprintf(MSG_OUT, "\nremsock: ");
if(f)
{
if(f) {
free(f);
}
}
static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, int oldact, GlobalInfo*g)
static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact,
GlobalInfo *g)
{
fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(s);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
socket_map.find(s);
if(it == socket_map.end())
{
if(it == socket_map.end()) {
fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s);
return;
}
@ -262,36 +253,33 @@ static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, int oldact, Glob
*fdp = act;
if(act == CURL_POLL_IN)
{
if(act == CURL_POLL_IN) {
fprintf(MSG_OUT, "\nwatching for socket to become readable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT)
{
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_IN, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_IN, _1, fdp));
}
}
else if (act == CURL_POLL_OUT)
{
else if(act == CURL_POLL_OUT) {
fprintf(MSG_OUT, "\nwatching for socket to become writable");
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT)
{
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_OUT, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_OUT, _1, fdp));
}
}
else if(act == CURL_POLL_INOUT)
{
else if(act == CURL_POLL_INOUT) {
fprintf(MSG_OUT, "\nwatching for socket to become readable & writable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT)
{
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_IN, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_IN, _1, fdp));
}
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT)
{
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket, CURL_POLL_OUT, _1, fdp));
boost::bind(&event_cb, g, tcp_socket,
CURL_POLL_OUT, _1, fdp));
}
}
}
@ -317,20 +305,16 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
fprintf(MSG_OUT,
"\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
if(what == CURL_POLL_REMOVE)
{
if(what == CURL_POLL_REMOVE) {
fprintf(MSG_OUT, "\n");
remsock(actionp, g);
}
else
{
if(!actionp)
{
else {
if(!actionp) {
fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]);
addsock(s, e, what, g);
}
else
{
else {
fprintf(MSG_OUT,
"\nChanging action from %s to %s",
whatstr[*actionp], whatstr[what]);
@ -344,9 +328,8 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
/* CURLOPT_WRITEFUNCTION */
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t written = size * nmemb;
char* pBuffer = (char *) malloc(written + 1);
char *pBuffer = (char *)malloc(written + 1);
strncpy(pBuffer, (const char *)ptr, written);
pBuffer[written] = '\0';
@ -382,28 +365,28 @@ static curl_socket_t opensocket(void *clientp, curlsocktype purpose,
curl_socket_t sockfd = CURL_SOCKET_BAD;
/* restrict to IPv4 */
if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET)
{
if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) {
/* create a tcp socket object */
boost::asio::ip::tcp::socket *tcp_socket = new boost::asio::ip::tcp::socket(io_service);
boost::asio::ip::tcp::socket *tcp_socket =
new boost::asio::ip::tcp::socket(io_service);
/* open it and get the native handle*/
boost::system::error_code ec;
tcp_socket->open(boost::asio::ip::tcp::v4(), ec);
if(ec)
{
if(ec) {
/* An error occurred */
std::cout << std::endl << "Couldn't open socket [" << ec << "][" << ec.message() << "]";
std::cout << std::endl << "Couldn't open socket [" << ec << "][" <<
ec.message() << "]";
fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error");
}
else
{
else {
sockfd = tcp_socket->native_handle();
fprintf(MSG_OUT, "\nOpened socket %d", sockfd);
/* save it for monitoring */
socket_map.insert(std::pair<curl_socket_t, boost::asio::ip::tcp::socket *>(sockfd, tcp_socket));
socket_map.insert(std::pair<curl_socket_t,
boost::asio::ip::tcp::socket *>(sockfd, tcp_socket));
}
}
@ -415,10 +398,10 @@ static int close_socket(void *clientp, curl_socket_t item)
{
fprintf(MSG_OUT, "\nclose_socket : %d", item);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(item);
std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it =
socket_map.find(item);
if(it != socket_map.end())
{
if(it != socket_map.end()) {
delete it->second;
socket_map.erase(it);
}
@ -435,10 +418,8 @@ static void new_conn(char *url, GlobalInfo *g)
conn = (ConnInfo *) calloc(1, sizeof(ConnInfo));
conn->easy = curl_easy_init();
if(!conn->easy)
{
if(!conn->easy) {
fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!");
exit(2);
}

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -22,14 +22,14 @@
/* <DESC>
* Get a web page, extract the title with libxml.
* </DESC>
*/
// Written by Lars Nilsson
//
// GNU C++ compile command line suggestion (edit paths accordingly):
//
// g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
// -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
Written by Lars Nilsson
GNU C++ compile command line suggestion (edit paths accordingly):
g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
-o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -72,7 +72,7 @@ static std::string buffer;
static int writer(char *data, size_t size, size_t nmemb,
std::string *writerData)
{
if (writerData == NULL)
if(writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
@ -90,50 +90,38 @@ static bool init(CURL *&conn, char *url)
conn = curl_easy_init();
if (conn == NULL)
{
if(conn == NULL) {
fprintf(stderr, "Failed to create CURL connection\n");
exit(EXIT_FAILURE);
}
code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to set error buffer [%d]\n", code);
return false;
}
code = curl_easy_setopt(conn, CURLOPT_URL, url);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
return false;
}
code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
return false;
}
code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
return false;
}
code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
return false;
}
@ -150,8 +138,7 @@ static void StartElement(void *voidContext,
{
Context *context = (Context *)voidContext;
if (COMPARE((char *)name, "TITLE"))
{
if(COMPARE((char *)name, "TITLE")) {
context->title = "";
context->addTitle = true;
}
@ -167,7 +154,7 @@ static void EndElement(void *voidContext,
{
Context *context = (Context *)voidContext;
if (COMPARE((char *)name, "TITLE"))
if(COMPARE((char *)name, "TITLE"))
context->addTitle = false;
}
@ -179,7 +166,7 @@ static void handleCharacters(Context *context,
const xmlChar *chars,
int length)
{
if (context->addTitle)
if(context->addTitle)
context->title.append((char *)chars, length);
}
@ -273,10 +260,8 @@ int main(int argc, char *argv[])
// Ensure one argument is given
if (argc != 2)
{
if(argc != 2) {
fprintf(stderr, "Usage: %s <url>\n", argv[0]);
exit(EXIT_FAILURE);
}
@ -284,10 +269,8 @@ int main(int argc, char *argv[])
// Initialize CURL connection
if (!init(conn, argv[1]))
{
if(!init(conn, argv[1])) {
fprintf(stderr, "Connection initializion failed\n");
exit(EXIT_FAILURE);
}
@ -296,19 +279,15 @@ int main(int argc, char *argv[])
code = curl_easy_perform(conn);
curl_easy_cleanup(conn);
if (code != CURLE_OK)
{
if(code != CURLE_OK) {
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
exit(EXIT_FAILURE);
}
// Parse the (assumed) HTML code
parseHtml(buffer, title);
// Display the extracted title
printf("Title: %s\n", title.c_str());
return EXIT_SUCCESS;