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