diff --git a/CMakeLists.txt b/CMakeLists.txt index f058cc3..17302f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,32 @@ +#BSD 2-Clause license +# +#Copyright (c) 2014, Domen Vrankar +#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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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. + cmake_minimum_required( VERSION 2.8.12 ) -project(libarchive_cpp_wrapper) +project(archive_cpp_wrapper) set(CMAKE_CXX_FLAGS "-Wall -std=c++11") @@ -12,8 +38,6 @@ add_library( ${PROJECT_NAME} SHARED archive_reader.cpp archive_reader_entry.cpp archive_reader_iterator.cpp - archive_reader_file_container.cpp - archive_reader_memory_container.cpp archive_reader_entry_buffer.cpp archive_exception.cpp @@ -23,3 +47,28 @@ add_library( ${PROJECT_NAME} SHARED ) target_link_libraries( ${PROJECT_NAME} /usr/lib/x86_64-linux-gnu/libarchive.so.13 ) +set_target_properties( ${PROJECT_NAME} PROPERTIES VERSION "0.0.1" SOVERSION "1" ) + +install( + TARGETS ${PROJECT_NAME} LIBRARY + DESTINATION lib +) + +install( + FILES + + archive_reader_entry_buffer.hpp + archive_reader.hpp + archive_writer_format.hpp + archive_exception.hpp + archive_writer_entry.hpp + archive_reader_iterator.hpp + archive_reader_format.hpp + archive_writer_filter.hpp + archive_reader_filter.hpp + archive_reader_entry.hpp + archive_writer.hpp + archive_reader.tpp + + DESTINATION include +) diff --git a/archive_reader.cpp b/archive_reader.cpp index dabd822..d462d61 100644 --- a/archive_reader.cpp +++ b/archive_reader.cpp @@ -28,16 +28,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "archive_reader.hpp" -#include "archive_reader_file_container.hpp" -#include "archive_reader_memory_container.hpp" #include "archive_exception.hpp" - #include namespace ns_archive { -reader::reader() : - _archive( archive_read_new(), [](archive* archive){ archive_read_free(archive); } ) // errors in destructor will be silently ignored +reader::reader(std::istream& stream, size_t block_size) : + _archive( archive_read_new(), [](archive* archive){ archive_read_free(archive); } ), // errors in destructor will be silently ignored + _reader_container( stream, block_size ) { // } @@ -119,22 +117,26 @@ READER_INIT_FILTER(LZOP, lzop) READER_INIT_FILTER(GRZIP, gzip) /// ---------------- init_data ---------------- // -template<> -void reader::init_data(ns_reader::file_container&& container) +ssize_t reader_callback( archive* archive, void* in_reader_container, const void** buff ) { - if(archive_read_open_filename(_archive.get(), container.get_path().c_str(), container.get_block_size()) != ARCHIVE_OK) - { - throw archive_exception( "Failed to open archive '" + container.get_path() + "'" ); - } + reader::reader_container* p_reader_container = reinterpret_cast( in_reader_container ); + + p_reader_container->_stream.read( &p_reader_container->_buff[0], p_reader_container->_buff.size() ); + *buff = &p_reader_container->_buff[0]; + + return p_reader_container->_stream.gcount(); } -template<> -void reader::init_data(ns_reader::memory_container&& container) +int close_callback( archive*, void* ) { - std::vector content = container.move_get_buffer(); - if(archive_read_open_memory(_archive.get(), content.data(), content.size()) != ARCHIVE_OK) + return ARCHIVE_OK; +} + +void reader::init_data() +{ + if(archive_read_open( _archive.get(), &_reader_container, nullptr, reader_callback, close_callback ) != ARCHIVE_OK) { - throw archive_exception( "Failed to open memory archive" ); + throw archive_exception( "Failed to read the archive!" ); } } diff --git a/archive_reader.hpp b/archive_reader.hpp index 8f78ae8..1e918a8 100644 --- a/archive_reader.hpp +++ b/archive_reader.hpp @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include "archive_reader_format.hpp" @@ -48,11 +49,11 @@ public: reader(const reader&) = delete; reader& operator=(const reader&) = delete; - template - static reader make_reader(DATA_CONTAINER&& container); + template + static reader make_reader( std::istream& stream, size_t block_size ); - template - static reader make_reader(DATA_CONTAINER&& container); + template + static reader make_reader( std::istream& stream, size_t block_size); ns_reader::entry* get_next_entry(); bool has_next_entry(); @@ -61,7 +62,7 @@ public: ns_reader::iterator end(); private: - reader(); + reader( std::istream& stream, size_t block_size ); template void init_format(); @@ -69,11 +70,26 @@ private: template void init_filter(); - template - void init_data(DATA_CONTAINER&& container); + void init_data(); std::shared_ptr _archive; ns_reader::entry *_next_entry = nullptr; + + class reader_container + { + public: + reader_container( std::istream& stream, size_t block_size ) : + _stream( stream ) + { + _buff.resize( block_size ); + } + public: + std::istream& _stream; + std::vector _buff; + } _reader_container; + + friend ssize_t reader_callback( archive* archive, void* in_reader_container, const void** buff ); + friend int close_callback( archive*, void* ); }; } diff --git a/archive_reader.tpp b/archive_reader.tpp index 9d98478..ee97117 100644 --- a/archive_reader.tpp +++ b/archive_reader.tpp @@ -28,23 +28,23 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace ns_archive { -template -reader reader::make_reader(DATA_CONTAINER&& container) +template +reader reader::make_reader( std::istream& stream, size_t block_size ) { - reader a_reader; + reader a_reader( stream, block_size ); + a_reader.init_format(); + a_reader.init_data(); + + return a_reader; +} + +template +reader reader::make_reader( std::istream& stream, size_t block_size ) +{ + reader a_reader( stream, block_size ); a_reader.init_format(); a_reader.init_filter(); - a_reader.init_data(std::move(container)); - - return a_reader; -} - -template -reader reader::make_reader(DATA_CONTAINER&& container) -{ - reader a_reader; - a_reader.init_format(); - a_reader.init_data(std::move(container)); + a_reader.init_data(); return a_reader; } diff --git a/archive_reader_file_container.cpp b/archive_reader_file_container.cpp deleted file mode 100644 index abdd872..0000000 --- a/archive_reader_file_container.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* -BSD 2-Clause license - -Copyright (c) 2014, Domen Vrankar -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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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_reader_file_container.hpp" - -namespace ns_archive { -namespace ns_reader { - -file_container::file_container(const std::string& file_path, size_t block_size) : - _file_path(file_path), - _block_size(block_size) -{ - // -} - -const std::string& file_container::get_path() const -{ - return _file_path; -} - -size_t file_container::get_block_size() const -{ - return _block_size; -} - -} -} diff --git a/archive_reader_file_container.hpp b/archive_reader_file_container.hpp deleted file mode 100644 index 9d90b67..0000000 --- a/archive_reader_file_container.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* -BSD 2-Clause license - -Copyright (c) 2014, Domen Vrankar -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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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. -*/ - -#ifndef ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED -#define ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED - -#include - -namespace ns_archive { -namespace ns_reader { - -class file_container -{ -public: - file_container(const std::string& file_path, size_t block_size); - - const std::string& get_path() const; - size_t get_block_size() const; - -private: - std::string _file_path; - size_t _block_size; -}; - -} -} - -#endif // ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED diff --git a/archive_reader_memory_container.cpp b/archive_reader_memory_container.cpp deleted file mode 100644 index d0782cb..0000000 --- a/archive_reader_memory_container.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* -BSD 2-Clause license - -Copyright (c) 2014, Domen Vrankar -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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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_reader_memory_container.hpp" - -#include -#include - -namespace ns_archive { -namespace ns_reader { - -memory_container::memory_container(std::istream& input_stream) -{ - std::copy(std::istream_iterator(input_stream), std::istream_iterator(), std::back_inserter(_buffer)); -} - -memory_container::memory_container(std::vector&& buffer) : - _buffer(std::forward>(buffer)) -{ - // -} - -std::vector&& memory_container::move_get_buffer() -{ - return std::move(_buffer); -} - -} -} diff --git a/archive_reader_memory_container.hpp b/archive_reader_memory_container.hpp deleted file mode 100644 index 7863ef7..0000000 --- a/archive_reader_memory_container.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* -BSD 2-Clause license - -Copyright (c) 2014, Domen Vrankar -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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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. -*/ - -#ifndef ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED -#define ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED - -#include -#include - -namespace ns_archive { -namespace ns_reader { - -class memory_container -{ -public: - memory_container(std::istream& input_stream); - memory_container(std::vector&& buffer); - - std::vector&& move_get_buffer(); - -private: - std::vector _buffer; -}; - -} -} - -#endif // ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED