Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / htmltidy.c
blob1c8c4eaec0efbe5614c054b68829cf1d0010bc7e
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: htmltidy.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * Download a document and use libtidy to parse the HTML.
11 * Written by Jeff Pohlmeyer
13 * LibTidy => http://tidy.sourceforge.net
15 * gcc -Wall -I/usr/local/include tidycurl.c -lcurl -ltidy -o tidycurl
19 #include <stdio.h>
20 #include <tidy/tidy.h>
21 #include <tidy/buffio.h>
22 #include <curl/curl.h>
24 /* curl write callback, to fill tidy's input buffer... */
25 uint write_cb(char *in, uint size, uint nmemb, TidyBuffer *out)
27 uint r;
28 r = size * nmemb;
29 tidyBufAppend( out, in, r );
30 return(r);
33 /* Traverse the document tree */
34 void dumpNode(TidyDoc doc, TidyNode tnod, int indent )
36 TidyNode child;
37 for ( child = tidyGetChild(tnod); child; child = tidyGetNext(child) )
39 ctmbstr name = tidyNodeGetName( child );
40 if ( name )
42 /* if it has a name, then it's an HTML tag ... */
43 TidyAttr attr;
44 printf( "%*.*s%s ", indent, indent, "<", name);
45 /* walk the attribute list */
46 for ( attr=tidyAttrFirst(child); attr; attr=tidyAttrNext(attr) ) {
47 printf(tidyAttrName(attr));
48 tidyAttrValue(attr)?printf("=\"%s\" ",
49 tidyAttrValue(attr)):printf(" ");
51 printf( ">\n");
53 else {
54 /* if it doesn't have a name, then it's probably text, cdata, etc... */
55 TidyBuffer buf;
56 tidyBufInit(&buf);
57 tidyNodeGetText(doc, child, &buf);
58 printf("%*.*s\n", indent, indent, buf.bp?(char *)buf.bp:"");
59 tidyBufFree(&buf);
61 dumpNode( doc, child, indent + 4 ); /* recursive */
66 int main(int argc, char **argv )
68 CURL *curl;
69 char curl_errbuf[CURL_ERROR_SIZE];
70 TidyDoc tdoc;
71 TidyBuffer docbuf = {0};
72 TidyBuffer tidy_errbuf = {0};
73 int err;
74 if ( argc == 2) {
75 curl = curl_easy_init();
76 curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
77 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
78 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
79 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
80 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
82 tdoc = tidyCreate();
83 tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */
84 tidyOptSetInt(tdoc, TidyWrapLen, 4096);
85 tidySetErrorBuffer( tdoc, &tidy_errbuf );
86 tidyBufInit(&docbuf);
88 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf);
89 err=curl_easy_perform(curl);
90 if ( !err ) {
91 err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */
92 if ( err >= 0 ) {
93 err = tidyCleanAndRepair(tdoc); /* fix any problems */
94 if ( err >= 0 ) {
95 err = tidyRunDiagnostics(tdoc); /* load tidy error buffer */
96 if ( err >= 0 ) {
97 dumpNode( tdoc, tidyGetRoot(tdoc), 0 ); /* walk the tree */
98 fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */
103 else
104 fprintf(stderr, "%s\n", curl_errbuf);
106 /* clean-up */
107 curl_easy_cleanup(curl);
108 tidyBufFree(&docbuf);
109 tidyBufFree(&tidy_errbuf);
110 tidyRelease(tdoc);
111 return(err);
114 else
115 printf( "usage: %s <url>\n", argv[0] );
117 return(0);