Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
.pc/delete2/compressor.hpp
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$
00022 //
00023 /*********************************************************************/
00024 
00025 #ifndef COMPRESSOR_HPP
00026 #define COMPRESSOR_HPP
00027 
00028 #pragma interface
00029 
00030 #include "generic_file.hpp"
00031 #include "integers.hpp"
00032 #include "wrapperlib.hpp"
00033 
00034 enum compression { none = 'n', zip = 'p', gzip = 'z', bzip2 = 'y' };
00035 
00036 extern compression char2compression(char a);
00037 extern char compression2char(compression c);
00038 extern string compression2string(compression c);
00039 
00040 class compressor : public generic_file
00041 {
00042 public :
00043     compressor(compression algo, generic_file & compressed_side, U_I compression_level = 9);
00044         // compressed_side is not owned by the object and will remains
00045         // after the objet destruction
00046     compressor(compression algo, generic_file *compressed_side, U_I compression_level = 9);
00047         // compressed_side is owned by the object and will be 
00048         // deleted a destructor time
00049     ~compressor();
00050     
00051     void flush_write(); // flush all data to compressed_side, and reset the compressor 
00052         // for that additional write can be uncompresssed starting at this point.
00053     void flush_read(); // reset decompression engine to be able to read the next block of compressed data 
00054         // if not called, furthur read return EOF
00055     void clean_read(); // discard any byte buffered and not yet returned by read()
00056     void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
00057 
00058     compression get_algo() const { return current_algo; };
00059     void change_algo(compression new_algo, U_I new_compression_level = 9);
00060     
00061         // inherited from generic file   
00062     bool skip(const infinint & position) { flush_write(); flush_read(); clean_read(); return compressed->skip(position); };
00063     bool skip_to_eof()  { flush_write(); flush_read(); clean_read(); return compressed->skip_to_eof(); };
00064     bool skip_relative(S_I x) { flush_write(); flush_read(); clean_read(); return compressed->skip_relative(x); };
00065     infinint get_position() { return compressed->get_position(); };
00066 
00067 protected :
00068     S_I inherited_read(char *a, size_t size) { return (this->*read_ptr)(a, size); };
00069     S_I inherited_write(char *a, size_t size) { return (this->*write_ptr)(a, size); };
00070 
00071 private :
00072     struct xfer 
00073     {
00074         wrapperlib wrap;
00075         char *buffer;
00076         U_I size;
00077 
00078         xfer(U_I sz, wrapperlib_mode mode);
00079         ~xfer();
00080     };
00081     
00082     xfer *compr, *decompr;
00083     generic_file *compressed;
00084     bool compressed_owner;
00085     compression current_algo;
00086 
00087     void init(compression algo, generic_file *compressed_side, U_I compression_level);
00088     void terminate();
00089     S_I (compressor::*read_ptr) (char *a, size_t size);
00090     S_I none_read(char *a, size_t size);
00091     S_I gzip_read(char *a, size_t size);
00092         // S_I zip_read(char *a, size_t size);
00093         // S_I bzip2_read(char *a, size_t size); // using gzip_read, same code thanks to wrapperlib
00094     
00095     S_I (compressor::*write_ptr) (char *a, size_t size);
00096     S_I none_write(char *a, size_t size);
00097     S_I gzip_write(char *a, size_t size);
00098         // S_I zip_write(char *a, size_t size);
00099         // S_I bzip2_write(char *a, size_t size); // using gzip_write, same code thanks to wrapperlib
00100 };
00101 
00102 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines