libtld 2.0.14
A library to determine the Top-Level Domain name of any Internet URI.
tld_object.cpp
Go to the documentation of this file.
1/* TLD library -- 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
31#include "libtld/tld.h"
32#include <stdio.h>
33
56tld_object::tld_object(char const * domain_name)
57{
59}
60
74tld_object::tld_object(std::string const & domain_name)
75{
77}
78
93void tld_object::set_domain(char const * domain_name)
94{
95 set_domain(std::string(domain_name == nullptr ? "" : domain_name));
96}
97
111void tld_object::set_domain(std::string const & domain_name)
112{
113 // tld() supports empty strings and initializes f_info appropriately
115 f_result = tld(f_domain.c_str(), &f_info);
116 // TBD -- should we clear f_domain on an invalid result?
117}
118
137{
138 return f_result;
139}
140
160{
161 return f_info.f_status;
162}
163
179
188std::string tld_object::domain() const
189{
190 return f_domain;
191}
192
206std::string tld_object::sub_domains() const
207{
208 if(!is_valid())
209 {
210 throw invalid_domain();
211 }
212 char const * domain_name(f_info.f_tld);
213 char const * start(f_domain.c_str());
214 for(; domain_name > start && domain_name[-1] != '.'; --domain_name);
215 if(domain_name == start)
216 {
217 return std::string();
218 }
219 // no not return the period
220 return std::string(start, domain_name - start - 1);
221}
222
241std::string tld_object::full_domain() const
242{
243 if(!is_valid())
244 {
245 throw invalid_domain();
246 }
247 char const * domain_name(f_info.f_tld);
248 for(char const * start(f_domain.c_str()); domain_name > start && domain_name[-1] != '.'; --domain_name);
249 return domain_name;
250}
251
266std::string tld_object::domain_only() const
267{
268 if(!is_valid())
269 {
270 throw invalid_domain();
271 }
272 char const * end(f_info.f_tld);
273 char const * domain_name(end);
274 for(char const * start(f_domain.c_str()); domain_name > start && domain_name[-1] != '.'; --domain_name);
275 return std::string(domain_name, end - domain_name);
276}
277
295std::string tld_object::tld_only() const
296{
297 if(!is_valid())
298 {
299 throw invalid_domain();
300 }
301 return f_info.f_tld;
302}
303
330
346std::string tld_object::country() const
347{
348 return f_info.f_country;
349}
350
351
412/* vim: ts=4 sw=4 et
413 */
std::string domain_only() const
Retrieve the domain name only.
tld_object(const char *domain_name=NULL)
Initialize a tld object with the specified domain.
std::string sub_domains() const
Retrieve the sub-domains of the URI.
std::string country() const
The name of the country linked to that TLD.
std::string domain() const
Retrieve the domain name of this TLD object.
tld_result f_result
The result of the tld() function call.
Definition tld.h:218
tld_status status() const
Retrieve the current status of the TLD.
tld_result result() const
Check the result of the tld() command.
std::string tld_only() const
Return the TLD of the URI.
void set_domain(const char *domain_name)
Change the domain of a tld object with the newly specified domain.
std::string full_domain() const
Full domain name: domain and TLD.
tld_category category() const
Retrieve the category of this URI.
std::string f_domain
The domain or URI as specified in the constructor or set_domain() function.
Definition tld.h:216
bool is_valid() const
Check whether this TLD object is valid.
tld_info f_info
The information of the domain of this tld_object.
Definition tld.h:217
Exception thrown when querying for data of an invalid domain.
Definition tld.h:193
enum tld_category f_category
The category of the TLD.
Definition tld.h:103
enum tld_status f_status
The status of the TLD.
Definition tld.h:104
char f_country[48]
The country where this TLD is used.
Definition tld.h:105
const char * f_tld
Pointer to the TLD in the URI string you supplied.
Definition tld.h:106
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
tld_category
Definition tld.h:54
tld_result
Definition tld.h:92
@ TLD_RESULT_SUCCESS
Success! The TLD of the specified URI is valid.
Definition tld.h:93
tld_status
Definition tld.h:70
@ TLD_STATUS_VALID
The TLD is currently valid.
Definition tld.h:71

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.