From 9ca74e372d470afecbeff8768f4fa282a1fa00f3 Mon Sep 17 00:00:00 2001 From: Ondrej Holy Date: Tue, 6 Mar 2012 20:43:27 +0100 Subject: [PATCH] Introduce archive_write_add_fitler() - a convenience function to set the filter based on the code. --- Makefile.am | 1 + libarchive/CMakeLists.txt | 1 + libarchive/archive.h | 2 + libarchive/archive_write_add_filter.c | 66 +++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 libarchive/archive_write_add_filter.c diff --git a/Makefile.am b/Makefile.am index 2a58bae6c..b6e85b3bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -169,6 +169,7 @@ libarchive_la_SOURCES= \ libarchive/archive_write_open_filename.c \ libarchive/archive_write_open_memory.c \ libarchive/archive_write_private.h \ + libarchive/archive_write_add_filter.c \ libarchive/archive_write_add_filter_bzip2.c \ libarchive/archive_write_add_filter_compress.c \ libarchive/archive_write_add_filter_gzip.c \ diff --git a/libarchive/CMakeLists.txt b/libarchive/CMakeLists.txt index a506908ee..73d9baf01 100644 --- a/libarchive/CMakeLists.txt +++ b/libarchive/CMakeLists.txt @@ -93,6 +93,7 @@ SET(libarchive_SOURCES archive_write_open_file.c archive_write_open_filename.c archive_write_open_memory.c + archive_write_add_filter.c archive_write_add_filter_bzip2.c archive_write_add_filter_compress.c archive_write_add_filter_gzip.c diff --git a/libarchive/archive.h b/libarchive/archive.h index bf2875f0d..e28037b62 100644 --- a/libarchive/archive.h +++ b/libarchive/archive.h @@ -558,6 +558,8 @@ __LA_DECL int archive_write_set_compression_program(struct archive *, __LA_DECL int archive_write_set_compression_xz(struct archive *); #endif +/* A convenience function to set the filter based on the code. */ +__LA_DECL int archive_write_add_filter(struct archive *, int filter_code); __LA_DECL int archive_write_add_filter_bzip2(struct archive *); __LA_DECL int archive_write_add_filter_compress(struct archive *); __LA_DECL int archive_write_add_filter_gzip(struct archive *); diff --git a/libarchive/archive_write_add_filter.c b/libarchive/archive_write_add_filter.c new file mode 100644 index 000000000..915290aaa --- /dev/null +++ b/libarchive/archive_write_add_filter.c @@ -0,0 +1,66 @@ +/*- + * Copyright (c) 2012 Ondrej Holy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "archive_platform.h" +__FBSDID("$FreeBSD$"); + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "archive.h" +#include "archive_private.h" + +/* A table that maps filter codes to functions. */ +static +struct { int code; int (*setter)(struct archive *); } codes[] = +{ + { ARCHIVE_FILTER_NONE, archive_write_add_filter_none }, + { ARCHIVE_FILTER_GZIP, archive_write_add_filter_gzip }, + { ARCHIVE_FILTER_BZIP2, archive_write_add_filter_bzip2 }, + { ARCHIVE_FILTER_COMPRESS, archive_write_add_filter_compress }, + { ARCHIVE_FILTER_LZMA, archive_write_add_filter_lzma }, + { ARCHIVE_FILTER_XZ, archive_write_add_filter_xz }, + { ARCHIVE_FILTER_LZIP, archive_write_add_filter_lzip }, + { -1, NULL } +}; + +int +archive_write_add_filter(struct archive *a, int code) +{ + int i; + + for (i = 0; codes[i].code != -1; i++) { + if (code == codes[i].code) + return ((codes[i].setter)(a)); + } + + archive_set_error(a, EINVAL, "No such filter"); + return (ARCHIVE_FATAL); +} -- 2.47.2