Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/projet_lzw/include/outputstream.cpp

(Deskargatu)

/*
    This file is part of projet_lzw_univ_pau.

    projet_lzw_univ_pau is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    projet_lzw_univ_pau 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with projet_lzw_univ_pau; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "outputstream.h"
#include <ostream>
#include <iostream>
#include <fstream>
#include "deflater.h"

// Constructeur avec recopie de reference */
DeflaterOutputStream::DeflaterOutputStream(std::ostream& a):sortie(a) {}
DeflaterOutputStream::DeflaterOutputStream(std::ostream& a , Deflater& b):sortie(a) {}


DeflaterOutputStream::~DeflaterOutputStream()
{
// Destructeur
}


// Ajout de parametres manuellement
DeflaterOutputStream& DeflaterOutputStream::operator<<(std::vector<char> &a )
{
    comp.setInput(a);
    // Compression temporaire
    comp.deflate(1);
    // Recopie de la compression dans le flux de sortie
    for(int i=0;i<comp.compression.size();i++)
    {
    sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
    }    
    return *this;
}




// Ajout de parametres a partir d un string (meme procede qu avec un vecteur de char)
DeflaterOutputStream& DeflaterOutputStream::operator<<(const std::string &a) 
{
    buffer.clear();    
    for  (int i=0;i<a.size();i++)  { buffer.push_back(a[i]);   }
    comp.setInput(buffer);
    comp.deflate(1);
    for(int i=0;i<comp.compression.size();i++)
    {
    sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
    }
    return *this;

}

// Fin de compression
DeflaterOutputStream& DeflaterOutputStream::end()
{
    // Compression finale
    comp.deflate(0);
    // Recopie dans le flux de sortie
    for(int i=0;i<comp.compression.size();i++)
    {
    sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
    }    
    buffer.clear();    
    return *this;
}


// compression a partir d un fichier source
int DeflaterOutputStream::DeflateFromFile(char* fichier)
{
    std::ifstream entree;
    
    entree.open (fichier,std::ifstream::binary);    
    std::cout << "Ouverture du fichier K\n";    
    entree.seekg(0,entree.end);
    int size= entree.tellg();
    int total_size= size;
    std::cout << "Lecture du fichier de taille: " << size << "\n";
    entree.seekg(0,entree.beg);
    float pourcent;

    int a_lire=0;
    // Lire avec un pas de 5000 caracteres
    int pas=5000;
    int compteur=0;
    std::vector<char> buff;
    char* temp = new char[pas];
    
    // Tant qu il reste des elements a traiter */
    while(entree.good() and size>0)
    {
	a_lire = size-pas>=0? pas : size;
	pourcent = (float)compteur/total_size*100;
	std::cout <<  "\033[1A[";
	for(int i=0;i<pourcent/2;i++) { std::cout << "#"; }
	for(int i=0;i<(100-pourcent)/2;i++) { std::cout << "-"; }
	std::cout <<  "] " << pourcent << "%\033[0m . (" << size << " left)\n";
        comp.setInput(buff);
	// On compresse le buffer (temporaire)
	comp.deflate(1);
	// Recopie de la compression dans le flux de sortie
	for(int i=0;i<comp.compression.size();i++)
	{
	sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
	}    
	buff.clear();
	
	// Calcul du nombre d elements
	entree.read(temp,a_lire);
	for (int i=0;i<a_lire;i++) { buff.push_back(temp[i]); }
	compteur+=a_lire;
	size-=a_lire;
    }
    if (!buff.empty())
    {
	// Compression finale
	comp.setInput(buff);
	comp.deflate(0);
	// Recopie dans le flux de sortie
	for(int i=0;i<comp.compression.size();i++)
	{
	sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
	}    
	
	buff.clear();
    }
        comp.setInput(buff);
	comp.deflate(0);
	for(int i=0;i<comp.compression.size();i++)
	{
	sortie.write((const char*)&comp.compression[i],sizeof(unsigned int));	
	}    
    
    std::cout << "\033[1A\033[KCompression en cours: \033[1m100%\033[0m . ( il reste 0 bits )  \n";    
    entree.close();
}