Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/projet_lzw/include/inputstream.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 "inputstream.h"
#include <ostream>
#include <iostream>
#include <fstream>
#include "deflater.h"

/* Constructeurs avec le flux en parametre: on attribue alors la valeur de la reference */
DeflaterInputStream::DeflaterInputStream(std::ostream& a):sortie(a) {}
DeflaterInputStream::DeflaterInputStream(std::ostream& a , Inflater& b):sortie(a) {}


DeflaterInputStream::~DeflaterInputStream()
{
// deconstructeur 
}


// Definition de l operateur <<  pour ajouter des donnees
DeflaterInputStream& DeflaterInputStream::operator<<(std::vector<unsigned int> &a )
{
    /* Recopie du buffer a dans la suite du buffer d entree */
    for  (int i=0;i<a.size();i++)
    {
	buffer.push_back(a[i]);
    }
    comp.setInput(buffer);
    comp.inflate(1); // Methode 1: attende d une suite ... */

    /* Recopie de la sortie decompresee dans le flux de sortie */
    for(int i=0;i<comp.decompression.size();i++)
    {
	sortie.put(comp.decompression[i]);
    }

    buffer.clear();    
    return *this;
}





// Fonction de fin  
DeflaterInputStream& DeflaterInputStream::end()
{
    // Appel de decompression finale (tout le buffer va etre traite)
    comp.inflate(0);
    // Recopie de la decompression dans le buffer de sortie
    for(int i=0;i<comp.decompression.size();i++)
    {
	sortie.put(comp.decompression[i]);	
    }
    buffer.clear();    
    return *this;
}


// Fonction pour traiter tout un fichier
int DeflaterInputStream::DeflateFromFile(char* lect)
{
   std::ifstream fichier;
   // Ouverture
    fichier.open (lect,std::ifstream::binary);
    fichier.seekg(0,fichier.end);
    // Calcul de la taille
    int size= fichier.tellg();
    int total_size=size;
    fichier.seekg(0,fichier.beg);
    int a_lire=0;
    
    int elt_size = sizeof(unsigned int);
    int pas=500*elt_size; // On definie un pas de 500 elements a lire
    unsigned int *temp = new unsigned int[1];
    int compteur=0;
    float pourcent;        

	// Tant qu il reste des elements a traiter
	while(fichier.good() and size>=elt_size)
	{
	    a_lire = (size-pas>=0 ? pas : size);
	    // Calcul du pourcentage fini 
	    pourcent =  (float)compteur/total_size*100;
	    
	    std::cout << "\033[1ADecompression en cours: \033[1m[";
    	    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 ( reste " << size << " bits  )  \n";
	    comp.setInput(buffer);
	    // Appel de la fonction de decompression temporaire
	    comp.inflate(1);
	    for(int i=0;i<comp.decompression.size();i++)
	      {
		sortie.put(comp.decompression[i]);	
	      }
	    buffer.clear(); 
	    
	    for (int i=0;i<a_lire/elt_size;i++) {
			    fichier.read((char*)temp,sizeof(unsigned int)); 
	                    buffer.push_back(temp[0]);
			    }
	    size-=a_lire;
	    compteur+=a_lire;
	}

	
	if (!buffer.empty())
	{
	    // Decompression finale
	    comp.setInput(buffer);
	    comp.inflate(0);
	    for(int i=0;i<comp.decompression.size();i++)
	      {
		sortie.put(comp.decompression[i]);	
	      }
	    buffer.clear();
	}	    
        std::cout << "\033[1A\033[KDecompression finie !  \n";	

   comp.inflate(0);
    for(int i=0;i<comp.decompression.size();i++)
    {
	sortie.put(comp.decompression[i]);	
    }
    buffer.clear();
    fichier.close();
}