aboutsummaryrefslogtreecommitdiff
path: root/docs/examples/htmltitle.cpp
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-01-08 16:38:54 +0100
committerDaniel Stenberg <daniel@haxx.se>2017-01-08 16:39:41 +0100
commita41e8592d6b3e58311e628c5a34bc53776b93e16 (patch)
tree8770956444ed6fc7e47cdee3ba41adcc34206618 /docs/examples/htmltitle.cpp
parented2fcd54283179dfb2fa52c6c4b08e1f4db9632d (diff)
examples: make the C++ examples follow our code style too
At least mostly, not counting // comments.
Diffstat (limited to 'docs/examples/htmltitle.cpp')
-rw-r--r--docs/examples/htmltitle.cpp63
1 files changed, 21 insertions, 42 deletions
diff --git a/docs/examples/htmltitle.cpp b/docs/examples/htmltitle.cpp
index 5e6b4a003..8148888a4 100644
--- a/docs/examples/htmltitle.cpp
+++ b/docs/examples/htmltitle.cpp
@@ -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;