htmltitle.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. // Get a web page, parse it with libxml.
  23. //
  24. // Written by Lars Nilsson
  25. //
  26. // GNU C++ compile command line suggestion (edit paths accordingly):
  27. //
  28. // g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
  29. // -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <string>
  34. #include <curl/curl.h>
  35. #include <libxml/HTMLparser.h>
  36. //
  37. // Case-insensitive string comparison
  38. //
  39. #ifdef _MSC_VER
  40. #define COMPARE(a, b) (!stricmp((a), (b)))
  41. #else
  42. #define COMPARE(a, b) (!strcasecmp((a), (b)))
  43. #endif
  44. //
  45. // libxml callback context structure
  46. //
  47. struct Context
  48. {
  49. Context(): addTitle(false) { }
  50. bool addTitle;
  51. std::string title;
  52. };
  53. //
  54. // libcurl variables for error strings and returned data
  55. static char errorBuffer[CURL_ERROR_SIZE];
  56. static std::string buffer;
  57. //
  58. // libcurl write callback function
  59. //
  60. static int writer(char *data, size_t size, size_t nmemb,
  61. std::string *writerData)
  62. {
  63. if (writerData == NULL)
  64. return 0;
  65. writerData->append(data, size*nmemb);
  66. return size * nmemb;
  67. }
  68. //
  69. // libcurl connection initialization
  70. //
  71. static bool init(CURL *&conn, char *url)
  72. {
  73. CURLcode code;
  74. conn = curl_easy_init();
  75. if (conn == NULL)
  76. {
  77. fprintf(stderr, "Failed to create CURL connection\n");
  78. exit(EXIT_FAILURE);
  79. }
  80. code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
  81. if (code != CURLE_OK)
  82. {
  83. fprintf(stderr, "Failed to set error buffer [%d]\n", code);
  84. return false;
  85. }
  86. code = curl_easy_setopt(conn, CURLOPT_URL, url);
  87. if (code != CURLE_OK)
  88. {
  89. fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
  90. return false;
  91. }
  92. code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L);
  93. if (code != CURLE_OK)
  94. {
  95. fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
  96. return false;
  97. }
  98. code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
  99. if (code != CURLE_OK)
  100. {
  101. fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
  102. return false;
  103. }
  104. code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer);
  105. if (code != CURLE_OK)
  106. {
  107. fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
  108. return false;
  109. }
  110. return true;
  111. }
  112. //
  113. // libxml start element callback function
  114. //
  115. static void StartElement(void *voidContext,
  116. const xmlChar *name,
  117. const xmlChar **attributes)
  118. {
  119. Context *context = (Context *)voidContext;
  120. if (COMPARE((char *)name, "TITLE"))
  121. {
  122. context->title = "";
  123. context->addTitle = true;
  124. }
  125. (void) attributes;
  126. }
  127. //
  128. // libxml end element callback function
  129. //
  130. static void EndElement(void *voidContext,
  131. const xmlChar *name)
  132. {
  133. Context *context = (Context *)voidContext;
  134. if (COMPARE((char *)name, "TITLE"))
  135. context->addTitle = false;
  136. }
  137. //
  138. // Text handling helper function
  139. //
  140. static void handleCharacters(Context *context,
  141. const xmlChar *chars,
  142. int length)
  143. {
  144. if (context->addTitle)
  145. context->title.append((char *)chars, length);
  146. }
  147. //
  148. // libxml PCDATA callback function
  149. //
  150. static void Characters(void *voidContext,
  151. const xmlChar *chars,
  152. int length)
  153. {
  154. Context *context = (Context *)voidContext;
  155. handleCharacters(context, chars, length);
  156. }
  157. //
  158. // libxml CDATA callback function
  159. //
  160. static void cdata(void *voidContext,
  161. const xmlChar *chars,
  162. int length)
  163. {
  164. Context *context = (Context *)voidContext;
  165. handleCharacters(context, chars, length);
  166. }
  167. //
  168. // libxml SAX callback structure
  169. //
  170. static htmlSAXHandler saxHandler =
  171. {
  172. NULL,
  173. NULL,
  174. NULL,
  175. NULL,
  176. NULL,
  177. NULL,
  178. NULL,
  179. NULL,
  180. NULL,
  181. NULL,
  182. NULL,
  183. NULL,
  184. NULL,
  185. NULL,
  186. StartElement,
  187. EndElement,
  188. NULL,
  189. Characters,
  190. NULL,
  191. NULL,
  192. NULL,
  193. NULL,
  194. NULL,
  195. NULL,
  196. NULL,
  197. cdata,
  198. NULL
  199. };
  200. //
  201. // Parse given (assumed to be) HTML text and return the title
  202. //
  203. static void parseHtml(const std::string &html,
  204. std::string &title)
  205. {
  206. htmlParserCtxtPtr ctxt;
  207. Context context;
  208. ctxt = htmlCreatePushParserCtxt(&saxHandler, &context, "", 0, "",
  209. XML_CHAR_ENCODING_NONE);
  210. htmlParseChunk(ctxt, html.c_str(), html.size(), 0);
  211. htmlParseChunk(ctxt, "", 0, 1);
  212. htmlFreeParserCtxt(ctxt);
  213. title = context.title;
  214. }
  215. int main(int argc, char *argv[])
  216. {
  217. CURL *conn = NULL;
  218. CURLcode code;
  219. std::string title;
  220. // Ensure one argument is given
  221. if (argc != 2)
  222. {
  223. fprintf(stderr, "Usage: %s <url>\n", argv[0]);
  224. exit(EXIT_FAILURE);
  225. }
  226. curl_global_init(CURL_GLOBAL_DEFAULT);
  227. // Initialize CURL connection
  228. if (!init(conn, argv[1]))
  229. {
  230. fprintf(stderr, "Connection initializion failed\n");
  231. exit(EXIT_FAILURE);
  232. }
  233. // Retrieve content for the URL
  234. code = curl_easy_perform(conn);
  235. curl_easy_cleanup(conn);
  236. if (code != CURLE_OK)
  237. {
  238. fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
  239. exit(EXIT_FAILURE);
  240. }
  241. // Parse the (assumed) HTML code
  242. parseHtml(buffer, title);
  243. // Display the extracted title
  244. printf("Title: %s\n", title.c_str());
  245. return EXIT_SUCCESS;
  246. }