htmltitle.cc 5.5 KB

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