Commit 1129bcc2ff4844b4e0abe14fab9cc9e19fb12284

Authored by Leonardo Domingues
1 parent e04353d6
Exists in master

Remove a dependência da lavidlib e refaz a implementação do main.cpp para teste da libCorePlugin

@@ -3,4 +3,5 @@ @@ -3,4 +3,5 @@
3 *.so 3 *.so
4 *.o 4 *.o
5 *.log 5 *.log
6 -plugin/  
7 \ No newline at end of file 6 \ No newline at end of file
  7 +plugin/
  8 +vlibras_test
@@ -29,9 +29,7 @@ CFLAGS =\ @@ -29,9 +29,7 @@ CFLAGS =\
29 LDFLAGS =\ 29 LDFLAGS =\
30 `python-config --ldflags`\ 30 `python-config --ldflags`\
31 -lX11\ 31 -lX11\
32 - -lcurl\  
33 - -llavid_base\  
34 - -llavid_io 32 + -lcurl
35 33
36 HEADERS =\ 34 HEADERS =\
37 py_tradutor.h\ 35 py_tradutor.h\
Makefile.build
@@ -16,10 +16,10 @@ shared: @@ -16,10 +16,10 @@ shared:
16 16
17 clean: 17 clean:
18 @echo -n Cleaning... 18 @echo -n Cleaning...
19 - @rm -rf $(addprefix $(SRCDIR),$(OBJS)) $(PLUGINDIR) 2> /dev/null && \ 19 + @rm -rf $(addprefix $(SRCDIR),$(OBJS)) $(PLUGINDIR) vlibras_test 2> /dev/null && \
20 echo " $(OK)" || (echo " $(FAILED)"; exit 1;) 20 echo " $(OK)" || (echo " $(FAILED)"; exit 1;)
21 21
22 vlibras_test: 22 vlibras_test:
23 @echo -n Compiling source test file... 23 @echo -n Compiling source test file...
24 - @$(CXX) $(CFLAGS) $(SOURCES) $(TESTSDIR)/main.cpp $(LDFLAGS) -o $(TESTSDIR)/vlibras_test && \ 24 + @$(CXX) $(CFLAGS) $(TESTSDIR)/main.cpp $(LDFLAGS) -o vlibras_test && \
25 echo " $(OK)" || (echo " $(FAILED)"; exit 1;) 25 echo " $(OK)" || (echo " $(FAILED)"; exit 1;)
26 \ No newline at end of file 26 \ No newline at end of file
include/core_plugin.h
@@ -23,7 +23,8 @@ @@ -23,7 +23,8 @@
23 #include <stdlib.h> 23 #include <stdlib.h>
24 #include <unistd.h> 24 #include <unistd.h>
25 #include <sys/types.h> 25 #include <sys/types.h>
26 -#include <lavidlib/base/RuntimeException.h> 26 +//#include <lavidlib/base/RuntimeException.h>
  27 +#include <exception>
27 #include <curl/curl.h> 28 #include <curl/curl.h>
28 29
29 // install libjson 30 // install libjson
include/property.h
@@ -17,26 +17,22 @@ @@ -17,26 +17,22 @@
17 #include <stdlib.h> 17 #include <stdlib.h>
18 #include <string> 18 #include <string>
19 #include <string.h> 19 #include <string.h>
20 - #include <lavidlib/io/BufferedReader.h>  
21 - #include <lavidlib/io/FileIO.h>  
22 - #include <lavidlib/io/IOException.h>  
23 - #include <lavidlib/io/EOFException.h>  
24 - #include <lavidlib/base/RuntimeException.h> 20 + #include <exception>
  21 + #include <fstream>
