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 2004/11/07 18:21:37 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         void change_algo(compression new_algo, U_I new_compression_level = 9);
00077 
00078             // inherited from generic file
00079         bool skip(const infinint & position) { flush_write(); flush_read(); clean_read(); return compressed->skip(position); };
00080         bool skip_to_eof()  { flush_write(); flush_read(); clean_read(); return compressed->skip_to_eof(); };
00081         bool skip_relative(S_I x) { flush_write(); flush_read(); clean_read(); return compressed->skip_relative(x); };
00082         infinint get_position() { return compressed->get_position(); };
00083 
00084     protected :
00085         S_I inherited_read(char *a, size_t size) { return (this->*read_ptr)(a, size); };
00086         S_I inherited_write(char *a, size_t size) { return (this->*write_ptr)(a, size); };
00087 
00088     private :
00089         struct xfer
00090         {
00091             wrapperlib wrap;
00092             char *buffer;
00093             U_I size;
00094 
00095             xfer(U_I sz, wrapperlib_mode mode);
00096             ~xfer();
00097         };
00098 
00099         xfer *compr, *decompr;
00100         generic_file *compressed;
00101         bool compressed_owner;
00102         compression current_algo;
00103 
00104         void init(compression algo, generic_file *compressed_side, U_I compression_level);
00105         void terminate();
00106         S_I (compressor::*read_ptr) (char *a, size_t size);
00107         S_I none_read(char *a, size_t size);
00108         S_I gzip_read(char *a, size_t size);
00109             // S_I zip_read(char *a, size_t size);
00110             // S_I bzip2_read(char *a, size_t size); // using gzip_read, same code thanks to wrapperlib
00111 
00112         S_I (compressor::*write_ptr) (char *a, size_t size);
00113         S_I none_write(char *a, size_t size);
00114         S_I gzip_write(char *a, size_t size);
00115             // S_I zip_write(char *a, size_t size);
00116             // S_I bzip2_write(char *a, size_t size); // using gzip_write, same code thanks to wrapperlib
00117     };
00118 
00119 } // end of namespace
00120 
00121 #endif

Generated on Mon Aug 14 13:23:03 2006 for Disk ARchive by  doxygen 1.4.7