compressor.hpp

Go to the documentation of this file.
00001 /*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : dar.linux@free.fr
00020 /*********************************************************************/
00021 // $Id: compressor.hpp,v 1.12.4.2 2007/07/22 16:34:59 edrusb Rel $
00022 //
00023 /*********************************************************************/
00026 
00027 #ifndef COMPRESSOR_HPP
00028 #define COMPRESSOR_HPP
00029 
00030 #include "../my_config.h"
00031 
00032 #include "infinint.hpp"
00033 #include "generic_file.hpp"
00034 #include "integers.hpp"
00035 #include "wrapperlib.hpp"
00036 
00037 namespace libdar
00038 {
00039 
00041 
00044     enum compression
00045     {
00046         none = 'n', 
00047         zip = 'p',  
00048         gzip = 'z', 
00049         bzip2 = 'y' 
00050     };
00051 
00052     extern compression char2compression(char a);
00053     extern char compression2char(compression c);
00054     extern std::string compression2string(compression c);
00055 
00057     class compressor : public generic_file
00058     {
00059     public :
00060         compressor(user_interaction & dialog, compression algo, generic_file & compressed_side, U_I compression_level = 9);
00061             // compressed_side is not owned by the object and will remains
00062             // after the objet destruction
00063         compressor(user_interaction & dialog, compression algo, generic_file *compressed_side, U_I compression_level = 9);
00064             // compressed_side is owned by the object and will be
00065             // deleted a destructor time
00066         ~compressor();
00067 
00068         void flush_write(); // flush all data to compressed_side, and reset the compressor
00069             // for that additional write can be uncompresssed starting at this point.
00070         void flush_read(); // reset decompression engine to be able to read the next block of compressed data
00071             // if not called, furthur read return EOF
00072         void clean_read(); // discard any byte buffered and not yet returned by read()
00073         void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
00074 
00075         compression get_algo() const { return current_algo; };
00076 
00078 
00083         void change_algo(compression new_algo, U_I new_compression_level);
00084 
00085 
00087 
00088         void change_algo(compression new_algo)
00089         {
00090             change_algo(new_algo, current_level);
00091         };
00092 
00093             // inherited from generic file
00094         bool skip(const infinint & position) { flush_write(); flush_read(); clean_read(); return compressed->skip(position); };
00095         bool skip_to_eof()  { flush_write(); flush_read(); clean_read(); return compressed->skip_to_eof(); };
00096         bool skip_relative(S_I x) { flush_write(); flush_read(); clean_read(); return compressed->skip_relative(x); };
00097         infinint get_position() { return compressed->get_position(); };
00098 
00099     protected :
00100         S_I inherited_read(char *a, size_t size) { return (this->*read_ptr)(a, size); };
00101         S_I inherited_write(const char *a, size_t size) { return (this->*write_ptr)(a, size); };
00102 
00103     private :
00104         struct xfer
00105         {
00106             wrapperlib wrap;
00107             char *buffer;
00108             U_I size;
00109 
00110             xfer(U_I sz, wrapperlib_mode mode);
00111             ~xfer();
00112         };
00113 
00114         xfer *compr, *decompr;
00115         generic_file *compressed;
00116         bool compressed_owner;
00117         compression current_algo;
00118         U_I current_level;
00119 
00120         void init(compression algo, generic_file *compressed_side, U_I compression_level);
00121         void terminate();
00122         S_I (compressor::*read_ptr) (char *a, size_t size);
00123         S_I none_read(char *a, size_t size);
00124         S_I gzip_read(char *a, size_t size);
00125             // S_I zip_read(char *a, size_t size);
00126             // S_I bzip2_read(char *a, size_t size); // using gzip_read, same code thanks to wrapperlib
00127 
00128         S_I (compressor::*write_ptr) (const char *a, size_t size);
00129         S_I none_write(const char *a, size_t size);
00130         S_I gzip_write(const char *a, size_t size);
00131             // S_I zip_write(char *a, size_t size);
00132             // S_I bzip2_write(char *a, size_t size); // using gzip_write, same code thanks to wrapperlib
00133     };
00134 
00135 } // end of namespace
00136 
00137 #endif

Generated on Fri Jun 20 23:48:26 2008 for Disk ARchive by  doxygen 1.5.6