25 22
26 - using namespace lavidlib; 23 + #define MAX_BUFFER 1024
27 24
28 namespace util { 25 namespace util {
29 26
30 class PropertyHandler { 27 class PropertyHandler {
31 28
32 public: 29 public:
33 - PropertyHandler(std::string filename); 30 + PropertyHandler(std::string _filename);
34 virtual ~PropertyHandler(); 31 virtual ~PropertyHandler();
35 std::string getAttributeValue(std::string attr); 32 std::string getAttributeValue(std::string attr);
36 33
37 private: 34 private:
38 - BufferedReader* rbuffer;  
39 - FileIO* file_property; 35 + std::string filename;
40 int checkFileExtension(std::string &filename); 36 int checkFileExtension(std::string &filename);
41 37
42 }; 38 };
src/core_plugin.cpp
@@ -48,7 +48,8 @@ int coreFinalize() @@ -48,7 +48,8 @@ int coreFinalize()
48 48
49 int coreUpdateInstall_player() 49 int coreUpdateInstall_player()
50 { 50 {
51 - system(SCRIPT_UPDATE_VLIBRAS); 51 + int r = system(SCRIPT_UPDATE_VLIBRAS);
  52 + if (r < 0) printf("%s has returned %d value\n", SCRIPT_UPDATE_VLIBRAS, r);
52 return system("/./opt/vlibras_desktop/script/launcher.sh &"); 53 return system("/./opt/vlibras_desktop/script/launcher.sh &");
53 } 54 }
54 55
@@ -163,32 +164,8 @@ int get_version_from_string(std::string json_message, std::string _key) @@ -163,32 +164,8 @@ int get_version_from_string(std::string json_message, std::string _key)
163 164
164 //@Deprecated 165 //@Deprecated
165 int get_version_from_json(std::string json_message, std::string _key) 166 int get_version_from_json(std::string json_message, std::string _key)
166 -{  
167 - 167 +{
168 return -1; 168 return -1;
169 -  
170 - /*//Code to JsonCpp  
171 - Json::Value parsed_from_str;  
172 - Json::Reader json_reader;  
173 - bool parsing_flag = json_reader.parse(json_message, parsed_from_str);  
174 -  
175 - if (!parsing_flag)  
176 - throw new RuntimeException("Cannot convert the json message to string.");  
177 -  
178 - std::string version_str = parsed_from_str[_key].asString();  
179 -  
180 - // Check if the version of dictionary contains '_' token.  
181 - if ( version_str.find("_") != std::string::npos )  
182 - {  
183 - char *tokens_ptr = strtok ((char*) version_str.c_str(), "_");  
184 - if (tokens_ptr == NULL)  
185 - throw new RuntimeException("Cannot handler the version of dictionary.");  
186 - version_str = (std::string) tokens_ptr;  
187 - }  
188 -  
189 - remove_special_chars(&version_str, ".", (char*) "");  
190 - return atoi(version_str.c_str());  
191 - */  
192 } 169 }
193 170
194 171
@@ -206,13 +183,14 @@ std::string request_current_versions() @@ -206,13 +183,14 @@ std::string request_current_versions()
206 if(curl_ptr) 183 if(curl_ptr)
207 { 184 {
208 std::string url_endpoint = property_handler_ptr->getAttributeValue(ENDPOINT_UPDATE_ID); 185 std::string url_endpoint = property_handler_ptr->getAttributeValue(ENDPOINT_UPDATE_ID);
  186 + printf("url_endpoint -> %s\n", url_endpoint.c_str());
209 187
210 std::stringstream ss_convert; 188 std::stringstream ss_convert;
211 ss_convert << dict_current_version; 189 ss_convert << dict_current_version;
212 url_endpoint.append(ss_convert.str()); 190 url_endpoint.append(ss_convert.str());
213 191
214 if (url_endpoint.size() <= 0) 192 if (url_endpoint.size() <= 0)
215 - throw new RuntimeException("The URL for this Endpoint is not available on the file config."); 193 + throw "The URL for this Endpoint is not available on the file config.";
216 194
217 curl_easy_setopt(curl_ptr, CURLOPT_URL, (char*) url_endpoint.c_str()); 195 curl_easy_setopt(curl_ptr, CURLOPT_URL, (char*) url_endpoint.c_str());
218 curl_easy_setopt(curl_ptr, CURLOPT_WRITEFUNCTION, writeDataCallback); 196 curl_easy_setopt(curl_ptr, CURLOPT_WRITEFUNCTION, writeDataCallback);
src/property.cpp
@@ -16,57 +16,58 @@ @@ -16,57 +16,58 @@
16 namespace util { 16 namespace util {
17 17
18 18
19 - PropertyHandler::PropertyHandler(std::string filename) {  
20 - try {  
21 - if (checkFileExtension(filename) != 1)  
22 - throw new RuntimeException("Format file not is recognized!");  
23 - file_property = new FileIO(filename, FileIO::MODE_READ);  
24 - } catch (lavidlib::IOException &ex) {  
25 - printf("[FAILED] Cannot open file %s.\n%s\n", filename.c_str(),  
26 - ex.getMessage().c_str());  
27 - } 19 + PropertyHandler::PropertyHandler(std::string _filename)
  20 + {
  21 + if (checkFileExtension(_filename) != 1)
  22 + throw "Format file not is recognized";
  23 +
  24 + filename = _filename;
28 } 25 }
29 26
30 27
31 - PropertyHandler::~PropertyHandler() {  
32 - if (rbuffer)  
33 - delete rbuffer;  
34 - if (file_property)  
35 - delete file_property; 28 + PropertyHandler::~PropertyHandler()
  29 + {
  30 + //todo
36 } 31 }
37 32
38 33
39 - std::string PropertyHandler::getAttributeValue(std::string attr) {  
40 -  
41 - rbuffer = new BufferedReader(file_property);  
42 - std::string fline; 34 + std::string PropertyHandler::getAttributeValue(std::string attr)
  35 + {
  36 + std::ifstream file_property(filename.c_str());
  37 + std::string buffer_str;
43 try { 38 try {
44 int target_pos = -1, begin = 0; 39 int target_pos = -1, begin = 0;
45 std::string attr_t, value_t; 40 std::string attr_t, value_t;
46 41
47 - while((fline = rbuffer->readLine()).size() > 0) {  
48 - target_pos = fline.find("="); 42 + while (std::getline(file_property, buffer_str))
  43 + {
  44 + target_pos = buffer_str.find("=");
49 if (target_pos < 2) 45 if (target_pos < 2)
50 - throw new RuntimeException("The assignment symbol was not found."); 46 + throw "The assignment symbol was not found.";
51 47
52 - attr_t = fline.substr(begin, target_pos); 48 + attr_t = buffer_str.substr(begin, target_pos);
53 begin = target_pos+1; 49 begin = target_pos+1;
54 - value_t = fline.substr(begin, fline.size());  
55 - if (attr.compare(attr_t) == 0) {  
56 - file_property->seek(0);  
57 - return value_t; 50 + value_t = buffer_str.substr(begin, buffer_str.size());
  51 + if (attr.compare(attr_t) == 0)
  52 + {
  53 + file_property.seekg (0, file_property.beg);
  54 + return value_t;
58 } 55 }
59 target_pos = -1; 56 target_pos = -1;
60 begin = 0; 57 begin = 0;
61 } 58 }
62 - } catch (lavidlib::EOFException &ex) {  
63 - printf("[INFO] File is completed.\n%s\n", ex.getMessage().c_str()); 59 + }
  60 + catch (std::exception& ex)
  61 + {
  62 + printf("[Exception] File is completed\n[Error] %s\n", ex.what());
64 } 63 }
65 - file_property->seek(0);  
66 - //FIXME: throw new exception  
67 - return fline; 64 + file_property.seekg (0, file_property.beg);
  65 +
  66 + return buffer_str;
  67 +
68 } 68 }
69 69
  70 +
70 #define EXTENSION ".conf" 71 #define EXTENSION ".conf"
71 72
72 int PropertyHandler::checkFileExtension(std::string &filename) { 73 int PropertyHandler::checkFileExtension(std::string &filename) {
@@ -9,70 +9,58 @@ @@ -9,70 +9,58 @@
9 9
10 /** 10 /**
11 * \file main.cpp 11 * \file main.cpp
12 - * \author Gustavo Sobral, Leonardo Araújo  
13 - * \date Janeiro 2015 12 + * \author Leonardo Araújo
  13 + * \date Outubro 2015
14 */ 14 */
15 15
16 -#include <core_plugin.h>  
17 - 16 +#include <stdlib.h>
  17 +#include <dlfcn.h>
18 #include <iostream> 18 #include <iostream>
19 #include <fstream> 19 #include <fstream>
20 #include <string> 20 #include <string>
21 21
22 int main() 22 int main()
23 { 23 {
24 -  
25 - if (coreInitialize() != 1)  
26 - {  
27 - printf("Cannot initialize the CorePlugin.\n");  
28 - exit(-1);  
29 - }  
30 24
31 - int result_check = coreUpdateCheck();  
32 - switch (result_check) 25 + void *vlibras_lib_handler;
  26 + const char *dlsym_error_ptr;
  27 +
  28 + vlibras_lib_handler = dlopen("plugin/libCorePlugin.so", RTLD_LAZY);
  29 + if (!vlibras_lib_handler)
33 { 30 {
34 - case 0:  
35 - printf("The VLibras is up-to-date!\n");  
36 - break;  
37 - case 1:  
38 - printf("The VLibras is outdated! Upgrading...\n");  
39 - break;  
40 - case 2:  
41 - printf("Only the dictionary is outdated! Upgrading...\n");  
42 - break;  
43 - case -1:  
44 - printf("Error!\n");  
45 - break;  
46 - default:  
47 - printf("Option not found! Try again.\n"); 31 + fprintf(stderr, "%s\n", dlerror());
  32 + exit(1);
48 } 33 }
49 -  
50 - // Inicializa o tradutor  
51 - //PyTradutor *tradutorTest;  
52 - //tradutorTest = new PyTradutor();  
53 -  
54 - // Pega o conteúdo do clipboard e salva em `pasteboardContent`  
55 - //std::string pasteboardContent = Pasteboard::getGeneralContent();  
56 - // Traduz o conteúdo do clipboard em `pasteboardContent` para a string `traducao`  
57 - //char* str = new char[10];  
58 - //strcpy(str, "o gato comeu o rato");  
59 - //std::string traducao = tradutorTest->convertStringToGlosa(str);  
60 -  
61 - // [LOG] Exibe o conteudo do buffer de clipboard em `pasteboardContent` e traduz para glosa salvando seu resultado em `traducao`  
62 - //std::cout << "[[ Conteudo em memoria ]]\n" << str << std::endl;  
63 - //std::cout << "[[ Tradução ]]\n" << traducao << std::endl;  
64 34
65 - //std::cout << "\n##### ##### #####" << std::endl; 35 + typedef void (*coreplugin_t)();
  36 + dlerror();
  37 +
  38 + coreplugin_t core_init_f = (coreplugin_t) dlsym(vlibras_lib_handler, "coreInitialize");
  39 + dlsym_error_ptr = dlerror();
  40 + if (dlsym_error_ptr)
  41 + {
  42 + printf("Cannot load symbol 'coreInitialize': %s\n", dlsym_error_ptr);
  43 + dlclose(vlibras_lib_handler);
  44 + return 1;
  45 + }
  46 +
  47 + /* Test coreInitialize function */
  48 + core_init_f();
  49 +
  50 + coreplugin_t core_update_f = (coreplugin_t) dlsym(vlibras_lib_handler, "coreUpdateCheck");
  51 + dlsym_error_ptr = dlerror();
  52 + if (dlsym_error_ptr)
  53 + {
  54 + printf("Cannot load symbol 'coreUpdateCheck': %s\n", dlsym_error_ptr);
  55 + dlclose(vlibras_lib_handler);
  56 + return 1;
  57 + }
66 58
67 - // Inicializa o plugin  
68 - //std::cout << "[[Initialize]] | " << coreInitialize() << std::endl;  
69 - // Executa o CorePlugin  
70 - //std::cout << "[[Execute]] | " << coreExecute() << std::endl;  
71 - // Finaliza o ambiente  
72 - //std::cout << "[[Finalize]] | " << coreFinalize() << std::endl; 59 + /* Test coreUpdateCheck function */
  60 + core_update_f();
73 61
74 - // Exclui o que foi utilizado  
75 - // delete tradutorTest; 62 + /* Close load library */
  63 + dlclose(vlibras_lib_handler);
76 64
77 return 0; 65 return 0;
78 } 66 }
79 \ No newline at end of file 67 \ No newline at end of file