IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 87b88b19 authored by ralfulrich's avatar ralfulrich
Browse files

get rid of boost::iostreams

parent 150c0b40
No related branches found
No related tags found
No related merge requests found
Showing with 970 additions and 46 deletions
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)
External projects and licences
- https://github.com/natir/compress_stream, LGPL-3.0 License
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
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.
# 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.
// ============================================================================
// 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
// ============================================================================
// 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
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})
File added
File added
// ============================================================================
// 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");
}
......@@ -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_));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment