core_plugin.cpp 1.62 KB
//*****************************************************************
/*
	VLibras: Automatic contents translator from Portuguese to LIBRAS

	Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo
	VLibras-Core group at LAViD, Federal University of Paraiba
*/
//*****************************************************************

/**
 * \file corePlugin.cpp
 * \author Leonardo Araújo
 * \date Maio 2015
 */

#include "core_plugin.h"




int coreInitialize()
{
	tradutor = new PyTradutor();
	return 1;
}

const char* coreExecute()
{
	return tradutor->convertStringToGlosa((const char*) get_text_selection());
}

int coreFinalize()
{
	if (!tradutor)
		return -1;
	delete tradutor;
	return 1;	
}

char * get_text_selection()
{

	FILE *comm_file_p = popen("xsel -o ", "r");
    if (!comm_file_p)
    {
        return NULL;
    }
    char line_buff [MAX_BUFFER_SELECTION];
    std::string line_str = "";

    if (comm_file_p == NULL) perror ("Error opening file");
    else
    {
     while ( ! feof (comm_file_p) )
     {
       if ( fgets (line_buff , (int) sizeof(line_buff) , comm_file_p) == NULL ) break;
       line_str.append((std::string) line_buff);
     }
     fclose (comm_file_p);
    }
    remove_special_chars(&line_str, (std::string) REPLACE_CHARACTERS);
    
    return (char*) line_str.c_str();

}

void remove_special_chars(std::string *_word, std::string _chars)
{    
    int index = -1; 
    for (int i = 0; i < _chars.size(); i++)
    {
        while ( (index = (int) _word->find(_chars[i])) != std::string::npos)
        {
            _word->replace(index, 1, " "); // remove chars replacing your position by " "
        }
    }
}