extratorTXT.cpp 3.06 KB
/***************************************************************************
 *   Universidade Federal da Paraíba                                       *
 *   Copyright (C) 2014 by Laboratório de Aplicações de Vídeo Digital      *
 *                                                                         *
 *   Centro de Informática - UFPB - Campus I                               *
 *   João Pessoa - PB - Brasil                                             *
 *                                                                         *
 *   Author: Erickson Silva (erickson.silva@lavid.ufpb.br)                 *
 *                                                                         *
 **************************************************************************/
 
#include "extratorTXT.h"

using namespace std;

ExtratorTXT::ExtratorTXT(){
    listeners = new list<ListenerTXT*>();
    finish = false;
    DPRINTF("Done!\n");
}

ExtratorTXT::~ExtratorTXT(){    
    listeners->clear();
    delete listeners;
    delete file;
    delete file_io;
    delete bff_reader;
    DDDPRINTF("Extractor Text finalized!\n");        
}

void ExtratorTXT::initialize(){
    file = new lavidlib::File(filepath);
    try{
        file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ);
        bff_reader = new BufferedReader(file_io);
    }catch(Exception &ex){ 
        finish = true;
        Util::Logger::Instance()->writeLog((char*) "[ERRO: extratorTXT.cpp] Arquivo de texto não encontrado.");
        throw ExtratorTxtException("Falha ao abrir o arquivo de texto! Verifique se o mesmo existe.");
    }     
    this->Start();
}

void ExtratorTXT::addListener(ListenerTXT* listener){
    listeners->push_back(listener);
}

void ExtratorTXT::notifyListeners(unsigned char* subtitle) {
    for(list<ListenerTXT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
        (*it)->notificaTradutor(subtitle);
    }    
}

void ExtratorTXT::notifyEndExtraction(int sub_size) {
    DDPRINTF("Extrator Text concluiu a extração: %d linhas.\n", sub_size);
    for(list<ListenerTXT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
        (*it)->notifyEnd(sub_size);
    }
}

void ExtratorTXT::setFilePath(char* path){
    filepath = (char*) path;
}

bool ExtratorTXT::isFinished(){
    return finish;
}

void ExtratorTXT::Run(){

    DDPRINTF("[AGUARDE] Extraindo Texto...\n")

	int line_index = 0;
    bool hasNext = true;   
    string line;

    while (hasNext) {
        try{
        line = bff_reader->readLine();
        if (line.length() > 0){ 
            notifyListeners((unsigned char*) line.c_str());
            cout << " . ";        
            line_index++;
        }
        }catch (EOFException &ex){
            hasNext = false;
        }catch (...){
            Util::Logger::Instance()->writeLog((char*) "[ERRO: extratorTXT.cpp] Erro durante a leitura do arquivo de texto.");
            throw ExtratorTxtException("Falha desconhecida na leitura do arquivo. Tente novamente."); 
        }
    }
    printf("\n");
    finish = true;
    notifyEndExtraction(line_index);
}