libtld 2.0.14
A library to determine the Top-Level Domain name of any Internet URI.
extract_tld.cpp
1/* TLD tools -- TLD, domain name, and sub-domain extraction
2 * Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24// libtld
25//
26#include <libtld/tld_file.h>
27#include <libtld/tld.h>
28
29
30// C++
31//
32#include <fstream>
33#include <iostream>
34#include <sstream>
35
36
37// C
38//
39#include <string.h>
40#include <limits.h>
41
42
43
44int g_errcnt;
45tld_file * g_tld_file = nullptr;
46
47void load_tld_file(char * filename)
48{
49 tld_file_free(&g_tld_file);
50 tld_file_error err(tld_file_load(filename, &g_tld_file));
51 if(err != TLD_FILE_ERROR_NONE)
52 {
53 ++g_errcnt;
54 std::cerr
55 << "error: could not load TLD file \""
56 << filename
57 << "\".\n";
58 return;
59 }
60}
61
62
63bool has_tld_file()
64{
65 if(g_tld_file == nullptr)
66 {
67 ++g_errcnt;
68 std::cerr
69 << "error: not loaded TLD file, try --input before other options.\n";
70 return false;
71 }
72
73 return true;
74}
75
76
77void print_string(int index, bool newline = true)
78{
79 if(!has_tld_file())
80 {
81 return;
82 }
83
84 uint32_t l;
85 const char *str = tld_file_string(g_tld_file, index, &l);
86 std::string s(str, l);
87
88 std::cout << "tld_string[" << index << "] = \"" << s << "\".";
89 if(newline)
90 {
91 std::cout << "\n";
92 }
93}
94
95void print_tag(int index, bool one_line = false)
96{
97 if(!has_tld_file())
98 {
99 return;
100 }
101
102 const tld_tag *tag(tld_file_tag(g_tld_file, index));
103 if(tag == nullptr)
104 {
105 ++g_errcnt;
106 std::cerr
107 << "error: tag["
108 << index
109 << "] not found (index too large?).\n";
110 return;
111 }
112
113 std::cout << "tag[" << index << "].f_name = ";
114 print_string(tag->f_tag_name, !one_line);
115 if(one_line)
116 {
117 std::cout << " .f_value = ";
118 print_string(tag->f_tag_value);
119 }
120 else
121 {
122 std::cout << "tag[" << index << "].f_value = ";
123 print_string(tag->f_tag_value);
124 }
125}
126
127void print_tld(int index)
128{
129 if(!has_tld_file())
130 {
131 return;
132 }
133
134 const tld_description *tld(tld_file_description(g_tld_file, index));
135 if(tld == nullptr)
136 {
137 ++g_errcnt;
138 std::cerr
139 << "error: tld["
140 << index
141 << "] not found (index too large?).\n";
142 return;
143 }
144
145 std::cout << "tld[" << index << "].f_status = "
146 << static_cast<int>(tld->f_status)
147 << " ("
148 << tld_status_to_string(static_cast<tld_status>(tld->f_status))
149 << ")\n";
150
151 std::cout << "tld[" << index << "].f_exception_level = "
152 << static_cast<int>(tld->f_exception_level)
153 << "\n";
154
155 if(tld->f_exception_apply_to != SHRT_MAX)
156 {
157 std::cout << "tld[" << index << "].f_exception_apply_to =\n\n";
158 print_tld(tld->f_exception_apply_to);
159 std::cout << "\n";
160 }
161
162 std::cout << "tld[" << index << "].f_start_offset = "
163 << tld->f_start_offset
164 << "\n";
165
166 std::cout << "tld[" << index << "].f_end_offset = "
167 << tld->f_end_offset
168 << "\n";
169
170 std::cout << "tld[" << index << "].f_tld = ";
171 print_string(tld->f_tld);
172
173 for(uint16_t idx(0); idx < tld->f_tags_count; ++idx)
174 {
175 std::cout << "tld[" << index << "].f_tags[" << idx << "] = ";
176 print_tag(tld->f_tags + idx * 2, true);
177 }
178}
179
180
181void print_sizes()
182{
183 if(!has_tld_file())
184 {
185 return;
186 }
187
188 std::cout << "Number of ...\n"
189 << "... descriptions: " << g_tld_file->f_descriptions_count << "\n"
190 << "... tags: " << g_tld_file->f_tags_size << "\n"
191 << "... strings: " << g_tld_file->f_strings_count << "\n"
192 << "... characters: " << g_tld_file->f_strings_end - g_tld_file->f_strings << "\n"
193 ;
194}
195
196
197int main(int argc, char * argv[])
198{
199 for(int i(1); i < argc; ++i)
200 {
201 if(argv[i][0] == '-')
202 {
203 if(strcmp(argv[i], "-h") == 0
204 || strcmp(argv[i], "--help") == 0)
205 {
206 std::cout << "Usage: extract-tld <opts>\n"
207 "where <opts> is one or more of:\n"
208 " --help | -h print this help screen\n"
209 " --tag | -T <index> print tag at that index out of all the tags\n"
210 " --tld | -t <offset> retrieve that specific TLD\n"
211 " --string | -s <index> print string at that index\n"
212 " --version | -V print out the version and exit\n"
213 " --input <filename> use <filename> as the TLD file to read from\n"
214 ;
215 return 1;
216 }
217 else if(strcmp(argv[i], "-V") == 0
218 || strcmp(argv[i], "--version") == 0)
219 {
220 std::cout << LIBTLD_VERSION << std::endl;
221 return 1;
222 }
223 else if(strcmp(argv[i], "-s") == 0
224 || strcmp(argv[i], "--string") == 0)
225 {
226 ++i;
227 if(i >= argc)
228 {
229 ++g_errcnt;
230 std::cerr
231 << "error: argument missing for --string.\n";
232 }
233 else
234 {
235 print_string(atoi(argv[i]));
236 }
237 }
238 else if(strcmp(argv[i], "-T") == 0
239 || strcmp(argv[i], "--tag") == 0)
240 {
241 ++i;
242 if(i >= argc)
243 {
244 ++g_errcnt;
245 std::cerr
246 << "error: argument missing for --tag.\n";
247 }
248 else
249 {
250 print_tag(atoi(argv[i]));
251 }
252 }
253 else if(strcmp(argv[i], "-t") == 0
254 || strcmp(argv[i], "--tld") == 0)
255 {
256 ++i;
257 if(i >= argc)
258 {
259 ++g_errcnt;
260 std::cerr
261 << "error: argument missing for --tld.\n";
262 }
263 else
264 {
265 print_tld(atoi(argv[i]));
266 }
267 }
268 else if(strcmp(argv[i], "--sizes") == 0)
269 {
270 print_sizes();
271 }
272 else if(strcmp(argv[i], "--input") == 0)
273 {
274 ++i;
275 if(i >= argc)
276 {
277 ++g_errcnt;
278 std::cerr
279 << "error: argument missing for --input.\n";
280 }
281 else
282 {
283 load_tld_file(argv[i]);
284 }
285 }
286 else
287 {
288 ++g_errcnt;
289 std::cerr
290 << "error: unknown command line option \""
291 << argv[i]
292 << "\".\n";
293 }
294 }
295 else
296 {
297 ++g_errcnt;
298 std::cerr
299 << "error: unknown command line option \""
300 << argv[i]
301 << "\".\n";
302 }
303 }
304
305 return g_errcnt > 0 ? 1 : 0;
306}
307
308// vim: ts=4 sw=4 et
[internal] The description of one TLD.
Definition tld_file.h:117
The public header of the libtld library.
LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info *info)
Get information about the TLD for the specified URI.
Definition tld.cpp:1113
#define LIBTLD_VERSION
The version of the library as a string.
Definition tld.h:51
LIBTLD_EXPORT const char * tld_status_to_string(enum tld_status status)
Transform the status to a string.
Definition tld_strings.c:49
tld_status
Definition tld.h:70
Declaration of the TLD file structures.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.