libtld 1.2.0

example.c

Go to the documentation of this file.
00001 /* TLD library -- TLD example
00002  * Copyright (C) 2011  Made to Order Software Corp.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 #include "tld.h"
00020 #include <stdio.h>
00021 
00022 
00023 int main(int argc, char *argv[])
00024 {
00025     char *uri = "www.example.co.uk";
00026     struct tld_info info;
00027     enum tld_result r;
00028 
00029     r = tld(uri, &info);
00030     if(r == TLD_RESULT_SUCCESS) {
00031         const char *tld = info.f_tld;
00032         const char *s = uri + info.f_offset - 1;
00033         while(s > uri) {
00034             if(*s == '.') {
00035                 ++s;
00036                 break;
00037             }
00038             --s;
00039         }
00040         // here uri points to your sub-domains, the length is "s - uri"
00041         // if uri == s then there are no sub-domains
00042         // s points to the domain name, the length is "info.f_tld - s"
00043         // and info.f_tld points to the TLD
00044         //
00045         // When TLD_RESULT_SUCCESS is returned the domain cannot be an
00046         // empty string; also the TLD cannot be empty, however, there
00047         // may be no sub-domains.
00048         printf("Sub-domain(s): \"%.*s\"\n", (int)(s - uri), uri);
00049         printf("Domain: \"%.*s\"\n", (int)(info.f_tld - s), s);
00050         printf("TLD: \"%s\"\n", info.f_tld);
00051     }
00052 }
00053 
00054 // vim: ts=4 sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

This document is part of the libtld Project.

Copyright by Made to Order Software Corp.