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