new function psl_load_fp()

This commit is contained in:
Tim Ruehsen 2014-03-22 14:28:55 +01:00
parent de7d394223
commit a707b267c9
2 changed files with 45 additions and 34 deletions

View File

@ -27,6 +27,8 @@
#ifndef _LIBPSL_LIBPSL_H #ifndef _LIBPSL_LIBPSL_H
#define _LIBPSL_LIBPSL_H #define _LIBPSL_LIBPSL_H
#include <stdio.h>
// Let C++ include C headers // Let C++ include C headers
#ifdef __cplusplus #ifdef __cplusplus
# define PSL_BEGIN_DECLS extern "C" { # define PSL_BEGIN_DECLS extern "C" {
@ -52,6 +54,8 @@ void
psl_free(psl_ctx_t **psl); psl_free(psl_ctx_t **psl);
psl_ctx_t * psl_ctx_t *
psl_load_file(const char *fname); psl_load_file(const char *fname);
psl_ctx_t *
psl_load_fp(FILE *fp);
int int
psl_is_public(const psl_ctx_t *psl, const char *domain); psl_is_public(const psl_ctx_t *psl, const char *domain);

View File

@ -265,17 +265,31 @@ int psl_is_public(const psl_ctx_t *psl, const char *domain)
} }
psl_ctx_t *psl_load_file(const char *fname) psl_ctx_t *psl_load_file(const char *fname)
{
FILE *fp;
psl_ctx_t *psl = NULL;
if ((fp = fopen(fname, "r"))) {
psl = psl_load_fp(fp);
fclose(fp);
}
return psl;
}
psl_ctx_t *psl_load_fp(FILE *fp)
{ {
psl_ctx_t *psl; psl_ctx_t *psl;
_psl_entry_t suffix, *suffixp; _psl_entry_t suffix, *suffixp;
FILE *fp;
int nsuffixes = 0; int nsuffixes = 0;
char buf[256], *linep, *p; char buf[256], *linep, *p;
if (!fp)
return NULL;
if (!(psl = calloc(1, sizeof(psl_ctx_t)))) if (!(psl = calloc(1, sizeof(psl_ctx_t))))
return NULL; return NULL;
if ((fp = fopen(fname, "r"))) {
// as of 02.11.2012, the list at http://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions. // as of 02.11.2012, the list at http://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions.
// as of 19.02.2014, the list at http://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions. // as of 19.02.2014, the list at http://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions.
psl->suffixes = _vector_alloc(8*1024, _suffix_compare); psl->suffixes = _vector_alloc(8*1024, _suffix_compare);
@ -307,16 +321,9 @@ psl_ctx_t *psl_load_file(const char *fname)
nsuffixes++;; nsuffixes++;;
} }
fclose(fp);
_vector_sort(psl->suffix_exceptions); _vector_sort(psl->suffix_exceptions);
_vector_sort(psl->suffixes); _vector_sort(psl->suffixes);
} else {
free(psl);
return NULL;
}
return psl; return psl;
} }