inputFile.cpp 1.54 KB
#include <stdio.h>
#include <list>
#include <stdlib.h>

#include "inputFile.h"
#include "ouvinteInput.h"

InputFile::InputFile(char* path) {
    this->path = path;
    this->ouvintes = new list<OuvinteInput*>();
    finished = false;
    DPRINTF("Done!\n");    
}

InputFile::~InputFile(){
    ouvintes->clear();
    delete ouvintes;
    DDDPRINTF("Input finalized!\n");    
}

void InputFile::initialize(){
    ifstream filein(path, ifstream::binary);
    
    if (filein.is_open()) {
        
        char buffer [MAX_SIZE_PACKET];
        DPRINTF("[AGUARDE] Lendo arquivo...\n")
        while (!filein.eof()) {
            filein.read(buffer, MAX_SIZE_PACKET);
            unsigned char* packet = (unsigned char*) buffer;
            notificaOuvintes(packet);
        }
        
        printf("\n");
        filein.close();
        finished = true;
        
    } else {
		finished = true;
        Util::Logger::Instance()->writeLog((char*) "[ERRO: inputFile.cpp] Arquivo de vídeo não encontrado.");
        throw InputException("Falha ao abrir o arquivo de vídeo! Verifique se o mesmo existe.");
    }
    
}

bool InputFile::isFinished(){
    return this->finished;
}

void InputFile::registraOuvinte(OuvinteInput* ouvinte){
    this->ouvintes->push_back(ouvinte);
}

void InputFile::removeOuvinte(OuvinteInput* ouvinte) {
    this->ouvintes->remove(ouvinte);
}

void InputFile::notificaOuvintes(unsigned char* pacote){  
    for(list<OuvinteInput*>::iterator it = this->ouvintes->begin(); it != this->ouvintes->end(); it++){
        (*it)->chegouInput(pacote);
    }
}