From: Luca Heltai Date: Fri, 17 Jan 2020 16:09:36 +0000 (+0100) Subject: New compress/decompress using gzip. X-Git-Tag: v9.2.0-rc1~637^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38caedfa676a7a54cddc3f04d8dd80d4d2dbe885;p=dealii.git New compress/decompress using gzip. --- diff --git a/doc/news/changes/minor/20200117LucaHeltai b/doc/news/changes/minor/20200117LucaHeltai new file mode 100644 index 0000000000..a804ce7390 --- /dev/null +++ b/doc/news/changes/minor/20200117LucaHeltai @@ -0,0 +1,3 @@ +New: Utilities::compress() and Utilities::decompress() allow to compress and decompress a string using gzip. +
+(Luca Heltai, Nicola Giuliani, 2020/01/17) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index bc5ae83a5f..18e267d901 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -136,6 +136,44 @@ namespace Utilities pack_integers(const std::array &index, const int bits_per_dim); + /** + * If the library is configured with ZLIB, then this function compresses the + * input string and returns a non-zero terminated string containing the + * compressed input. + * + * If the library was not configured with ZLIB enabled, the returned string + * is identical to the input string. + * + * @param[in] input The string to compress + * @param[in] compression_level The compression level at which we compress + * + * @return A compressed version of the input string + * + * @authors Luca Heltai, Nicola Giuliani, 2020 + */ + std::string + compress( + const std::string &input, + const int compression_level = boost::iostreams::gzip::default_compression); + + /** + * If the library is configured with ZLIB, then this function assumes that the + * input string has been compressed using the compress() function, and returns + * the original decompresses string. + * + * If the library was not configured with ZLIB enabled, the returned string + * is identical to the input string. + * + * @param[in] compressed_input A compressed string, as returned by the + * function compress() + * + * @return The original uncompressed string. + * + * @authors Luca Heltai, Nicola Giuliani, 2020 + */ + std::string + decompress(const std::string &compressed_input); + /** * Convert a number @p value to a string, with as many digits as given to * fill with leading zeros. diff --git a/source/base/utilities.cc b/source/base/utilities.cc index b2cfb58f06..f38c36d5a3 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -379,6 +380,50 @@ namespace Utilities + std::string + compress(const std::string &input, const int compression_level) + { +#ifdef DEAL_II_WITH_ZLIB + namespace bio = boost::iostreams; + + std::stringstream compressed; + std::stringstream origin(input); + + bio::filtering_streambuf out; + out.push(bio::gzip_compressor(bio::gzip_params(compression_level))); + out.push(origin); + bio::copy(out, compressed); + + return compressed.str(); +#else + return input; +#endif + } + + + + std::string + decompress(const std::string &compressed_input) + { +#ifdef DEAL_II_WITH_ZLIB + namespace bio = boost::iostreams; + + std::stringstream compressed(compressed_input); + std::stringstream decompressed; + + bio::filtering_streambuf out; + out.push(bio::gzip_decompressor()); + out.push(compressed); + bio::copy(out, decompressed); + + return decompressed.str(); +#else + return compressed_input; +#endif + } + + + std::string int_to_string(const unsigned int value, const unsigned int digits) { @@ -1283,6 +1328,8 @@ namespace Utilities pack_integers<2>(const std::array &, const int); template std::uint64_t pack_integers<3>(const std::array &, const int); + + } // namespace Utilities DEAL_II_NAMESPACE_CLOSE diff --git a/tests/base/utilities_compress_decompress_01.cc b/tests/base/utilities_compress_decompress_01.cc new file mode 100644 index 0000000000..7aa9f2b22f --- /dev/null +++ b/tests/base/utilities_compress_decompress_01.cc @@ -0,0 +1,31 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// test Utilities::compress/decompress on a string + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + std::string input = "deal.II Rocks!"; + auto compressed = Utilities::compress(input); + auto decompressed = Utilities::decompress(compressed); + deallog << (decompressed == input ? "OK" : "NOT OK") << std::endl; +} diff --git a/tests/base/utilities_compress_decompress_01.with_zlib=true.output b/tests/base/utilities_compress_decompress_01.with_zlib=true.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/utilities_compress_decompress_01.with_zlib=true.output @@ -0,0 +1,2 @@ + +DEAL::OK