Commit 40535e79fc2e27ca8b920429951a8860d4cfb754
1 parent
1562909b
Exists in
master
and in
1 other branch
Adicionado a API jsoncpp
Showing
10 changed files
with
45 additions
and
355 deletions
Show diff stats
Makefile
| @@ -9,6 +9,8 @@ FLAGS=-g | @@ -9,6 +9,8 @@ FLAGS=-g | ||
| 9 | LIBS= \ | 9 | LIBS= \ |
| 10 | `pkg-config --cflags jlibcpp` \ | 10 | `pkg-config --cflags jlibcpp` \ |
| 11 | `pkg-config --libs jlibcpp` \ | 11 | `pkg-config --libs jlibcpp` \ |
| 12 | + `pkg-config --cflags jsoncpp` \ | ||
| 13 | + `pkg-config --libs jsoncpp` \ | ||
| 12 | -lpython2.7 -llavid_base -llavid_io | 14 | -lpython2.7 -llavid_base -llavid_io |
| 13 | 15 | ||
| 14 | INCLUDES= \ | 16 | INCLUDES= \ |
| @@ -24,8 +26,7 @@ INCLUDES= \ | @@ -24,8 +26,7 @@ INCLUDES= \ | ||
| 24 | #-I libs/jthread/include -I libs/jcommon/include -I libs/jsocket/include -Wall | 26 | #-I libs/jthread/include -I libs/jcommon/include -I libs/jsocket/include -Wall |
| 25 | 27 | ||
| 26 | utilObjs= \ | 28 | utilObjs= \ |
| 27 | - logger.o \ | ||
| 28 | - property.o | 29 | + logger.o |
| 29 | 30 | ||
| 30 | tradutorObjs = \ | 31 | tradutorObjs = \ |
| 31 | pyTradutor.o \ | 32 | pyTradutor.o \ |
mixer/src/include/Mixer.h
| @@ -17,7 +17,6 @@ | @@ -17,7 +17,6 @@ | ||
| 17 | #include <string> | 17 | #include <string> |
| 18 | #include <fstream> | 18 | #include <fstream> |
| 19 | #include "dprintf.h" | 19 | #include "dprintf.h" |
| 20 | -#include "property.h" | ||
| 21 | 20 | ||
| 22 | //SL Video Position | 21 | //SL Video Position |
| 23 | #define TOP_LEFT 1 | 22 | #define TOP_LEFT 1 |
| @@ -80,10 +79,7 @@ private: | @@ -80,10 +79,7 @@ private: | ||
| 80 | char* uploads; | 79 | char* uploads; |
| 81 | int positionSecondaryVideo; | 80 | int positionSecondaryVideo; |
| 82 | double widthSecondaryVideo, heightSecondaryVideo; | 81 | double widthSecondaryVideo, heightSecondaryVideo; |
| 83 | - int transparency; | ||
| 84 | - | ||
| 85 | - PropertyHandler* reader; | ||
| 86 | - | 82 | + int transparency; |
| 87 | }; | 83 | }; |
| 88 | 84 | ||
| 89 | #endif /* MIXER_H */ | 85 | #endif /* MIXER_H */ |
servico/src/include/serviceWindowGeneration.h
| @@ -7,13 +7,13 @@ | @@ -7,13 +7,13 @@ | ||
| 7 | #include <locale> | 7 | #include <locale> |
| 8 | #include "jthread.h" | 8 | #include "jthread.h" |
| 9 | #include "dprintf.h" | 9 | #include "dprintf.h" |
| 10 | -#include "property.h" | ||
| 11 | #include "Mixer.h" | 10 | #include "Mixer.h" |
| 12 | #include "synchronizer.h" | 11 | #include "synchronizer.h" |
| 13 | #include "listenerSynchronizer.h" | 12 | #include "listenerSynchronizer.h" |
| 14 | #include "listenerTradutor.h" | 13 | #include "listenerTradutor.h" |
| 15 | #include "tradutorPortGlosa.h" | 14 | #include "tradutorPortGlosa.h" |
| 16 | #include "serviceException.h" | 15 | #include "serviceException.h" |
| 16 | +#include <json/json.h> | ||
| 17 | #include <lavidlib/base/RuntimeException.h> | 17 | #include <lavidlib/base/RuntimeException.h> |
| 18 | 18 | ||
| 19 | #define DEVELOPER "devel" | 19 | #define DEVELOPER "devel" |
| @@ -25,9 +25,10 @@ | @@ -25,9 +25,10 @@ | ||
| 25 | #define PATH_DEVEL "vlibras_user/vlibras-contents/videos/" | 25 | #define PATH_DEVEL "vlibras_user/vlibras-contents/videos/" |
| 26 | #define PATH_DEVEL_UPLOADS "vlibras_user/vlibras-contents/uploads/" | 26 | #define PATH_DEVEL_UPLOADS "vlibras_user/vlibras-contents/uploads/" |
| 27 | #define PATH_VBOX_UPLOADS "vlibras_user/.vlibras-conf/uploads/" | 27 | #define PATH_VBOX_UPLOADS "vlibras_user/.vlibras-conf/uploads/" |
| 28 | -#define PATH_CONF_FILE "vlibras_user/.vlibras-conf/configuration/params-vbox.conf" | 28 | +#define PATH_CONF_FILE "vlibras_user/.vlibras-conf/param.json" |
| 29 | #define MAX_SIZE_PATH 256 | 29 | #define MAX_SIZE_PATH 256 |
| 30 | 30 | ||
| 31 | +using namespace Json; | ||
| 31 | using namespace Tradutor; | 32 | using namespace Tradutor; |
| 32 | using namespace jthread; | 33 | using namespace jthread; |
| 33 | using namespace std; | 34 | using namespace std; |
| @@ -37,13 +38,15 @@ class ServiceWindowGeneration { | @@ -37,13 +38,15 @@ class ServiceWindowGeneration { | ||
| 37 | protected: | 38 | protected: |
| 38 | TradutorPortGlosa* tradutor; | 39 | TradutorPortGlosa* tradutor; |
| 39 | Synchronizer* sincronizador; | 40 | Synchronizer* sincronizador; |
| 40 | - PropertyHandler* ppty_h; | ||
| 41 | Mixer* mixer; | 41 | Mixer* mixer; |
| 42 | 42 | ||
| 43 | + Value root; | ||
| 44 | + Reader reader; | ||
| 43 | vector<int64_t>* vetor_pts; | 45 | vector<int64_t>* vetor_pts; |
| 44 | 46 | ||
| 45 | bool finish; | 47 | bool finish; |
| 46 | bool running; | 48 | bool running; |
| 49 | + bool parsingSuccessful; | ||
| 47 | 50 | ||
| 48 | char* path_input; | 51 | char* path_input; |
| 49 | char* path_libras; | 52 | char* path_libras; |
servico/src/serviceWindowGenerationFromRec.cpp
| @@ -44,7 +44,6 @@ ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec( | @@ -44,7 +44,6 @@ ServiceWindowGenerationFromRec::ServiceWindowGenerationFromRec( | ||
| 44 | 44 | ||
| 45 | ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){ | 45 | ServiceWindowGenerationFromRec::~ServiceWindowGenerationFromRec(){ |
| 46 | free(vetor_pts); | 46 | free(vetor_pts); |
| 47 | - delete(ppty_h); | ||
| 48 | if (tradutor) delete tradutor; | 47 | if (tradutor) delete tradutor; |
| 49 | if (rec) delete rec; | 48 | if (rec) delete rec; |
| 50 | if (sincronizador) delete sincronizador; | 49 | if (sincronizador) delete sincronizador; |
| @@ -58,16 +57,18 @@ void ServiceWindowGenerationFromRec::setPathContents(){ | @@ -58,16 +57,18 @@ void ServiceWindowGenerationFromRec::setPathContents(){ | ||
| 58 | this->path_uploads = PATH_DEVEL_UPLOADS; | 57 | this->path_uploads = PATH_DEVEL_UPLOADS; |
| 59 | rec->setPathAudioContents(path_uploads); | 58 | rec->setPathAudioContents(path_uploads); |
| 60 | }else if(strcmp(client_type, PRODUCTION) == 0){ | 59 | }else if(strcmp(client_type, PRODUCTION) == 0){ |
| 61 | - try{ | ||
| 62 | - ppty_h = new PropertyHandler(PATH_CONF_FILE, PropertyHandler::READ); | ||
| 63 | - this->path_contents = (char*) ppty_h->get_attibute_value("STORAGE"); | ||
| 64 | - }catch(RuntimeException &ex){ | ||
| 65 | - printf("%s\n", ex.getMessage().c_str()); | ||
| 66 | - } | ||
| 67 | - this->path_uploads = PATH_VBOX_UPLOADS; | 60 | + ifstream conf_file(PATH_CONF_FILE, ifstream::binary); |
| 61 | + parsingSuccessful = reader.parse(conf_file, root); | ||
| 62 | + if(!parsingSuccessful){ | ||
| 63 | + throw ServiceException("Fail to parsing param.json"); | ||
| 64 | + } | ||
| 65 | + string attr = root.get("storage", PATH_VBOX_UPLOADS).asString(); | ||
| 66 | + this->path_contents = new char[MAX_SIZE_PATH]; | ||
| 67 | + strcpy(this->path_contents, attr.c_str()); | ||
| 68 | + this->path_uploads = PATH_VBOX_UPLOADS; | ||
| 68 | rec->setPathAudioContents(path_uploads); | 69 | rec->setPathAudioContents(path_uploads); |
| 69 | }else{ | 70 | }else{ |
| 70 | - throw new ServiceException("Invalid client!"); | 71 | + throw ServiceException("Invalid client!"); |
| 71 | } | 72 | } |
| 72 | } | 73 | } |
| 73 | 74 |
servico/src/serviceWindowGenerationFromSRT.cpp
| @@ -43,7 +43,6 @@ ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* pathSRT, in | @@ -43,7 +43,6 @@ ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(char* pathSRT, in | ||
| 43 | 43 | ||
| 44 | ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() { | 44 | ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() { |
| 45 | free(vetor_pts); | 45 | free(vetor_pts); |
| 46 | - delete(ppty_h); | ||
| 47 | if (mixer) delete mixer; | 46 | if (mixer) delete mixer; |
| 48 | if (tradutor) delete tradutor; | 47 | if (tradutor) delete tradutor; |
| 49 | if (sincronizador) delete sincronizador; | 48 | if (sincronizador) delete sincronizador; |
| @@ -54,19 +53,21 @@ ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() { | @@ -54,19 +53,21 @@ ServiceWindowGenerationFromSRT::~ServiceWindowGenerationFromSRT() { | ||
| 54 | 53 | ||
| 55 | void ServiceWindowGenerationFromSRT::setPathContents() { | 54 | void ServiceWindowGenerationFromSRT::setPathContents() { |
| 56 | if(strcmp(client_type,DEVELOPER) == 0){ | 55 | if(strcmp(client_type,DEVELOPER) == 0){ |
| 57 | - this->path_contents = PATH_DEVEL; | ||
| 58 | - this->path_uploads = PATH_DEVEL_UPLOADS; | ||
| 59 | - }else if(strcmp(client_type, PRODUCTION) == 0){ | ||
| 60 | - try{ | ||
| 61 | - ppty_h = new PropertyHandler(PATH_CONF_FILE, PropertyHandler::READ); | ||
| 62 | - this->path_contents = (char*) ppty_h->get_attibute_value("STORAGE"); | ||
| 63 | - }catch(RuntimeException &ex){ | ||
| 64 | - printf("%s\n", ex.getMessage().c_str()); | ||
| 65 | - } | ||
| 66 | - this->path_uploads = PATH_VBOX_UPLOADS; | ||
| 67 | - }else{ | ||
| 68 | - throw new ServiceException("Invalid client!"); | ||
| 69 | - } | 56 | + this->path_contents = PATH_DEVEL; |
| 57 | + this->path_uploads = PATH_DEVEL_UPLOADS; | ||
| 58 | + }else if(strcmp(client_type, PRODUCTION) == 0){ | ||
| 59 | + ifstream conf_file(PATH_CONF_FILE, ifstream::binary); | ||
| 60 | + parsingSuccessful = reader.parse(conf_file, root); | ||
| 61 | + if(!parsingSuccessful){ | ||
| 62 | + throw ServiceException("Fail to parsing param.json"); | ||
| 63 | + } | ||
| 64 | + string attr = root.get("storage", PATH_VBOX_UPLOADS).asString(); | ||
| 65 | + this->path_contents = new char[MAX_SIZE_PATH]; | ||
| 66 | + strcpy(this->path_contents, attr.c_str()); | ||
| 67 | + this->path_uploads = PATH_VBOX_UPLOADS; | ||
| 68 | + }else{ | ||
| 69 | + throw ServiceException("Invalid client!"); | ||
| 70 | + } | ||
| 70 | } | 71 | } |
| 71 | 72 | ||
| 72 | void ServiceWindowGenerationFromSRT::setPathLibras() { | 73 | void ServiceWindowGenerationFromSRT::setPathLibras() { |
servico/src/serviceWindowGenerationFromText.cpp
| @@ -20,7 +20,6 @@ ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, | @@ -20,7 +20,6 @@ ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, | ||
| 20 | 20 | ||
| 21 | ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() { | 21 | ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() { |
| 22 | free(vetor_pts); | 22 | free(vetor_pts); |
| 23 | - delete(ppty_h); | ||
| 24 | if (tradutor) delete tradutor; | 23 | if (tradutor) delete tradutor; |
| 25 | if (sincronizador) delete sincronizador; | 24 | if (sincronizador) delete sincronizador; |
| 26 | if (extratorTXT)delete extratorTXT; | 25 | if (extratorTXT)delete extratorTXT; |
| @@ -29,19 +28,21 @@ ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() { | @@ -29,19 +28,21 @@ ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() { | ||
| 29 | } | 28 | } |
| 30 | 29 | ||
| 31 | void ServiceWindowGenerationFromText::setPathContents() { | 30 | void ServiceWindowGenerationFromText::setPathContents() { |
| 32 | - if(strcmp(client_type,DEVELOPER) == 0){ | 31 | + if(strcmp(client_type,DEVELOPER) == 0){ |
| 33 | this->path_contents = PATH_DEVEL; | 32 | this->path_contents = PATH_DEVEL; |
| 34 | - this->path_uploads = PATH_DEVEL_UPLOADS; | 33 | + this->path_uploads = PATH_DEVEL_UPLOADS; |
| 35 | }else if(strcmp(client_type, PRODUCTION) == 0){ | 34 | }else if(strcmp(client_type, PRODUCTION) == 0){ |
| 36 | - try{ | ||
| 37 | - ppty_h = new PropertyHandler(PATH_CONF_FILE, PropertyHandler::READ); | ||
| 38 | - this->path_contents = (char*) ppty_h->get_attibute_value("STORAGE"); | ||
| 39 | - }catch(RuntimeException &ex){ | ||
| 40 | - printf("%s\n", ex.getMessage().c_str()); | 35 | + ifstream conf_file(PATH_CONF_FILE, ifstream::binary); |
| 36 | + parsingSuccessful = reader.parse(conf_file, root); | ||
| 37 | + if(!parsingSuccessful){ | ||
| 38 | + throw ServiceException("Fail to parsing param.json"); | ||
| 41 | } | 39 | } |
| 40 | + string attr = root.get("storage", PATH_VBOX_UPLOADS).asString(); | ||
| 41 | + this->path_contents = new char[MAX_SIZE_PATH]; | ||
| 42 | + strcpy(this->path_contents, attr.c_str()); | ||
| 42 | this->path_uploads = PATH_VBOX_UPLOADS; | 43 | this->path_uploads = PATH_VBOX_UPLOADS; |
| 43 | }else{ | 44 | }else{ |
| 44 | - throw new ServiceException("Invalid client!"); | 45 | + throw ServiceException("Invalid client!"); |
| 45 | } | 46 | } |
| 46 | } | 47 | } |
| 47 | 48 |
util/src/include/logger.h
| @@ -17,7 +17,6 @@ | @@ -17,7 +17,6 @@ | ||
| 17 | #include <stddef.h> | 17 | #include <stddef.h> |
| 18 | #include <iostream> | 18 | #include <iostream> |
| 19 | #include <ctime> | 19 | #include <ctime> |
| 20 | -#include "property.h" | ||
| 21 | //#include <lavidlib/utils/Logger.h> | 20 | //#include <lavidlib/utils/Logger.h> |
| 22 | 21 | ||
| 23 | 22 | ||
| @@ -32,9 +31,7 @@ namespace Util { | @@ -32,9 +31,7 @@ namespace Util { | ||
| 32 | public: | 31 | public: |
| 33 | static Logger* Instance(); | 32 | static Logger* Instance(); |
| 34 | void openLogFile(); | 33 | void openLogFile(); |
| 35 | - void openInfoLogFile(char* filepath); | ||
| 36 | void writeLog(char* log); | 34 | void writeLog(char* log); |
| 37 | - void writeInfoLog(char* filepath ,char* infoLog); | ||
| 38 | void closeLogFile(); | 35 | void closeLogFile(); |
| 39 | char* getTime(); | 36 | char* getTime(); |
| 40 | 37 |
util/src/include/property.h
| @@ -1,65 +0,0 @@ | @@ -1,65 +0,0 @@ | ||
| 1 | -/*************************************************************************** | ||
| 2 | - * Universidade Federal da Paraíba * | ||
| 3 | - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * | ||
| 4 | - * * | ||
| 5 | - * Centro de Informática - UFPB - Campus I * | ||
| 6 | - * João Pessoa - PB - Brasil * | ||
| 7 | - * * | ||
| 8 | - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * | ||
| 9 | - * Date: Mon Jun 1 19:29:50 BRT 2015 * | ||
| 10 | - * * | ||
| 11 | - **************************************************************************/ | ||
| 12 | - | ||
| 13 | - #ifndef PROPERTYHANDLER_H | ||
| 14 | - #define PROPERTYHANDLER_H | ||
| 15 | - | ||
| 16 | - #include <stdio.h> | ||
| 17 | - #include <stdlib.h> | ||
| 18 | - #include <map> | ||
| 19 | - #include <string> | ||
| 20 | - #include <string.h> | ||
| 21 | - #include <lavidlib/io/File.h> | ||
| 22 | - #include <lavidlib/io/FileIO.h> | ||
| 23 | - #include <lavidlib/io/BufferedReader.h> | ||
| 24 | - #include <lavidlib/io/IOException.h> | ||
| 25 | - #include <lavidlib/io/EOFException.h> | ||
| 26 | - #include <lavidlib/base/RuntimeException.h> | ||
| 27 | - | ||
| 28 | - using namespace lavidlib; | ||
| 29 | - | ||
| 30 | - class PropertyHandler { | ||
| 31 | - public: | ||
| 32 | - enum HandlerMode { | ||
| 33 | - READ, | ||
| 34 | - WRITE | ||
| 35 | - }; | ||
| 36 | - PropertyHandler(char* _filename, PropertyHandler::HandlerMode _handler_mode); | ||
| 37 | - virtual ~PropertyHandler(); | ||
| 38 | - | ||
| 39 | - int attr_exists(char* _attr); | ||
| 40 | - const char* get_attibute_value(char* _attr); | ||
| 41 | - void set_attribute_value(char* _attr_p, char* _value_p); | ||
| 42 | - void update_properties(); | ||
| 43 | - int remove_attribute(char* _attr_p); | ||
| 44 | - | ||
| 45 | - private: | ||
| 46 | - | ||
| 47 | - std::string filename; | ||
| 48 | - File* file_p; | ||
| 49 | - FileIO* file_io_p; | ||
| 50 | - BufferedReader* buff_reader_p; | ||
| 51 | - HandlerMode handler_mode; | ||
| 52 | - | ||
| 53 | - std::map<std::string, std::string> * map_ppty_p; | ||
| 54 | - | ||
| 55 | - int check_file_extension(char* _filename); | ||
| 56 | - void format_line_str(std::string* _line_str); | ||
| 57 | - int load_properties(); | ||
| 58 | - void init_write_mode(); | ||
| 59 | - std::string get_key(std::string _ppty_line); | ||
| 60 | - std::string get_value(std::string _ppty_line); | ||
| 61 | - int write_properties_on_file(std::string _key, std::string _value, FILE* _file_output); | ||
| 62 | - }; | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - #endif /* PROPERTYHANDLER_H */ | ||
| 66 | \ No newline at end of file | 0 | \ No newline at end of file |
util/src/logger.cpp
| @@ -30,9 +30,6 @@ namespace Util { | @@ -30,9 +30,6 @@ namespace Util { | ||
| 30 | file.open("vlibras_user/vlibras-core/log/log", ios_base::app); | 30 | file.open("vlibras_user/vlibras-core/log/log", ios_base::app); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | - void Logger::openInfoLogFile(char* filepath){ | ||
| 34 | - file.open(filepath , ios_base::trunc); | ||
| 35 | - } | ||
| 36 | 33 | ||
| 37 | void Logger::closeLogFile(){ | 34 | void Logger::closeLogFile(){ |
| 38 | file.close(); | 35 | file.close(); |
| @@ -51,10 +48,4 @@ namespace Util { | @@ -51,10 +48,4 @@ namespace Util { | ||
| 51 | this->closeLogFile(); | 48 | this->closeLogFile(); |
| 52 | } | 49 | } |
| 53 | 50 | ||
| 54 | - void Logger::writeInfoLog(char* filepath ,char* infoLog){ | ||
| 55 | - this->openInfoLogFile(filepath); | ||
| 56 | - file << "READY="; | ||
| 57 | - file << infoLog << "\n\r"; | ||
| 58 | - this->closeLogFile(); | ||
| 59 | - } | ||
| 60 | } | 51 | } |
util/src/property.cpp
| @@ -1,236 +0,0 @@ | @@ -1,236 +0,0 @@ | ||
| 1 | -/*************************************************************************** | ||
| 2 | - * Universidade Federal da Paraíba * | ||
| 3 | - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * | ||
| 4 | - * * | ||
| 5 | - * Centro de Informática - UFPB - Campus I * | ||
| 6 | - * João Pessoa - PB - Brasil * | ||
| 7 | - * * | ||
| 8 | - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * | ||
| 9 | - * Date: Mon Jun 1 19:29:50 BRT 2015 * | ||
| 10 | - * * | ||
| 11 | - **************************************************************************/ | ||
| 12 | - | ||
| 13 | -#include "property.h" | ||
| 14 | - | ||
| 15 | - | ||
| 16 | -PropertyHandler::PropertyHandler(char* _filename, PropertyHandler::HandlerMode _handler_mode) | ||
| 17 | -{ | ||
| 18 | - if ( check_file_extension(_filename) == -1 ) | ||
| 19 | - throw new RuntimeException("Format file not is supported"); | ||
| 20 | - | ||
| 21 | - filename = (std::string) _filename; | ||
| 22 | - handler_mode = _handler_mode; | ||
| 23 | - | ||
| 24 | - switch (handler_mode) | ||
| 25 | - { | ||
| 26 | - case READ: | ||
| 27 | - if ( load_properties() <= 0 ) | ||
| 28 | - throw new RuntimeException("Cannot to load properties of the file"); | ||
| 29 | - break; | ||
| 30 | - case WRITE: | ||
| 31 | - init_write_mode(); | ||
| 32 | - break; | ||
| 33 | - default: | ||
| 34 | - printf("Handler mode not supported"); | ||
| 35 | - exit(1); | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | -} | ||
| 39 | - | ||
| 40 | - | ||
| 41 | -PropertyHandler::~PropertyHandler() | ||
| 42 | -{ | ||
| 43 | - if (handler_mode == WRITE) | ||
| 44 | - update_properties(); | ||
| 45 | - | ||
| 46 | - if (buff_reader_p) delete buff_reader_p; | ||
| 47 | - if (file_io_p) delete file_io_p; | ||
| 48 | - if (file_p) delete file_p; | ||
| 49 | - if (map_ppty_p) delete map_ppty_p; | ||
| 50 | - | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | - | ||
| 54 | -int PropertyHandler::load_properties() | ||
| 55 | -{ | ||
| 56 | - std::string buff_str; | ||
| 57 | - int buff_index = 0; | ||
| 58 | - | ||
| 59 | - file_p = new File(filename); | ||
| 60 | - try { | ||
| 61 | - file_io_p = new FileIO(file_p->getPath(), FileIO::MODE_READ); | ||
| 62 | - } | ||
| 63 | - catch(IOException &ex) { | ||
| 64 | - printf("[ERROR] %s\n", ex.getMessage().c_str()); | ||
| 65 | - exit(1); | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - buff_reader_p = new BufferedReader(file_io_p); | ||
| 69 | - map_ppty_p = new std::map<std::string, std::string>(); | ||
| 70 | - | ||
| 71 | - try { | ||
| 72 | - buff_str = buff_reader_p->readLine(); | ||
| 73 | - | ||
| 74 | - while (buff_str.size() > 0) | ||
| 75 | - { | ||
| 76 | - buff_index++; | ||
| 77 | - format_line_str(&buff_str); | ||
| 78 | - | ||
| 79 | - map_ppty_p->insert(map_ppty_p->begin(), | ||
| 80 | - (std::pair<std::string, std::string>(get_key(buff_str), get_value(buff_str)))); | ||
| 81 | - | ||
| 82 | - /* get next line (key, value) */ | ||
| 83 | - buff_str = buff_reader_p->readLine(); | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - } catch (EOFException &ex) { | ||
| 87 | - printf("[ERROR] %s\n", ex.getMessage().c_str()); | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - return buff_index; | ||
| 91 | - | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | - | ||
| 95 | -void PropertyHandler::init_write_mode() | ||
| 96 | -{ | ||
| 97 | - /* init map structure to manipulate the properties in WRITE mode */ | ||
| 98 | - map_ppty_p = new std::map<std::string, std::string>(); | ||
| 99 | -} | ||
| 100 | - | ||
| 101 | - | ||
| 102 | -int PropertyHandler::attr_exists(char* _attr) | ||
| 103 | -{ | ||
| 104 | - std::map<std::string, std::string>::iterator it; | ||
| 105 | - if ( (it = map_ppty_p->find((const char*) _attr)) != map_ppty_p->end() ) | ||
| 106 | - return 1; | ||
| 107 | - return 0; | ||
| 108 | -} | ||
| 109 | - | ||
| 110 | -const char* PropertyHandler::get_attibute_value(char* _attr) | ||
| 111 | -{ | ||
| 112 | - if (attr_exists(_attr)) | ||
| 113 | - { | ||
| 114 | - return (map_ppty_p->find((const char*) _attr)->second).c_str(); | ||
| 115 | - } | ||
| 116 | - return _attr; | ||
| 117 | -} | ||
| 118 | - | ||
| 119 | - | ||
| 120 | -void PropertyHandler::set_attribute_value(char* _attr_p, char* _value_p) | ||
| 121 | -{ | ||
| 122 | - if (handler_mode == PropertyHandler::READ) | ||
| 123 | - throw new RuntimeException("Cannot set properties in the READ mode"); | ||
| 124 | - | ||
| 125 | - if (attr_exists(_attr_p)) | ||
| 126 | - throw new RuntimeException("This attribute already exists"); | ||
| 127 | - | ||
| 128 | - map_ppty_p->insert(map_ppty_p->begin(), | ||
| 129 | - (std::pair<std::string, std::string>((std::string) _attr_p, (std::string) _value_p))); | ||
| 130 | -} | ||
| 131 | - | ||
| 132 | - | ||
| 133 | -void PropertyHandler::update_properties() | ||
| 134 | -{ | ||
| 135 | - FILE* file_output_p; | ||
| 136 | - file_output_p = fopen (filename.c_str(), "w"); | ||
| 137 | - if (file_output_p == NULL) | ||
| 138 | - throw new IOException("Cannot open file to write properties"); | ||
| 139 | - fseek(file_output_p, 0, SEEK_SET); | ||
| 140 | - | ||
| 141 | - std::string str_properties; | ||
| 142 | - | ||
| 143 | - std::map<std::string, std::string>::iterator it; | ||
| 144 | - for (it = map_ppty_p->begin(); it != map_ppty_p->end(); it++) | ||
| 145 | - if ( write_properties_on_file(it->first, it->second, file_output_p) == 0 ) | ||
| 146 | - throw new IOException("Error to write properties in the file"); | ||
| 147 | - | ||
| 148 | - fclose (file_output_p); | ||
| 149 | -} | ||
| 150 | - | ||
| 151 | - | ||
| 152 | -int PropertyHandler::write_properties_on_file(std::string _key, std::string _value, FILE* _file_output) | ||
| 153 | -{ | ||
| 154 | - std::string str_properties = _key; | ||
| 155 | - str_properties.append("="); | ||
| 156 | - str_properties.append(_value); | ||
| 157 | - | ||
| 158 | - int count_bytes = 0; | ||
| 159 | - | ||
| 160 | - count_bytes += fputs((char*) str_properties.c_str(), _file_output); | ||
| 161 | - count_bytes += fputs("\n", _file_output); | ||
| 162 | - | ||
| 163 | - return count_bytes; | ||
| 164 | -} | ||
| 165 | - | ||
| 166 | -//TODO | ||
| 167 | -int PropertyHandler::remove_attribute(char* _attr_p) | ||
| 168 | -{ | ||
| 169 | - | ||
| 170 | - if (attr_exists(_attr_p)) { | ||
| 171 | - | ||
| 172 | - if ( map_ppty_p->erase((const char*) _attr_p) != 1 ) | ||
| 173 | - throw new RuntimeException("Cannot remove attribute of the map structure"); | ||
| 174 | - | ||
| 175 | - /* if delete the attribute corretly, update the map of the properties */ | ||
| 176 | - update_properties(); | ||
| 177 | - return 1; | ||
| 178 | - } | ||
| 179 | - return 0; | ||
| 180 | -} | ||
| 181 | - | ||
| 182 | - | ||
| 183 | -#define CONF_FILE_EXTENSION ".conf" | ||
| 184 | -/** | ||
| 185 | - Verify if the file extension is valid. | ||
| 186 | -*/ | ||
| 187 | -int PropertyHandler::check_file_extension(char* _filename) | ||
| 188 | -{ | ||
| 189 | - return ((std::string) _filename).find(CONF_FILE_EXTENSION); | ||
| 190 | -} | ||
| 191 | - | ||
| 192 | -#undef CONF_FILE_EXTENSION /* .conf */ | ||
| 193 | - | ||
| 194 | - | ||
| 195 | -#define CHAR_TO_REMOVE " \t" | ||
| 196 | -/** | ||
| 197 | - Remove all whitespaces in the text line. | ||
| 198 | -*/ | ||
| 199 | -void PropertyHandler::format_line_str(std::string* _line_str) | ||
| 200 | -{ | ||
| 201 | - int index = -1; | ||
| 202 | - std::string chars_rm_str = (std::string) CHAR_TO_REMOVE; | ||
| 203 | - for (int i = 0; i < chars_rm_str.size(); i++) | ||
| 204 | - { | ||
| 205 | - while ( (index = (int) _line_str->find(chars_rm_str[i]) ) != std::string::npos) | ||
| 206 | - { | ||
| 207 | - _line_str->replace(index, 1, ""); // remove whitespaces (" ") replacing your position by "" | ||
| 208 | - } | ||
| 209 | - } | ||
| 210 | -} | ||
| 211 | -#undef CHAR_TO_REMOVE | ||
| 212 | - | ||
| 213 | -#define TOKEN_ATTR "=" | ||
| 214 | -std::string PropertyHandler::get_key(std::string _ppty_line) | ||
| 215 | -{ | ||
| 216 | - int target_pos = -1; | ||
| 217 | - target_pos = _ppty_line.find(TOKEN_ATTR); | ||
| 218 | - | ||
| 219 | - if (target_pos < 1) /* minimum lenght of a key (k=value) */ | ||
| 220 | - throw new RuntimeException("Bad format sentence"); | ||
| 221 | - | ||
| 222 | - return (std::string) _ppty_line.substr(0, target_pos); | ||
| 223 | -} | ||
| 224 | - | ||
| 225 | - | ||
| 226 | -std::string PropertyHandler::get_value(std::string _ppty_line) | ||
| 227 | -{ | ||
| 228 | - int target_pos = -1; | ||
| 229 | - target_pos = _ppty_line.find(TOKEN_ATTR); | ||
| 230 | - | ||
| 231 | - if (target_pos < 1) /* minimum lenght of a key (k=value) */ | ||
| 232 | - throw new RuntimeException("Bad format sentence"); | ||
| 233 | - | ||
| 234 | - return (std::string) _ppty_line.substr(target_pos+1, _ppty_line.size()); | ||
| 235 | -} | ||
| 236 | -#undef TOKEN_ATTR | ||
| 237 | \ No newline at end of file | 0 | \ No newline at end of file |