diff --git a/Makefile.am b/Makefile.am index 6475978..fe74366 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,6 +23,7 @@ pkginclude_HEADERS = \ include/igloo/interface.h \ include/igloo/io.h \ include/igloo/stdio.h \ + include/igloo/filter.h \ include/igloo/buffer.h \ include/igloo/list.h \ include/igloo/reportxml.h @@ -33,6 +34,7 @@ libigloo_la_SOURCES = \ src/ro.c \ src/io.c \ src/stdio.c \ + src/filter.c \ src/buffer.c \ src/list.c \ src/reportxml.c diff --git a/include/igloo/filter.h b/include/igloo/filter.h new file mode 100644 index 0000000..56c2542 --- /dev/null +++ b/include/igloo/filter.h @@ -0,0 +1,87 @@ +/* Copyright (C) 2019 Philipp "ph3-der-loewe" Schafft + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef _LIBIGLOO__FILTER_H_ +#define _LIBIGLOO__FILTER_H_ +/** + * @file + * Put a good description of this file here + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ro.h" +#include "interface.h" + +/* About thread safety: + * This set of functions is thread safe. + */ + +igloo_RO_FORWARD_TYPE(igloo_filter_t); + +/* Result of a test. */ +typedef enum { + /* The test resulted in an error condition. */ + igloo_FILTER_RESULT_ERROR = -1, + /* The object did not pass the test and should not be processed. */ + igloo_FILTER_RESULT_DROP = 0, + /* The object passed the test and should be processed. */ + igloo_FILTER_RESULT_PASS = 1 +} igloo_filter_result_t; + +/* Interface description */ +typedef struct { + igloo_interface_base_ifdesc_t __base; + + /* Perform the actual test on the object. + * This must implement thread-safety itself if necessary. + */ + igloo_filter_result_t (*test)(igloo_INTERFACE_BASIC_ARGS, igloo_ro_t object); +} igloo_filter_ifdesc_t; + +/* This creates a new filter from a interface description and state. + * Parameters: + * ifdesc + * The interface description to use. + * backend_object + * A object used by the backend or igloo_RO_NULL. + * backend_userdata + * A userdata pointer used by the backend or NULL. + * name, associated + * See refobject_new(). + */ +igloo_filter_t * igloo_filter_new(const igloo_filter_ifdesc_t *ifdesc, igloo_ro_t backend_object, void *backend_userdata, const char *name, igloo_ro_t associated); + +/* This tests a object according to the filter. + * Parameters: + * filter + * The filter to use. + * object + * The object to test. + * Returns: + * Result of the test (error, drop, pass). + */ +igloo_filter_result_t igloo_filter_test(igloo_filter_t *filter, igloo_ro_t object); + +#ifdef __cplusplus +} +#endif + +#endif /* ! _LIBIGLOO__FILTER_H_ */ diff --git a/include/igloo/types.h b/include/igloo/types.h index 5a1e7c1..2e487c7 100644 --- a/include/igloo/types.h +++ b/include/igloo/types.h @@ -33,6 +33,7 @@ extern "C" { #include "typedef.h" typedef struct igloo_io_tag igloo_io_t; +typedef struct igloo_filter_tag igloo_filter_t; typedef struct igloo_buffer_tag igloo_buffer_t; typedef struct igloo_list_tag igloo_list_t; @@ -52,6 +53,7 @@ typedef union __attribute__ ((__transparent_union__)) { /* Those are libigloo's own types */ igloo_RO_TYPE(igloo_ro_base_t) igloo_RO_TYPE(igloo_io_t) + igloo_RO_TYPE(igloo_filter_t) igloo_RO_TYPE(igloo_buffer_t) igloo_RO_TYPE(igloo_list_t) igloo_RO_TYPE(igloo_reportxml_t) diff --git a/src/filter.c b/src/filter.c new file mode 100644 index 0000000..0a4e923 --- /dev/null +++ b/src/filter.c @@ -0,0 +1,39 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2019, Philipp "ph3-der-loewe" Schafft , + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include "private.h" + +struct igloo_filter_tag { + igloo_interface_base(filter) +}; + +igloo_RO_PUBLIC_TYPE(igloo_filter_t, + igloo_RO_TYPEDECL_FREE(igloo_interface_base_free) + ); + +igloo_filter_t * igloo_filter_new(const igloo_filter_ifdesc_t *ifdesc, igloo_ro_t backend_object, void *backend_userdata, const char *name, igloo_ro_t associated) +{ + return igloo_interface_base_new(igloo_filter_t, ifdesc, backend_object, backend_userdata, name, associated); +} + +igloo_filter_result_t igloo_filter_test(igloo_filter_t *filter, igloo_ro_t object) +{ + if (!filter) + return igloo_FILTER_RESULT_ERROR; + + if (filter->ifdesc->test) + return filter->ifdesc->test(igloo_INTERFACE_BASIC_CALL(filter), object); + + return igloo_FILTER_RESULT_ERROR; +}