reader_srt.cpp 3.61 KB
/***************************************************************************
 *   Universidade Federal da Paraíba                                       *
 *   Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital      *
 *                                                                         *
 *   Centro de Informática - UFPB - Campus I                               *
 *   João Pessoa - PB - Brasil                                             *
 *                                                                         *
 *   Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br)  *
 *   Date: Qui Out 24 22:26:35 BRT 2013                                    *
 *                                                                         *
 **************************************************************************/
 
 #include "reader_srt.h"
 
 namespace sndesc {

 
 	ReaderSRT::ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode) {

 		file = new lavidlib::File(_filepath);
 		try{
	 		file_io = new lavidlib::FileIO(file->getPath(), _mode);
 		}catch(lavidlib::IOException){
 			throw ReaderException("[ERRO: reader_srt.cpp] Arquivo de legenda não encontrado.");
 		}
 		seek_pos = 0;
 		hasNextSub = true;

 	}

 	
 	ReaderSRT::~ReaderSRT() {

 		if (bff_reader != NULL) delete bff_reader;
 		if (file_io != NULL) delete file_io;

 	}
 	
 	
 	Subtitle* ReaderSRT::next() {
 		
 		if (seek_pos >= file_io->getSize())
 			throw ReaderException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido.");
 		
  		file_io->seek(seek_pos);
  		try{
	 		bff_reader = new BufferedReader(file_io);
  		}catch(lavidlib::IOException){
 			throw ReaderException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado.");
  		}

 		Subtitle* sub = new Subtitle();
 		std::string line = ""; 		
 		std::string text_sub = "";
 		
 		try {
 			/* ID */
	 		int id = 0;
	 		line = bff_reader->readLine();
	 		seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
	 		id = atoi(line.c_str()); 		
	 		sub->setID(id);

	 		/* TimeIn and TimeOut */
	 		int64_t t_in = 0, t_out = 0;
	 		line = bff_reader->readLine();
	 		seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
	 		
	 		int target_pos = line.find(TARGET_TIME);
	 		t_in = str_to_time(line.substr(0, target_pos));
	 		sub->setTimeIn(t_in);
	 		t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size()));
	 		sub->setTimeOut(t_out); 		
 		
 			/* Text: read until line be empty */	 		
	 		while ((line = bff_reader->readLine()).size() > 0) {
	 			text_sub += line;
	 			text_sub.append(" ");	 			
	 		}
	 		seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
 		
 		} catch (lavidlib::EOFException &ex) {
 			sub->setSubtitleText(text_sub);
	 		sub->setStatusOfReady(true);
	 		delete(bff_reader);
	 		seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
	 		hasNextSub = false;
 			return sub;
 		}
 		
 		sub->setSubtitleText(text_sub);
 		sub->setStatusOfReady(true);
 		delete(bff_reader);
 		return sub;

 	}


 	bool ReaderSRT::hasNextSubtitle() {
 		return hasNextSub;
 	}


 	int64_t ReaderSRT::str_to_time(std::string str_time) {

 		int64_t ttime = 0;
 		char* tokens = new char[4]; // hh, mm, ss, ms
    	strcpy(tokens, (char*)str_time.c_str());

    	int index = 0;
    	int values [4]; // hh, mm, ss, ms
 		char * str = strtok(tokens, ":,"); 		
	    while (str != NULL) {
	    	values[index] = atoi(str);
	        str = strtok(NULL, ":,");
	        index++;
	    }
	    delete(tokens);

	    /* calculate time */
	    ttime = /*hour to sec*/((((values[0] * 60) * 60) + 
	    	/*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3];

	    return ttime;

 	}

 }