Line data Source code
1 : /* TLD library -- TLD, domain name, and sub-domain extraction
2 : * Copyright (c) 2011-2022 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 : /** \file
25 : * \brief Implementation of some functions converting numbers to strings.
26 : *
27 : * This file includes functions converting numbers to strings.
28 : */
29 :
30 : // self
31 : //
32 : #include "libtld/tld.h"
33 :
34 :
35 : // C lib
36 : //
37 : #include <string.h>
38 :
39 :
40 : /** \brief Transform the status to a string.
41 : *
42 : * The status returned in a tld_info can be converted to a string using
43 : * this function. This is useful to print out an error message.
44 : *
45 : * \param[in] status The status to convert to a string.
46 : *
47 : * \return A string representing the input tld_status.
48 : */
49 31392 : const char * tld_status_to_string(enum tld_status status)
50 : {
51 31392 : switch(status)
52 : {
53 29751 : case TLD_STATUS_VALID:
54 29751 : return "valid";
55 :
56 36 : case TLD_STATUS_PROPOSED:
57 36 : return "proposed";
58 :
59 558 : case TLD_STATUS_DEPRECATED:
60 558 : return "deprecated";
61 :
62 909 : case TLD_STATUS_UNUSED:
63 909 : return "unused";
64 :
65 48 : case TLD_STATUS_RESERVED:
66 48 : return "reserved";
67 :
68 24 : case TLD_STATUS_INFRASTRUCTURE:
69 24 : return "infrastructure";
70 :
71 3 : case TLD_STATUS_EXAMPLE:
72 3 : return "example";
73 :
74 0 : case TLD_STATUS_UNDEFINED:
75 0 : return "undefined";
76 :
77 63 : case TLD_STATUS_EXCEPTION:
78 63 : return "exception";
79 :
80 : }
81 :
82 0 : return "unknown";
83 : }
84 :
85 :
86 : /** \brief This is for backward compatibility.
87 : *
88 : * Many times, a simple category is not useful because one TLD may actually
89 : * be part of multiple groups (i.e. a groups, a country, a language, an
90 : * entrepreneurial TLD can very well exist!)
91 : *
92 : * The idea is to be backward compatible for anyone who was using the old
93 : * category value. This function will convert the specified \p word in a
94 : * category. The word is expected to be a non null terminated string,
95 : * hence the parameter \p n to specify its length.
96 : *
97 : * \param[in] word The word to convert.
98 : * \param[in] n The exact number of characters in the word.
99 : *
100 : * \return The corresponding TLD_CATEGORY_... or TLD_CATEGORY_UNDEFINED if
101 : * the word could not be converted.
102 : */
103 62397 : enum tld_category tld_word_to_category(const char *word, int n)
104 : {
105 62397 : char buf[32];
106 :
107 62397 : if(word != NULL
108 62397 : && (size_t) n < sizeof(buf))
109 : {
110 : // force all lowercase
111 : //
112 633957 : for(int idx = 0; idx < n; ++idx)
113 : {
114 571560 : if(word[idx] >= 'A' && word[idx] <= 'Z')
115 : {
116 0 : buf[idx] = word[idx] | 0x20;
117 : }
118 : else
119 : {
120 571560 : buf[idx] = word[idx];
121 : }
122 : }
123 62397 : buf[n] = '\0';
124 :
125 62397 : switch(buf[0])
126 : {
127 3764 : case 'b':
128 3764 : if(strcmp(buf, "brand") == 0)
129 : {
130 3764 : return TLD_CATEGORY_BRAND;
131 : }
132 0 : break;
133 :
134 38338 : case 'c':
135 38338 : if(strcmp(buf, "country") == 0)
136 : {
137 38338 : return TLD_CATEGORY_COUNTRY;
138 : }
139 0 : break;
140 :
141 11820 : case 'e':
142 11820 : if(strcmp(buf, "entrepreneurial") == 0)
143 : {
144 11820 : return TLD_CATEGORY_ENTREPRENEURIAL;
145 : }
146 0 : break;
147 :
148 7777 : case 'i':
149 7777 : if(strcmp(buf, "international") == 0)
150 : {
151 7777 : return TLD_CATEGORY_INTERNATIONAL;
152 : }
153 0 : break;
154 :
155 27 : case 'g':
156 27 : if(strcmp(buf, "group") == 0)
157 : {
158 27 : return TLD_CATEGORY_GROUP;
159 : }
160 0 : break;
161 :
162 46 : case 'l':
163 46 : if(strcmp(buf, "language") == 0)
164 : {
165 46 : return TLD_CATEGORY_LANGUAGE;
166 : }
167 0 : if(strcmp(buf, "location") == 0)
168 : {
169 0 : return TLD_CATEGORY_LOCATION;
170 : }
171 0 : break;
172 :
173 223 : case 'p':
174 223 : if(strcmp(buf, "professionals") == 0)
175 : {
176 223 : return TLD_CATEGORY_PROFESSIONALS;
177 : }
178 0 : break;
179 :
180 349 : case 'r':
181 349 : if(strcmp(buf, "region") == 0)
182 : {
183 349 : return TLD_CATEGORY_REGION;
184 : }
185 0 : break;
186 :
187 53 : case 't':
188 53 : if(strcmp(buf, "technical") == 0)
189 : {
190 53 : return TLD_CATEGORY_TECHNICAL;
191 : }
192 0 : break;
193 :
194 : }
195 : }
196 :
197 0 : return TLD_CATEGORY_UNDEFINED;
198 : }
199 :
200 :
201 : /* vim: ts=4 sw=4 et
202 : */
|