]> https://gitweb.dealii.org/ - dealii.git/commitdiff
New compress/decompress using gzip.
authorLuca Heltai <luca.heltai@sissa.it>
Fri, 17 Jan 2020 16:09:36 +0000 (17:09 +0100)
committerLuca Heltai <luca.heltai@sissa.it>
Fri, 17 Jan 2020 16:15:18 +0000 (17:15 +0100)
doc/news/changes/minor/20200117LucaHeltai [new file with mode: 0644]
include/deal.II/base/utilities.h
source/base/utilities.cc
tests/base/utilities_compress_decompress_01.cc [new file with mode: 0644]
tests/base/utilities_compress_decompress_01.with_zlib=true.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20200117LucaHeltai b/doc/news/changes/minor/20200117LucaHeltai
new file mode 100644 (file)
index 0000000..a804ce7
--- /dev/null
@@ -0,0 +1,3 @@
+New: Utilities::compress() and Utilities::decompress() allow to compress and decompress a string using gzip.
+<br>
+(Luca Heltai, Nicola Giuliani, 2020/01/17)
index bc5ae83a5f82a16bf420aa72c8eca649b26a014e..18e267d901154b42ac560dabcde640d0f819983a 100644 (file)
@@ -136,6 +136,44 @@ namespace Utilities
   pack_integers(const std::array<std::uint64_t, dim> &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.
index b2cfb58f06096aa36fd726c08580810f0f306051..f38c36d5a39f20d3432620eb2cbcf7624b2b1faa 100644 (file)
@@ -28,6 +28,7 @@
 #include <deal.II/base/thread_local_storage.h>
 #include <deal.II/base/utilities.h>
 
+#include <boost/iostreams/copy.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/random.hpp>
 
@@ -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<bio::input> 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<bio::input> 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<std::uint64_t, 2> &, const int);
   template std::uint64_t
   pack_integers<3>(const std::array<std::uint64_t, 3> &, 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 (file)
index 0000000..7aa9f2b
--- /dev/null
@@ -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 <deal.II/base/utilities.h>
+
+#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 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.