comparison mupdf-source/thirdparty/gumbo-parser/examples/find_links.cc @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: jdtang@google.com (Jonathan Tang)
16 //
17 // Finds the URLs of all links in the page.
18
19 #include <stdlib.h>
20
21 #include <fstream>
22 #include <iostream>
23 #include <string>
24
25 #include "gumbo.h"
26
27 static void search_for_links(GumboNode* node) {
28 if (node->type != GUMBO_NODE_ELEMENT) {
29 return;
30 }
31 GumboAttribute* href;
32 if (node->v.element.tag == GUMBO_TAG_A &&
33 (href = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
34 std::cout << href->value << std::endl;
35 }
36
37 GumboVector* children = &node->v.element.children;
38 for (unsigned int i = 0; i < children->length; ++i) {
39 search_for_links(static_cast<GumboNode*>(children->data[i]));
40 }
41 }
42
43 int main(int argc, char** argv) {
44 if (argc != 2) {
45 std::cout << "Usage: find_links <html filename>.\n";
46 exit(EXIT_FAILURE);
47 }
48 const char* filename = argv[1];
49
50 std::ifstream in(filename, std::ios::in | std::ios::binary);
51 if (!in) {
52 std::cout << "File " << filename << " not found!\n";
53 exit(EXIT_FAILURE);
54 }
55
56 std::string contents;
57 in.seekg(0, std::ios::end);
58 contents.resize(in.tellg());
59 in.seekg(0, std::ios::beg);
60 in.read(&contents[0], contents.size());
61 in.close();
62
63 GumboOutput* output = gumbo_parse(contents.c_str());
64 search_for_links(output->root);
65 gumbo_destroy_output(&kGumboDefaultOptions, output);
66 }