From 87b88b198aa67ec188637bc94884eef22cdc9127 Mon Sep 17 00:00:00 2001 From: ralfulrich <ralf.ulrich@kit.edu> Date: Wed, 4 Nov 2020 09:44:56 +0100 Subject: [PATCH] get rid of boost::iostreams --- readLib/CMakeLists.txt | 51 ++-- readLib/externals/README.md | 3 + readLib/externals/compress_stream/.gitignore | 53 ++++ readLib/externals/compress_stream/LICENSE | 165 +++++++++++ readLib/externals/compress_stream/README.md | 65 +++++ .../compress_stream/include/bzstream.hpp | 258 ++++++++++++++++++ .../compress_stream/include/gzstream.hpp | 246 +++++++++++++++++ .../compress_stream/test/CMakeLists.txt | 49 ++++ .../compress_stream/test/reference.bz | Bin 0 -> 252 bytes .../compress_stream/test/reference.gz | Bin 0 -> 231 bytes .../compress_stream/test/src/main.cpp | 103 +++++++ .../{Interface.boost.cc => Interface.bz2.cc} | 23 +- 12 files changed, 970 insertions(+), 46 deletions(-) create mode 100644 readLib/externals/README.md create mode 100644 readLib/externals/compress_stream/.gitignore create mode 100644 readLib/externals/compress_stream/LICENSE create mode 100644 readLib/externals/compress_stream/README.md create mode 100644 readLib/externals/compress_stream/include/bzstream.hpp create mode 100644 readLib/externals/compress_stream/include/gzstream.hpp create mode 100644 readLib/externals/compress_stream/test/CMakeLists.txt create mode 100644 readLib/externals/compress_stream/test/reference.bz create mode 100644 readLib/externals/compress_stream/test/reference.gz create mode 100644 readLib/externals/compress_stream/test/src/main.cpp rename readLib/source/{Interface.boost.cc => Interface.bz2.cc} (88%) diff --git a/readLib/CMakeLists.txt b/readLib/CMakeLists.txt index 313d20b..8837539 100644 --- a/readLib/CMakeLists.txt +++ b/readLib/CMakeLists.txt @@ -1,34 +1,28 @@ -find_package (Boost OPTIONAL_COMPONENTS iostreams) +cmake_minimum_required (VERSION 3.9) +project (CorsikaData) -if (Boost_IOSTREAMS_FOUND) - set ( - files - source/Interface.boost.cc - ) -else (Boost_IOSTREAMS_FOUND) - message (STATUS "Boost::iostreams not found, data tables need to be unpacked with bunzip2 manually!") - set ( +find_package (BZip2 REQUIRED) + +set ( files - source/Interface.dummy.cc + source/Interface.bz2.cc ) -endif (Boost_IOSTREAMS_FOUND) add_library (CorsikaData STATIC ${files}) - -if (Boost_IOSTREAMS_FOUND) - target_link_libraries (CorsikaData PUBLIC Boost::iostreams) -endif (Boost_IOSTREAMS_FOUND) +target_link_libraries (CorsikaData PUBLIC BZip2::BZip2) set_target_properties ( CorsikaData PROPERTIES PUBLIC_HEADER corsika_data/Interface.h + POSITION_INDEPENDENT_CODE 1 ) target_include_directories ( CorsikaData PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/externals/compress_stream/include> $<INSTALL_INTERFACE:include/> ) @@ -41,21 +35,14 @@ install ( # add unit test, if run inside CORSIKA if (COMMAND CORSIKA_ADD_TEST) - CORSIKA_ADD_TEST (testData SOURCES source/testData.cc) - set (TEST_WITH_BOOST "") - if (Boost_IOSTREAMS_FOUND) - set (TEST_WITH_BOOST "TEST_WITH_BOOST") - endif (Boost_IOSTREAMS_FOUND) - - target_compile_definitions ( - testData - PRIVATE - TESTDATA="${CMAKE_CURRENT_SOURCE_DIR}/source/test.data" - ${TEST_WITH_BOOST} - ) - target_link_libraries ( - testData - CorsikaData - CORSIKAtesting - ) + if (CORSIKA_DATA_WITH_TEST) + CORSIKA_ADD_TEST (testData SOURCES source/testData.cc) + target_compile_definitions ( + testData + PRIVATE + TESTDATA="${CMAKE_CURRENT_SOURCE_DIR}/source/test.data" + ) + endif (CORSIKA_DATA_WITH_TEST) endif (COMMAND CORSIKA_ADD_TEST) + +export (TARGETS CorsikaData NAMESPACE CORSIKA:: FILE CorsikaData.cmake) diff --git a/readLib/externals/README.md b/readLib/externals/README.md new file mode 100644 index 0000000..390f5af --- /dev/null +++ b/readLib/externals/README.md @@ -0,0 +1,3 @@ +External projects and licences + +- https://github.com/natir/compress_stream, LGPL-3.0 License diff --git a/readLib/externals/compress_stream/.gitignore b/readLib/externals/compress_stream/.gitignore new file mode 100644 index 0000000..12aef5e --- /dev/null +++ b/readLib/externals/compress_stream/.gitignore @@ -0,0 +1,53 @@ +test/out.gz +test/out.bz + +# Created by https://www.gitignore.io/api/c++,cmake + +### C++ ### +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +### CMake ### +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +build + + +# End of https://www.gitignore.io/api/c++,cmake diff --git a/readLib/externals/compress_stream/LICENSE b/readLib/externals/compress_stream/LICENSE new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/readLib/externals/compress_stream/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/readLib/externals/compress_stream/README.md b/readLib/externals/compress_stream/README.md new file mode 100644 index 0000000..7515203 --- /dev/null +++ b/readLib/externals/compress_stream/README.md @@ -0,0 +1,65 @@ +# compress_stream + +Header only c++ library for read and write compress file with std like stream + +## Credits + +Code comes from [nice-core project](https://github.com/cvjena/nice-core), I just merge declaration and implementation of class in one header file, and add test. + +## How to use + +### Instalation + +#### Git submodule + +Add compress_stream as submodule : + +``` +git submodule add https://github.com/natir/compress_stream.git +``` + +#### Hard copy + +Download include file : + +``` +wget https://raw.githubusercontent.com/natir/compress_stream/master/include/gzstream.hpp +wget https://raw.githubusercontent.com/natir/compress_stream/master/include/bzstream.hpp +``` + +### Compile with cmake + +```cmake +find_package(ZLIB REQUIRED) +include_directories(${ZLIB_INCLUDE_DIRS}) + +find_package(BZip2 REQUIRED) +include_directories(${BZIP2_INCLUDE_DIR}) + + +# If you use git submodule uncomment this line +# include_directories(compress_stream/include/) +# else +# include_directories(path/to/header/file/) + +file( + GLOB_RECURSE + src_files + src/* + ) + +add_executable(${executable_name} ${src_files}) +target_link_libraries(${executable_name} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES}) +``` + +### Compile with manual build system + +``` +g++ $(pkg-config --cflags --libs zlib) -lbz2 dev/compress_stream/test/src/main.cpp -I path/to/header/file/ +``` + + +## Documentation + +Use (i|o)gzstream and (i|o)bzstream exactly like classic (i|o)stream. + diff --git a/readLib/externals/compress_stream/include/bzstream.hpp b/readLib/externals/compress_stream/include/bzstream.hpp new file mode 100644 index 0000000..331f1a4 --- /dev/null +++ b/readLib/externals/compress_stream/include/bzstream.hpp @@ -0,0 +1,258 @@ +// ============================================================================ +// bzstream, C++ iostream classes wrapping the zlib compression library. +// Copyright (C) 2006 Frank Mattern +// +// This library is free software; you can 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. +// +// 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// ============================================================================ +// +// File : bzstream.h +// Revision : $Revision: 1.2 $ +// Revision_date : $Date: 2009/05/28 11:36:30 $ +// Author(s) : Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern +// +// Standard streambuf implementation following Nicolai Josuttis, "The +// Standard C++ Library". +// ============================================================================ + + +// ============================================================================ +// Update by Pierre Marijon <pierre@marijon.fr> in 2018 to become header only +// ============================================================================ + +#ifndef BZSTREAM_H +#define BZSTREAM_H 1 + + +// standard C++ with new header file names and std:: namespace +#include <iostream> +#include <fstream> +#include <cstring> +#include <string> + +#include <bzlib.h> + +// ---------------------------------------------------------------------------- +// Internal classes to implement bzstream. See below for user classes. +// ---------------------------------------------------------------------------- + +class bzstreambuf : public std::streambuf { +private: + static const int bufferSize = 47+256; // size of data buff + // totals 512 bytes under g++ for ibzstream at the end. + + BZFILE *file; // file handle for compressed file + char buffer[bufferSize]; // data buffer + char opened; // open/close state of stream + int mode; // I/O mode + + int flush_buffer(); +public: + bzstreambuf() : opened(0) { + setp( buffer, buffer + (bufferSize-1)); + setg( buffer + 4, // beginning of putback area + buffer + 4, // read position + buffer + 4); // end position + // ASSERT: both input & output capabilities will not be used together + } + int is_open() { return opened; } + bzstreambuf* open( const std::string& name, int open_mode); + bzstreambuf* close(); + ~bzstreambuf() { close(); } + + virtual int overflow( int c = EOF); + virtual int underflow(); + virtual int sync(); +}; + +class bzstreambase : virtual public std::ios { +protected: + bzstreambuf buf; +public: + bzstreambase() { init(&buf); } + bzstreambase( const std::string& name, int open_mode); + ~bzstreambase(); + void open( const std::string& name, int open_mode); + void close(); + bzstreambuf* rdbuf() { return &buf; } +}; + +// ---------------------------------------------------------------------------- +// User classes. Use ibzstream and obzstream analogously to ifstream and +// ofstream respectively. They read and write files based on the bz* +// function interface of the zlib. Files are compatible with bzip compression. +// ---------------------------------------------------------------------------- + +/** + * Input bzip stream class. + * Supports reading of bzip files. + * + * @author Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern + */ +class ibzstream : public bzstreambase, public std::istream { +public: + ibzstream() : std::istream( &buf) {} + ibzstream( const std::string& name, int open_mode = std::ios::in) + : bzstreambase( name, open_mode), std::istream( &buf) {} + bzstreambuf* rdbuf() { return bzstreambase::rdbuf(); } + void open( const std::string& name, int open_mode = std::ios::in) { + bzstreambase::open( name, open_mode); + } + bool is_open() { return bzstreambase::rdbuf() ? bzstreambase::rdbuf()->is_open() : false; } +}; + +/** + * Output bzip stream class. + * Supports writing of bzip files. + * + * @author Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern + */ +class obzstream : public bzstreambase, public std::ostream { +public: + obzstream() : std::ostream( &buf) {} + obzstream( const char* name, int mode = std::ios::out) + : bzstreambase( name, mode), std::ostream( &buf) {} + bzstreambuf* rdbuf() { return bzstreambase::rdbuf(); } + void open( const std::string& name, int open_mode = std::ios::out) { + bzstreambase::open( name, open_mode); + } + bool is_open() { return bzstreambase::rdbuf() ? bzstreambase::rdbuf()->is_open() : false; } +}; + +// ---------------------------------------------------------------------------- +// Internal classes to implement bzstream. See header file for user classes. +// ---------------------------------------------------------------------------- + +// -------------------------------------- +// class bzstreambuf: +// -------------------------------------- + +bzstreambuf* bzstreambuf::open( const std::string& name, int open_mode) { + if ( is_open()) + return (bzstreambuf*)0; + mode = open_mode; + // no append nor read/write mode + if ((mode & std::ios::ate) || (mode & std::ios::app) + || ((mode & std::ios::in) && (mode & std::ios::out))) + return (bzstreambuf*)0; + char fmode[10]; + char* fmodeptr = fmode; + if ( mode & std::ios::in) + *fmodeptr++ = 'r'; + else if ( mode & std::ios::out) + *fmodeptr++ = 'w'; + *fmodeptr++ = 'b'; + *fmodeptr = '\0'; + file = 0; + file = BZ2_bzopen( name.c_str(), fmode); + if (file == 0) + return (bzstreambuf*)0; + opened = 1; + return this; +} + +bzstreambuf * bzstreambuf::close() { + if ( is_open()) { + sync(); + opened = 0; + BZ2_bzclose(file); + return this; + } + return (bzstreambuf*)0; +} + +int bzstreambuf::underflow() { // used for input buffer only + if ( gptr() && ( gptr() < egptr())) + return * reinterpret_cast<unsigned char *>( gptr()); + + if ( ! (mode & std::ios::in) || ! opened) + return EOF; + // Josuttis' implementation of inbuf + int n_putback = gptr() - eback(); + if ( n_putback > 4) + n_putback = 4; + std::memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback); + + int num = 0; + num = BZ2_bzread( file, buffer+4, bufferSize-4); + if (num <= 0) // ERROR or EOF + return EOF; + + // reset buffer pointers + setg( buffer + (4 - n_putback), // beginning of putback area + buffer + 4, // read position + buffer + 4 + num); // end of buffer + + // return next character + return * reinterpret_cast<unsigned char *>( gptr()); +} + +int bzstreambuf::flush_buffer() { + // Separate the writing of the buffer from overflow() and + // sync() operation. + int w = pptr() - pbase(); + if ( BZ2_bzwrite( file, pbase(), w) != w) + return EOF; + pbump( -w); + return w; +} + +int bzstreambuf::overflow( int c) { // used for output buffer only + if ( ! ( mode & std::ios::out) || ! opened) + return EOF; + if (c != EOF) { + *pptr() = c; + pbump(1); + } + if ( flush_buffer() == EOF) + return EOF; + return c; +} + +int bzstreambuf::sync() { + // Changed to use flush_buffer() instead of overflow( EOF) + // which caused improper behavior with std::endl and flush(), + // bug reported by Vincent Ricard. + if ( pptr() && pptr() > pbase()) { + if ( flush_buffer() == EOF) + return -1; + } + return 0; +} + +// -------------------------------------- +// class bzstreambase: +// -------------------------------------- + +bzstreambase::bzstreambase( const std::string& name, int mode) { + init( &buf); + open( name, mode); +} + +bzstreambase::~bzstreambase() { + buf.close(); +} + +void bzstreambase::open( const std::string& name, int open_mode) { + if ( ! buf.open( name, open_mode)) + clear( rdstate() | std::ios::badbit); +} + +void bzstreambase::close() { + if ( buf.is_open()) + if ( ! buf.close()) + clear( rdstate() | std::ios::badbit); +} + +#endif // BZSTREAM_H diff --git a/readLib/externals/compress_stream/include/gzstream.hpp b/readLib/externals/compress_stream/include/gzstream.hpp new file mode 100644 index 0000000..ad06ec0 --- /dev/null +++ b/readLib/externals/compress_stream/include/gzstream.hpp @@ -0,0 +1,246 @@ +// ============================================================================ +// gzstream, C++ iostream classes wrapping the zlib compression library. +// Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner +// +// This library is free software; you can 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. +// +// 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// ============================================================================ +// +// File : gzstream.h +// Revision : $Revision: 1.2 $ +// Revision_date : $Date: 2009/05/28 11:36:30 $ +// Author(s) : Deepak Bandyopadhyay, Lutz Kettner +// +// Standard streambuf implementation following Nicolai Josuttis, "The +// Standard C++ Library". +// ============================================================================ + +// ============================================================================ +// Update by Pierre Marijon <pierre@marijon.fr> in 2018 to become header only +// ============================================================================ + +#ifndef GZSTREAM_H +#define GZSTREAM_H 1 + +#include <iostream> +#include <fstream> +#include <cstring> + +#include <zlib.h> + +// ---------------------------------------------------------------------------- +// Internal classes to implement gzstream. See below for user classes. +// ---------------------------------------------------------------------------- + +class gzstreambuf : public std::streambuf { +private: + static const int bufferSize = 47+256; // size of data buff + // totals 512 bytes under g++ for igzstream at the end. + + gzFile file; // file handle for compressed file + char buffer[bufferSize]; // data buffer + char opened; // open/close state of stream + int mode; // I/O mode + + int flush_buffer(); +public: + gzstreambuf() : opened(0) { + setp( buffer, buffer + (bufferSize-1)); + setg( buffer + 4, // beginning of putback area + buffer + 4, // read position + buffer + 4); // end position + // ASSERT: both input & output capabilities will not be used together + } + int is_open() { return opened; } + gzstreambuf* open( const char* name, int open_mode); + gzstreambuf* close(); + ~gzstreambuf() { close(); } + + virtual int overflow( int c = EOF); + virtual int underflow(); + virtual int sync(); +}; + +class gzstreambase : virtual public std::ios { +protected: + gzstreambuf buf; +public: + gzstreambase() { init(&buf); } + gzstreambase( const char* name, int open_mode); + ~gzstreambase(); + void open( const char* name, int open_mode); + void close(); + gzstreambuf* rdbuf() { return &buf; } +}; + +// ---------------------------------------------------------------------------- +// User classes. Use igzstream and ogzstream analogously to ifstream and +// ofstream respectively. They read and write files based on the gz* +// function interface of the zlib. Files are compatible with gzip compression. +// ---------------------------------------------------------------------------- + +/** + * Input gzip stream class. + * Supports reading of gzip files. + * + * @author Deepak Bandyopadhyay, Lutz Kettner + */ +class igzstream : public gzstreambase, public std::istream { +public: + igzstream() : std::istream( &buf) {} + igzstream( const char* name, int open_mode = std::ios::in) + : gzstreambase( name, open_mode), std::istream( &buf) {} + gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); } + void open( const char* name, int open_mode = std::ios::in) { + gzstreambase::open( name, open_mode); + } +}; + +/** + * Output gzip stream class. + * Supports writing of gzip files. + * + * @author Deepak Bandyopadhyay, Lutz Kettner + */ +class ogzstream : public gzstreambase, public std::ostream { +public: + ogzstream() : std::ostream( &buf) {} + ogzstream( const char* name, int mode = std::ios::out) + : gzstreambase( name, mode), std::ostream( &buf) {} + gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); } + void open( const char* name, int open_mode = std::ios::out) { + gzstreambase::open( name, open_mode); + } +}; + + +gzstreambuf* gzstreambuf::open( const char* name, int open_mode) { + if ( is_open()) + return (gzstreambuf*)0; + mode = open_mode; + // no append nor read/write mode + if ((mode & std::ios::ate) || (mode & std::ios::app) + || ((mode & std::ios::in) && (mode & std::ios::out))) + return (gzstreambuf*)0; + char fmode[10]; + char* fmodeptr = fmode; + if ( mode & std::ios::in) + *fmodeptr++ = 'r'; + else if ( mode & std::ios::out) + *fmodeptr++ = 'w'; + *fmodeptr++ = 'b'; + *fmodeptr = '\0'; + file = 0; + file = gzopen( name, fmode); + if (file == 0) + return (gzstreambuf*)0; + opened = 1; + return this; +} + +gzstreambuf * gzstreambuf::close() { + if ( is_open()) { + sync(); + opened = 0; + if ( gzclose( file) == Z_OK) + return this; + } + return (gzstreambuf*)0; +} + +int gzstreambuf::underflow() { // used for input buffer only + if ( gptr() && ( gptr() < egptr())) + return * reinterpret_cast<unsigned char *>( gptr()); + + if ( ! (mode & std::ios::in) || ! opened) + return EOF; + // Josuttis' implementation of inbuf + int n_putback = gptr() - eback(); + if ( n_putback > 4) + n_putback = 4; + std::memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback); + + int num = 0; + num = gzread( file, buffer+4, bufferSize-4); + if (num <= 0) // ERROR or EOF + return EOF; + + // reset buffer pointers + setg( buffer + (4 - n_putback), // beginning of putback area + buffer + 4, // read position + buffer + 4 + num); // end of buffer + + // return next character + return * reinterpret_cast<unsigned char *>( gptr()); +} + +int gzstreambuf::flush_buffer() { + // Separate the writing of the buffer from overflow() and + // sync() operation. + int w = pptr() - pbase(); + if ( gzwrite( file, pbase(), w) != w) + return EOF; + pbump( -w); + return w; +} + +int gzstreambuf::overflow( int c) { // used for output buffer only + if ( ! ( mode & std::ios::out) || ! opened) + return EOF; + if (c != EOF) { + *pptr() = c; + pbump(1); + } + if ( flush_buffer() == EOF) + return EOF; + return c; +} + +int gzstreambuf::sync() { + // Changed to use flush_buffer() instead of overflow( EOF) + // which caused improper behavior with std::endl and flush(), + // bug reported by Vincent Ricard. + if ( pptr() && pptr() > pbase()) { + if ( flush_buffer() == EOF) + return -1; + } + return 0; +} + +// -------------------------------------- +// class gzstreambase: +// -------------------------------------- + +gzstreambase::gzstreambase( const char* name, int mode) { + init( &buf); + open( name, mode); +} + +gzstreambase::~gzstreambase() { + buf.close(); +} + +void gzstreambase::open( const char* name, int open_mode) { + if ( ! buf.open( name, open_mode)) + clear( rdstate() | std::ios::badbit); +} + +void gzstreambase::close() { + if ( buf.is_open()) + if ( ! buf.close()) + clear( rdstate() | std::ios::badbit); +} + +#endif // GZSTREAM_H + diff --git a/readLib/externals/compress_stream/test/CMakeLists.txt b/readLib/externals/compress_stream/test/CMakeLists.txt new file mode 100644 index 0000000..2e6688b --- /dev/null +++ b/readLib/externals/compress_stream/test/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 2.8) + +project(test_compress_stream) + +# Set default build type to release +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'Release' as none was specified.") + set(CMAKE_BUILD_TYPE "Release" CACHE + STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Release" "MinSizeRel" "RelWithDebInfo") +endif() + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# warning flags +set(WARNING_FLAGS "-Wredundant-decls -Wcast-align -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Wextra -Wall -Werror -Winvalid-pch -Wredundant-decls -Wformat=2 -Wmissing-format-attribute -Wformat-nonliteral") +set(ADD_FLAGS_DEBUG "${WARNING_FLAGS} -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_GLIBCXX_SANITIZE_VECTOR") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ADD_FLAGS_DEBUG}") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ADD_FLAGS_DEBUG}") + +# optimisation flags +set(ADD_FLAGS_RELEASE "-O3 -flto -march=native -mtune=native") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${ADD_FLAGS_RELEASE}") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${ADD_FLAGS_RELEASE}") +set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${ADD_FLAGS_RELEASE}") +set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${ADD_FLAGS_RELEASE}") + + +find_package(ZLIB REQUIRED) +include_directories(${ZLIB_INCLUDE_DIRS}) + +find_package(BZip2 REQUIRED) +include_directories(${BZIP2_INCLUDE_DIR}) + + +include_directories(../include/) + +file( + GLOB_RECURSE + src_files + src/* + ) + +add_executable(${PROJECT_NAME} ${src_files}) +target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES}) diff --git a/readLib/externals/compress_stream/test/reference.bz b/readLib/externals/compress_stream/test/reference.bz new file mode 100644 index 0000000000000000000000000000000000000000..e34c770452b3a5ead334749cb51cf649afcbf5ed GIT binary patch literal 252 zcmV<Y00aL*T4*^jL0KkKS;=bmC;$K-*MI;JKm`H^N&r9S{{S!m=2SI3H51Av&?cH` z>S>{j00000l{ThRB4(OR0Awjj-1Iqi*)Lo57Puh-H=OUFig9)S4aB&d6bnNrb3>kH zLfF)|&%&5xhBBWObTAIaV=S+tdQ}VvI;WbZ4y6Bv#G7f%)ri!|UR|SBqeQdTrIwsj zvC7lIxt#WjP8>&Fwl}$xUgNm%<1mFeXb@pKIb`J^65AcM*I8#{VuznP3e?q`Z2<eK z2tpi&W*t`ZmJWzh43MSGI#uFT#_`2${)32dcyZr@98U>zGn(%ie;0B^I8czuYWFBP CVsN+s literal 0 HcmV?d00001 diff --git a/readLib/externals/compress_stream/test/reference.gz b/readLib/externals/compress_stream/test/reference.gz new file mode 100644 index 0000000000000000000000000000000000000000..34c9a65b34e76da2aebdd1f4b83ece4f16df1e3c GIT binary patch literal 231 zcmV<D02u!tiwFP!0000015J>@Z9_2(L+_n}1AKUk-r6ES(E$`&`4M1SjwNUQDk=Jr zGez=IA3C`~hOLI8RVPdw)Yvy<?Z(;J*8~+AVp;kQuHp&rF4S-rCk9XQ!-x8eyz^D^ zs)R<BZBe0w(<C<T4(B$OZE&6bsN{k|L(a?4G|pZ4MQkwI#a%2?qkIGJ(ev+!b7&C- zRk^JY8tpyWK6ReunSCJOwy16BoH4K3bGWPQL7DL5=-P4N*@7^>5mS~Wd@mc^i<mKn hdT5gH71L?u2i}FURIy<D+kFJ2w_g$)h;dv2008xGZ?gaZ literal 0 HcmV?d00001 diff --git a/readLib/externals/compress_stream/test/src/main.cpp b/readLib/externals/compress_stream/test/src/main.cpp new file mode 100644 index 0000000..04a3aaf --- /dev/null +++ b/readLib/externals/compress_stream/test/src/main.cpp @@ -0,0 +1,103 @@ +// ============================================================================ +// compress_stream, C++ iostream classes wrapping the zlib compression library. +// Copyright (C) 2018 PierreĆ Marijon <pierre@marijon.fr> +// +// This library is free software; you can 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. +// +// 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// ============================================================================ + +/* standard include */ + +/* project include */ +#include "gzstream.hpp" +#include "bzstream.hpp" + + +#include <algorithm> +#include <iterator> +#include <string> +#include <fstream> + +template<typename InputIterator1, typename InputIterator2> +bool +range_equal(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2) +{ + while(first1 != last1 && first2 != last2) + { + if(*first1 != *first2) return false; + ++first1; + ++first2; + } + return (first1 == last1) && (first2 == last2); +} + +bool compare_files(const std::string& filename1, const std::string& filename2) +{ + std::ifstream file1(filename1); + std::ifstream file2(filename2); + + std::istreambuf_iterator<char> begin1(file1); + std::istreambuf_iterator<char> begin2(file2); + + std::istreambuf_iterator<char> end; + + return range_equal(begin1, end, begin2, end); +} + +void test_out(bool result, std::string message) +{ + if(result) + { + std::cout<<"\033[32m"<<message<<": Passed"<<"\033[0m"<<std::endl; + } + else + { + std::cout<<"\033[31m"<<message<<": Failled"<<"\033[0m"<<std::endl; + } +} + +int main(int argc, char** argv) +{ + std::string message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae erat sit amet lorem vehicula malesuada. Praesent tempus, tortor sed dapibus molestie, tortor sem sagittis velit, eget dapibus est odio aliquet magna. Morbi sed malesuada quam, nec rhoncus ligula. Ut non fermentum metus. Suspendisse potenti. Nam auctor facilisis lorem fermentum."; + + /* write a message */ + ogzstream out_gz("out.gz"); + obzstream out_bz("out.bz"); + + out_gz<<message<<std::endl; + out_bz<<message<<std::endl; + + out_gz.close(); + out_bz.close(); + + /* read message */ + igzstream in_gz("out.gz"); + ibzstream in_bz("out.bz"); + + std::string gzread_message; + std::getline(in_gz, gzread_message); + + std::string bzread_message; + std::getline(in_bz, bzread_message); + + /* check */ + /* created file to reference file */ + test_out(compare_files("out.gz", "reference.gz"), "gzip file same as reference"); + test_out(compare_files("out.bz", "reference.bz"), "bzip file same as reference"); + + /* read message to write message */ + test_out(gzread_message == message, "gz message read same as original"); + test_out(bzread_message == message, "bz message read same as original"); +} diff --git a/readLib/source/Interface.boost.cc b/readLib/source/Interface.bz2.cc similarity index 88% rename from readLib/source/Interface.boost.cc rename to readLib/source/Interface.bz2.cc index f2fb278..5b489c5 100644 --- a/readLib/source/Interface.boost.cc +++ b/readLib/source/Interface.bz2.cc @@ -8,8 +8,8 @@ #include <corsika_data/Interface.h> -#include <boost/iostreams/filter/bzip2.hpp> -#include <boost/iostreams/filtering_stream.hpp> +#include <bzstream.hpp> + #include <fstream> #include <iostream> #include <string> @@ -20,9 +20,8 @@ using namespace std; namespace corsika_data { struct DataFile { - std::ifstream file_in_; + ibzstream file_in_; std::string file_name_; - boost::iostreams::filtering_istream in_; DataFile() {} ~DataFile() {} void Open(const char* name) { @@ -30,7 +29,6 @@ namespace corsika_data { std::cout << "DataFile is still open! Closing it now..." << std::endl; file_in_.close(); } - in_.reset(); std::string s(name); auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left @@ -42,15 +40,12 @@ namespace corsika_data { << std::endl; throw std::runtime_error(std::string("Cannot open file: ") + file_name_); } - if (trimmed.rfind(".bz2") == trimmed.length() - 4) - in_.push(boost::iostreams::bzip2_decompressor()); - in_.push(file_in_); } void Close() { file_in_.close(); } void FillArray(double* data, const int& length) { for (int i = 0; i < length; i++) { - in_ >> data[i]; - if (!in_.good()) { + file_in_ >> data[i]; + if (!file_in_.good()) { throw std::runtime_error(std::string("Cannot FillArray i/length=") + std::to_string(i) + "/" + std::to_string(length) + " in file " + file_name_); @@ -59,16 +54,16 @@ namespace corsika_data { } double ReadNextNumber() { double data; - in_ >> data; - if (!in_.good()) { + file_in_ >> data; + if (!file_in_.good()) { throw std::runtime_error( std::string("Cannot ReadNextNumber in file " + file_name_)); } return data; } int ReadNextText(std::string& data) { - std::getline(in_, data, ' '); - if (!in_.good()) { + std::getline(file_in_, data, ' '); + if (!file_in_.good()) { throw std::runtime_error( std::string("Cannot ReadNextText in file " + file_name_)); } -- GitLab