avoid a printf in the library

In general, we don't want libraries to send data to the standard file
descriptors.  There are more that need fixing.

Note: this introduces a new API (psl_suffix_count() and
psl_suffix_exception_count) to enable the same sort of output from the
test.  But this new API seems to imply the internal structure of the
public suffix list.

Do we want to expose this API?  There could be some other PSL
mechanism (e.g. DBOUND) that doesn't have these counts, and a drop-in
replacement would not know what to return here.
This commit is contained in:
Daniel Kahn Gillmor 2014-03-21 14:26:55 -04:00
parent cdeea860f7
commit 2d99b964ff
3 changed files with 24 additions and 2 deletions

View File

@ -55,6 +55,14 @@ psl_ctx_t *
int int
psl_is_tld(const psl_ctx_t *psl, const char *domain); psl_is_tld(const psl_ctx_t *psl, const char *domain);
/* does not include exceptions */
int
psl_suffix_count(const psl_ctx_t *psl);
/* just counts exceptions */
int
psl_suffix_exception_count(const psl_ctx_t *psl);
PSL_END_DECLS PSL_END_DECLS
#endif /* _LIBPSL_LIBPSL_H */ #endif /* _LIBPSL_LIBPSL_H */

View File

@ -312,14 +312,25 @@ psl_ctx_t *psl_load_file(const char *fname)
_vector_sort(psl->suffix_exceptions); _vector_sort(psl->suffix_exceptions);
_vector_sort(psl->suffixes); _vector_sort(psl->suffixes);
printf("loaded %d (%d/%d) suffixes\n", nsuffixes, psl->suffixes->cur, psl->suffix_exceptions->cur);
} else } else
fprintf(stderr, _("Failed to open PSL file '%s'\n"), fname); fprintf(stderr, _("Failed to open PSL file '%s'\n"), fname);
return psl; return psl;
} }
/* does not include exceptions */
int psl_suffix_count(const psl_ctx_t *psl)
{
return psl->suffixes->cur;
}
/* just counts exceptions */
int psl_suffix_exception_count(const psl_ctx_t *psl)
{
return psl->suffix_exceptions->cur;
}
void psl_free(psl_ctx_t **psl) void psl_free(psl_ctx_t **psl)
{ {
if (psl && *psl) { if (psl && *psl) {

View File

@ -64,6 +64,9 @@ static void test_psl(void)
psl = psl_load_file(DATADIR "/effective_tld_names.dat"); psl = psl_load_file(DATADIR "/effective_tld_names.dat");
printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
for (it = 0; it < countof(test_data); it++) { for (it = 0; it < countof(test_data); it++) {
const struct test_data *t = &test_data[it]; const struct test_data *t = &test_data[it];
int result = psl_is_tld(psl, t->domain); int result = psl_is_tld(psl, t->domain);