extratorTXT.cpp 3.48 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"

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

ExtratorTXT::~ExtratorTXT(){    
    listeners->clear();
    delete listeners;
    delete file;
    delete file_io;
    delete bff_reader;
    PRINTL(util::_DEBUG, "ExtratorTXT 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;
        Logging::instance()->writeLog("extratorTXT.cpp <Error>: Arquivo de texto não encontrado.");
        throw ExtratorException("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* line) {
    for(list<ListenerTXT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
        (*it)->notifyLine(line);
    }    
}

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

void ExtratorTXT::setFilePath(char* path){
    filePath = (char*) path; 
    string command1 = "perl -p -e 's/\r$//' < ";
    command1.append(filePath).append(" > ").append(INPUT_TXT);
    system(command1.c_str());

    string command2 = "iconv -f";
    command2.append(" $(file --mime-encoding -b ")
    .append(INPUT_TXT).append(")")
    .append(" -t utf-8 ").append(INPUT_TXT).append(" > ").append(filePath)
    .append(" && rm -f ").append(INPUT_TXT);
    system(command2.c_str());
}

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

void ExtratorTXT::Run(){
    PRINTL(util::_INFO, "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());       
            line_index++;
        }
        }catch (EOFException &ex){
            if(line_index == 0)
                notifyListeners((unsigned char*)"ARQUIVO_INVALIDO");
            hasNext = false;
        }catch (...){
            Logging::instance()->writeLog("extratorTXT.cpp <Error>: Erro durante a leitura do arquivo de texto.");
            throw ExtratorException("Falha desconhecida na leitura do arquivo. Tente novamente."); 
        }
    }
    finish = true;
    notifyEndExtraction(line_index);
}