From aa018e709fc04034f49cd7dc502f254cfa467322 Mon Sep 17 00:00:00 2001 From: Jonnhy Jazeix Date: Fri, 20 Feb 2009 17:42:03 +0000 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20gestion=20des=20couleurs=20po?= =?UTF-8?q?ur=20les=20points=20ou=20les=20virgules.=20Normes=20sur=20rende?= =?UTF-8?q?r=5Fsvg=20et=20main.=20TokenSource=20en=20un=20seul=20fichier?= =?UTF-8?q?=20avec=20macros=20pour=20diff=C3=A9rencier=20windows=20du=20re?= =?UTF-8?q?ste=20du=20monde=20(=C3=A0=20v=C3=A9rifier=20sous=20windows).?= =?UTF-8?q?=20Makefile=20sans=20les=20erreurs=20au=20cas=20o=C3=B9=20les?= =?UTF-8?q?=20fichiers=20n'existent=20pas=20pour=20la=20suppression.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 15 ++- interface/Makefile | 8 +- interface/src/render_svg.cpp | 116 ++++++++-------- interface/src/render_svg.hpp | 50 ++++--- main.cpp | 3 +- parser/src/TokenSource.cpp | 205 ++++++++++++++++++++++++++++- parser/src/TokenSource.hpp | 101 +++++++++++++- parser/src/TokenSource_fstream.cpp | 151 --------------------- parser/src/TokenSource_fstream.hpp | 79 ----------- parser/src/TokenSource_mmap.cpp | 166 ----------------------- parser/src/TokenSource_mmap.hpp | 84 ------------ trace/src/values/Color.cpp | 54 ++++++-- trace/src/values/Color.hpp | 12 ++ 13 files changed, 449 insertions(+), 595 deletions(-) delete mode 100644 parser/src/TokenSource_fstream.cpp delete mode 100644 parser/src/TokenSource_fstream.hpp delete mode 100644 parser/src/TokenSource_mmap.cpp delete mode 100644 parser/src/TokenSource_mmap.hpp diff --git a/Makefile b/Makefile index 179f0af..c1cc05f 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ DIR_TRACE = trace/src all: # @(cd $(DIR_TRACE) && make) # @(cd $(DIR_PARSER) && make) - @(cd $(DIR_INTERFACE) && make) + make -C $(DIR_INTERFACE) @echo " " @echo "Compilation completed! File created in 'bin' folder." @echo " " @@ -35,9 +35,11 @@ doc: debugs: - @(cd $(DIR_INTERFACE) && make debugs) + make debugs -C $(DIR_INTERFACE) +# @(cd $(DIR_INTERFACE) && make debugs) # @(cd $(DIR_TRACE) && make) - @(cd $(DIR_PARSER) && make debug) + make debug -C $(DIR_PARSER) +# @(cd $(DIR_PARSER) && make debug) @echo " " @echo "Compilation completed! (debug)" @echo " " @@ -52,8 +54,9 @@ clean: # Must be called with option -i, otherwise an error occured in the first instructions, the followings won't be executed. wash: - @(cd $(DIR_TRACE) && make clean) - @(cd $(DIR_INTERFACE) && make clean) - @(cd $(DIR_PARSER) && make clean) + make clean -C $(DIR_INTERFACE) + make clean -C $(DIR_TRACE) + make clean -C $(DIR_PARSER) + cd bin && $(RM) * diff --git a/interface/Makefile b/interface/Makefile index 8732cf9..56d81a4 100644 --- a/interface/Makefile +++ b/interface/Makefile @@ -65,7 +65,7 @@ clean: # Must be called with option -i, otherwise an error occured in the first instructions, the followings won't be executed. wash: - cd ./src && rm *~ *.o Makefile qrc_vite.cpp core - cd ./tests && rm *~ *.o Makefile core - cd ./bin && rm *.o - cd ./debug && rm * + cd ./src && $(RM) *~ *.o Makefile qrc_vite.cpp core + cd ./tests && $(RM) *~ *.o Makefile core + cd ./bin && $(RM) *.o + cd ./debug && $(RM) * diff --git a/interface/src/render_svg.cpp b/interface/src/render_svg.cpp index e3e3288..750d57b 100644 --- a/interface/src/render_svg.cpp +++ b/interface/src/render_svg.cpp @@ -1,76 +1,68 @@ #include "render_svg.hpp" +using namespace std; - void Svg::rectangle() - { - _buffer << ""; - afficher(); - } +void Svg::rectangle(){ + _buffer << ""; + afficher(); +} - void Svg::afficher() - { +void Svg::afficher(){ - if (_buffer.str().size()>BUFFER_SIZE){ - svg_file.write(_buffer.str().c_str(), _buffer.str().size()); + if (_buffer.str().size()>BUFFER_SIZE){ + _svg_file.write(_buffer.str().c_str(), _buffer.str().size()); _buffer.flush(); - } + } - } +} - void Svg::init(char * path) - { +void Svg::init(char *path){ - svg_file.open(path , ofstream::out | ofstream::trunc); + _svg_file.open(path , ofstream::out | ofstream::trunc); - if (svg_file.is_open()==false){ + if (_svg_file.is_open()==false){ std::cerr<<"unable to open file"; - } - _buffer << "\n\n\n\t\n\tRectangles\n"; - afficher(); - - } - void Svg::end() - { - _buffer << ""; - svg_file.write(_buffer.str().c_str(), _buffer.str().size()); - _buffer.flush(); - svg_file.close(); - } - void Svg::draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) - { - - - _r=0xff; - _g=0x44; - _b=0xcc; - _w=(unsigned long)w; - _h=(unsigned long)h; - _x=(unsigned long)x; - _y=(unsigned long)y; - rectangle(); - - } - void Svg::draw_state(const Element_pos start , const Element_pos end, const Element_count level, const Element_col r, const Element_col g, const Element_col b) - { - - - _r=(unsigned char)r; - _g=(unsigned char)g; - _b=(unsigned char)b; - _w=(unsigned long)(end-start); - _h=(unsigned long)LEVEL - 10; - _x=(unsigned long)start; - _y=(unsigned long)level*LEVEL; - rectangle(); - - - - } + } + _buffer << "\n\n\n\t\n\tRectangles\n"; + afficher(); +} + +void Svg::end(){ + _buffer << ""; + _svg_file.write(_buffer.str().c_str(), _buffer.str().size()); + _buffer.flush(); + _svg_file.close(); +} +void Svg::draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h){ + + + _r=0xff; + _g=0x44; + _b=0xcc; + _w=(unsigned long)w; + _h=(unsigned long)h; + _x=(unsigned long)x; + _y=(unsigned long)y; + rectangle(); +} + +void Svg::draw_state(const Element_pos start , const Element_pos end, const Element_count level, const Element_col r, const Element_col g, const Element_col b){ + + _r=(unsigned char)r; + _g=(unsigned char)g; + _b=(unsigned char)b; + _w=(unsigned long)(end-start); + _h=(unsigned long)LEVEL - 10; + _x=(unsigned long)start; + _y=(unsigned long)level*LEVEL; + rectangle(); + +} diff --git a/interface/src/render_svg.hpp b/interface/src/render_svg.hpp index 2af8a31..0ba3b67 100644 --- a/interface/src/render_svg.hpp +++ b/interface/src/render_svg.hpp @@ -1,4 +1,4 @@ -#ifndef RENDER_SGV +#ifndef RENDER_SVG #define RENDER_SVG #include #include @@ -6,8 +6,6 @@ #include #define BUFFER_SIZE 2048 #define LEVEL 110 -using namespace std; - typedef unsigned long Element_count ; typedef double Element_pos; @@ -17,44 +15,44 @@ typedef unsigned int Element_col; class Svg{ - private: +private: ostringstream _buffer; - ofstream svg_file; + ofstream _svg_file; unsigned int _r,_g,_b; unsigned long _x,_y,_w,_h; void afficher(); void rectangle(); - public: - /*! +public: + /*! * \brief SVG header buiding */ - void init(char *path); + void init(char *path); - /*! + /*! * \brief SVG bottom file buiding */ - void end(); + void end(); - /*! + /*! * \brief Draw a container according to the parameters * \param x the x position of the container * \param y the y position of the container * \param w the width of the container * \param h the height of the container */ - void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) ; - - /*! - * \brief Draw a state of the trace. - * \param r the red color rate of the state. - * \param g the green color rate of the state. - * \param b the blue color rate of the state. - * \param start the beginning time of the state. - * \param end the ending time of the state. - * \param level refer to the container which state belongs to. - */ - void draw_state(const Element_pos start , const Element_pos end, const Element_count level, const Element_col r, const Element_col g, const Element_col b) ; - }; - -#endif// RENDER_SGV + void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) ; + + /*! + * \brief Draw a state of the trace. + * \param r the red color rate of the state. + * \param g the green color rate of the state. + * \param b the blue color rate of the state. + * \param start the beginning time of the state. + * \param end the ending time of the state. + * \param level refer to the container which state belongs to. + */ + void draw_state(const Element_pos start , const Element_pos end, const Element_count level, const Element_col r, const Element_col g, const Element_col b) ; +}; + +#endif // RENDER_SVG diff --git a/main.cpp b/main.cpp index ce41fd1..c3d50fd 100644 --- a/main.cpp +++ b/main.cpp @@ -13,8 +13,7 @@ /*! *\brief The main function of ViTE. */ - int main(int argc, char **argv) - { + int main(int argc, char **argv) { glutInit(&argc, argv); Interface_console console(argc, argv); diff --git a/parser/src/TokenSource.cpp b/parser/src/TokenSource.cpp index eda4b78..62d38ef 100644 --- a/parser/src/TokenSource.cpp +++ b/parser/src/TokenSource.cpp @@ -1,5 +1,204 @@ -#ifndef MAPPINGDENIED -#include "TokenSource_mmap.cpp" +#include "TokenSource.hpp" + +using namespace std; + +TokenSource::TokenSource(){ + //no file are opened + //you must use open later + _eof = true; +} + + +TokenSource::TokenSource(const char *filename){ + sopen(filename); +} + +TokenSource::~TokenSource(){ +#ifdef WIN32 + _file.close(); #else -#include "TokenSource_fstream.hpp" + if (munmap(_buffer, _filesize) == -1){ + perror("error : munmap"); + } + close(_fd); #endif +} + +void TokenSource::sopen(const char *filename){ + _cursor = 0; + + _eof = false; + +#ifdef WIN32 + _file.open(filename, ios::in); + fill_buffer(); +#else + if ((_fd = open(filename, O_RDONLY)) == -1){ + perror("error : open file"); + exit(-1); + } + + struct stat buf; + if(fstat(_fd, &buf) == -1){ + perror("error status file"); + exit(-1); + } + + _filesize = buf.st_size; + + + + if ((_buffer = (char *)mmap(0, _filesize, PROT_READ, MAP_PRIVATE, _fd, 0)) == MAP_FAILED){ + perror("error mmap"); + exit(-1); + } +#endif +} + +#ifdef WIN32 +bool TokenSource::fill_buffer(){ + if (_file.eof()){ + return false; + } + else{ + _file.read(_buffer, BUFFER_SIZE); + _buffer_size = _file.gcount(); + return true; + } +} +#endif + +bool TokenSource::go_to_next_token(){ + while(_buffer[_cursor] == ' ' || _buffer[_cursor] == '\t'){ + _cursor ++; +#ifdef WIN32 + if (_cursor >= _buffer_size){ + if (!fill_buffer()){ + return true; + } + _cursor = 0; + } +#else + if(ensure_capacity()) + break; +#endif + } + + return false; +} + + +void TokenSource::build_composite_token(){ + _cursor ++; +#ifdef WIN32 + if (_cursor == _buffer_size){ + if (!fill_buffer()){ + cout << "found \" at end of file" << endl; + return; + } + _cursor = 0; + } +#else + if (ensure_capacity()){ + cout << "error :found \" at end of file" << endl; + return; + } +#endif + do{ + _token.push_back(_buffer[_cursor]); + _cursor ++; + + + if (ensure_capacity()){ + break; + } + + } + while(_buffer[_cursor] != '"'); + _cursor ++; + + ensure_capacity(); +} + +void TokenSource::build_simple_token(){ + while(_buffer[_cursor] != ' ' && _buffer[_cursor] != '\n' && _buffer[_cursor] != '\t'){ + _token.push_back(_buffer[_cursor]); + _cursor ++; + + + if (ensure_capacity()){ + break; + } + + } +} + +bool TokenSource::ensure_capacity(){ +#ifdef WIN32 + if (_cursor >= _buffer_size){ + if (!fill_buffer()){ + _eof = true; + return true; + } + _cursor = 0; + } + return false; +#else + if (_cursor < _filesize) + return false; + _eof = true; + return true; +#endif +} + +void TokenSource::build_line_breaks(){ + while(_buffer[_cursor] == '\n'){ + _cursor ++; + if (ensure_capacity()){ + break; + } + } + _token.push_back('\n'); +} + +void TokenSource::build_definition_starter(){ + _cursor ++; + ensure_capacity(); + _token.push_back('%'); + +} + +std::string *TokenSource::lire_token(){ + //regexp recognition + //a token is + //(1) a line break + //(2) composite : a char buffer delimited by \" + //(3) a char buffer delimited by space + + if (!_token.empty()) + _token.clear(); + + if (_eof){ + return NULL; + } + + if(go_to_next_token()) + return NULL; + + if (_buffer[_cursor] == '\n'){ + build_line_breaks(); + } + else if (_buffer[_cursor] == '%'){ + build_definition_starter(); + } + else if (_buffer[_cursor] != '"'){ + build_simple_token(); + } + else{ + build_composite_token(); + } + + + + return &_token; +} diff --git a/parser/src/TokenSource.hpp b/parser/src/TokenSource.hpp index 45cf69d..ffa786d 100644 --- a/parser/src/TokenSource.hpp +++ b/parser/src/TokenSource.hpp @@ -1,12 +1,107 @@ #ifndef TOKENSOURCE_HPP #define TOKENSOURCE_HPP +#include +#include -#ifndef MAPPINGDENIED -#include "TokenSource_mmap.hpp" +#ifdef WIN32 // They do not have mmap + #define BUFFER_SIZE 2048 + #define WORD_SIZE 64 + #include #else -#include "TokenSource_fstream.hpp" + #include + #include + #include + #include + #include + #include + #include #endif +/*! + * \class TokenSource + * \brief supply tokens from a file trace. Tokens are word unit used in PajeFile trace formats. + */ +class TokenSource{ + +private: + +#ifdef WIN32 + char _buffer[BUFFER_SIZE]; +#else + char* _buffer; +#endif + + std::string _token; + int _cursor; + +#ifdef WIN32 + int _buffer_size; + bool _eof; + std::ifstream _file; +#else + off_t _filesize; + bool _eof; + int _fd; +#endif + + +#ifdef WIN32 + bool fill_buffer(); +#endif + + bool go_to_next_token(); + bool ensure_capacity(); + + void build_definition_starter(); + void build_line_breaks(); + void build_composite_token(); + void build_simple_token(); + +public: + + + /*! + * \fn TokenSource(const char* filename); + * \brief Constructor opening a file + * \param Valid filename + */ + TokenSource(const char* filename); + + + + + /*! + * \fn TokenSource + * \brief This constructor needs a file to be openned + */ + TokenSource(); + + + + /*! + * \fn open(const char *filename) + * \brief open a file + * \param valid filename + */ + void sopen(const char *filename); + + + + /*! + * \fn lire_token + * \brief supplies a token + * \return a \\0-ended character string (char*), NULL if the file ended, NULL if no file is opened + */ + std::string* lire_token(); + + + /*! + * \fn ~TokenSource + * \brief Destuctor which release the file + */ + ~TokenSource(); + +}; #endif // TOKENSOURCE_HPP diff --git a/parser/src/TokenSource_fstream.cpp b/parser/src/TokenSource_fstream.cpp deleted file mode 100644 index e5128f9..0000000 --- a/parser/src/TokenSource_fstream.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include "TokenSource.hpp" -using namespace std; - -TokenSource::TokenSource(){ - //no file are opened - //you must use open later - _eof = true; -} - - -TokenSource::TokenSource(const char *filename){ - sopen(filename); -} - -TokenSource::~TokenSource(){ - _file.close(); -} - -void TokenSource::sopen(const char *filename){ - _cursor = 0; - - _eof = false; - _file.open(filename, ios::in); - fill_buffer(); -} - -bool TokenSource::fill_buffer(){ - if (_file.eof()){ - return false; - } - else{ - _file.read(_buffer, BUFFER_SIZE); - _buffer_size = _file.gcount(); - return true; - } -} - -bool TokenSource::go_to_next_token(){ - while(_buffer[_cursor] == ' ' || _buffer[_cursor] == '\t'){ - _cursor ++; - if (_cursor >= _buffer_size){ - if (!fill_buffer()){ - return true; - } - _cursor = 0; - } - } - return false; -} - - -void TokenSource::build_composite_token(){ - _cursor ++; - if (_cursor == _buffer_size){ - if (!fill_buffer()){ - cout << "found \" at end of file" << endl; - return; - } - _cursor = 0; - } - do{ - _token.push_back(_buffer[_cursor]); - _cursor ++; - - - if (ensure_capacity()){ - break; - } - - } - while(_buffer[_cursor] != '"'); - _cursor ++; - - ensure_capacity(); -} - -void TokenSource::build_simple_token(){ - while(_buffer[_cursor] != ' ' && _buffer[_cursor] != '\n' && _buffer[_cursor] != '\t'){ - _token.push_back(_buffer[_cursor]); - _cursor ++; - - - if (ensure_capacity()){ - break; - } - - } -} - -bool TokenSource::ensure_capacity(){ - if (_cursor >= _buffer_size){ - if (!fill_buffer()){ - _eof = true; - return true; - } - _cursor = 0; - } - return false; -} - -void TokenSource::build_line_breaks(){ - while(_buffer[_cursor] == '\n'){ - _cursor ++; - if (ensure_capacity()){ - break; - } - } - _token.push_back('\n'); -} - -void TokenSource::build_definition_starter(){ - _cursor ++; - ensure_capacity(); - _token.push_back('%'); - -} - -std::string *TokenSource::lire_token(){ - //regexp recognition - //a token is - //(1) a line break - //(2) composite : a char buffer delimited by \" - //(3) a char buffer delimited by space - - if (!_token.empty()) - _token.clear(); - - if (_eof){ - return NULL; - } - - if(go_to_next_token()) - return NULL; - - if (_buffer[_cursor] == '\n'){ - build_line_breaks(); - } - else if (_buffer[_cursor] == '%'){ - build_definition_starter(); - } - else if (_buffer[_cursor] != '"'){ - build_simple_token(); - } - else{ - build_composite_token(); - } - - - - return &_token; -} diff --git a/parser/src/TokenSource_fstream.hpp b/parser/src/TokenSource_fstream.hpp deleted file mode 100644 index a9e4e57..0000000 --- a/parser/src/TokenSource_fstream.hpp +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#define BUFFER_SIZE 2048 -#define WORD_SIZE 64 -#include - -/*! - * \class TokenSource - * \brief supply tokens from a file trace. Tokens are word unit used in PajeFile trace formats. - */ -class TokenSource{ - -private: - - char _buffer[BUFFER_SIZE]; - - std::string _token; - int _cursor; - - int _buffer_size; - bool _eof; - std::ifstream _file; - - bool fill_buffer(); - bool go_to_next_token(); - bool ensure_capacity(); - - void build_definition_starter(); - void build_line_breaks(); - void build_composite_token(); - void build_simple_token(); - -public: - - - /*! - * \fn TokenSource(const char* filename); - * \brief Constructor opening a file - * \param Valid filename - */ - TokenSource(const char* filename); - - - - - /*! - * \fn TokenSource - * \brief This constructor needs a file to be openned - */ - TokenSource(); - - - - /*! - * \fn open(const char *filename) - * \brief open a file - * \param valid filename - */ - void sopen(const char *filename); - - - - /*! - * \fn lire_token - * \brief supplies a token - * \return a \\0-ended character string (char*), NULL if the file ended, NULL if no file is opened - */ - std::string* lire_token(); - - - /*! - * \fn ~TokenSource - * \brief Destuctor which release the file - */ - ~TokenSource(); - -}; - - diff --git a/parser/src/TokenSource_mmap.cpp b/parser/src/TokenSource_mmap.cpp deleted file mode 100644 index 0ade7ce..0000000 --- a/parser/src/TokenSource_mmap.cpp +++ /dev/null @@ -1,166 +0,0 @@ - -#include "TokenSource.hpp" - -using namespace std; - - -TokenSource::TokenSource(){ - //no file are opened - //you must use open later - _eof = true; -} - - -TokenSource::TokenSource(const char *filename){ - sopen(filename); -} - - - -TokenSource::~TokenSource(){ - if (munmap(_buffer,_filesize)==-1){ - perror("error : munmap"); - } - - close(_fd); - -} - -void TokenSource::sopen(const char *filename){ - _cursor = 0; - - - - _eof = false; - - if ((_fd=open(filename,O_RDONLY))==-1){ - perror("error : open file"); - exit(-1); - } - - struct stat buf; - if(fstat(_fd,&buf )==-1){ - perror("error status file"); - exit(-1); - } - - _filesize = buf.st_size; - - - - if ((_buffer=(char *)mmap(0,_filesize,PROT_READ,MAP_PRIVATE,_fd,0))==MAP_FAILED){ - perror("error mmap"); - exit(-1); - } - - -} - - - -bool TokenSource::go_to_next_token(){ - while(_buffer[_cursor] == ' ' || _buffer[_cursor] == '\t'){ - _cursor ++; - if(ensure_capacity()) - break; - } - return false; -} - - -void TokenSource::build_composite_token(){ - _cursor ++; - if (ensure_capacity()){ - cout << "error :found \" at end of file" << endl; - return; - } - - - do{ - _token.push_back(_buffer[_cursor]); - _cursor ++; - - - if (ensure_capacity()){ - break; - } - - } - while(_buffer[_cursor] != '"'); - _cursor ++; - - ensure_capacity(); -} - -void TokenSource::build_simple_token(){ - while(_buffer[_cursor] != ' ' && _buffer[_cursor] != '\n' && _buffer[_cursor] != '\t'){ - _token.push_back(_buffer[_cursor]); - _cursor ++; - - - if (ensure_capacity()){ - break; - } - - } -} - -bool TokenSource::ensure_capacity(){ - if (_cursor < _filesize) - return false; - _eof = true; - return true; -} - -void TokenSource::build_line_breaks(){ - while(_buffer[_cursor] == '\n'){ - _cursor ++; - if (ensure_capacity()){ - break; - } - } - _token.push_back('\n'); -} - -void TokenSource::build_definition_starter(){ - _cursor ++; - ensure_capacity(); - _token.push_back('%'); - -} - -std::string *TokenSource::lire_token(){ - //regexp recognition - //a token is - //(1) a line break - //(2) composite : a char buffer delimited by \" - //(3) a char buffer delimited by space - - if (!_token.empty()) - _token.clear(); - - if (_eof){ - return NULL; - } - - if(go_to_next_token()) - return NULL; - - if (_buffer[_cursor] == '\n'){ - build_line_breaks(); - } - else if (_buffer[_cursor] == '%'){ - build_definition_starter(); - } - else if (_buffer[_cursor] != '"'){ - build_simple_token(); - } - else{ - build_composite_token(); - } - - - - return &_token; -} - diff --git a/parser/src/TokenSource_mmap.hpp b/parser/src/TokenSource_mmap.hpp deleted file mode 100644 index 57b34df..0000000 --- a/parser/src/TokenSource_mmap.hpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/*! - * \class TokenSource - * \brief supply tokens from a file trace. Tokens are word unit used in PajeFile trace formats. - */ -class TokenSource{ - -private: - - char* _buffer; - - std::string _token; - int _cursor; - - off_t _filesize; - bool _eof; - int _fd; - - - bool go_to_next_token(); - bool ensure_capacity(); - - void build_definition_starter(); - void build_line_breaks(); - void build_composite_token(); - void build_simple_token(); - -public: - - - /*! - * \fn TokenSource(const char* filename); - * \brief Constructor opening a file - * \param Valid filename - */ - TokenSource(const char* filename); - - - - - /*! - * \fn TokenSource - * \brief This constructor needs a file to be openned - */ - TokenSource(); - - - - /*! - * \fn open(const char *filename) - * \brief open a file - * \param valid filename - */ - void sopen(const char *filename); - - - - /*! - * \fn lire_token - * \brief supplies a token - * \return a \\0-ended character string (char*), NULL if the file ended, NULL if no file is opened - */ - std::string* lire_token(); - - - /*! - * \fn ~TokenSource - * \brief Destuctor which release the file - */ - ~TokenSource(); - -}; - - - diff --git a/trace/src/values/Color.cpp b/trace/src/values/Color.cpp index a1213dd..7ddf854 100644 --- a/trace/src/values/Color.cpp +++ b/trace/src/values/Color.cpp @@ -15,17 +15,39 @@ Color::Color(double r, double g, double b) { bool Color::instantiate(std::string &in, Color &out) { double r, g, b; - size_t position; - while((position = in.find(',')) != -1) { - std::string part1 = in.substr(0,position); - std::string part2 = in.substr(position+1); - in.assign(part1.append(".").append(part2)); + + if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) == 3){ + out = Color(r, g, b); + return true; + } + + // The error could occur because this is not the good format for decimal. + // Dots instead of commas or the contrary + bool commas_first = false; + + if(replace_in_string(in, ',', '.')){ + commas_first = true; + } + + if(commas_first){ // We have dots now + if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) != 3){ + return false; + } + else{ + out = Color(r, g, b); + return true; + } } - if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) != 3){ - return false; + else{ // We had dots initially, we need to change them in commas + replace_in_string(in, '.', ','); + if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) != 3){ + return false; + } + else{ + out = Color(r, g, b); + return true; + } } - out = Color(r, g, b); - return true; } std::string Color::to_string() const { @@ -33,3 +55,17 @@ std::string Color::to_string() const { oss << _r << " " << _g << " " << _b; return oss.str(); } + + +bool Color::replace_in_string(std::string &characters, char to_replace, char replace_by) { + size_t position; + bool has_change = false; + while((position = characters.find(to_replace)) != std::string::npos) { // We had commas + std::string part1 = characters.substr(0, position); + std::string part2 = characters.substr(position+1); + characters = part1 + replace_by + part2; + has_change = true; + } + + return has_change; +} diff --git a/trace/src/values/Color.hpp b/trace/src/values/Color.hpp index d11aa92..39b8c1a 100644 --- a/trace/src/values/Color.hpp +++ b/trace/src/values/Color.hpp @@ -59,6 +59,18 @@ public: */ std::string to_string() const; +private: + /*! + * + * \fn replace_in_string(std::string &characters, char to_replace, char replace_by) const + * \brief Replace in the string the character to_replace by replace_by. + * \param characters string to be replaced. + * \param to_replace the character we want to replace. + * \param replace_by the character we use to replace the first one. + * \return true if there was a change. + * + */ + static bool replace_in_string(std::string &characters, char to_replace, char replace_by); }; -- GitLab