From 441f570d41a8ec81a1aa571b84b7d1dd3c2ed120 Mon Sep 17 00:00:00 2001 From: Jonnhy Jazeix <jazeix@gmail.com> Date: Fri, 6 Mar 2009 15:34:15 +0000 Subject: [PATCH] Ajout de corrections d'erreurs. Modif du doxyfile pour qu'il prenne en compte le bon main. Ajout de la precision lors du to_string() des valeurs (avant ca tronquait mais la valeur stockee etait la bonne). Ajout de la partie raccourcis dans le manuel user. --- Doxyfile | 4 +- src/interface/interface_console.cpp | 11 ++- src/parser/Errors.cpp | 87 ++++++++--------- src/parser/Errors.hpp | 34 ++++--- src/parser/ParserDefinitionDecoder.cpp | 32 ++++--- src/parser/ParserEventDecoder.cpp | 32 +++---- src/parser/ParserPaje.cpp | 31 +++--- src/trace/values/Color.cpp | 126 ++++++++++++------------- src/trace/values/Date.cpp | 10 +- src/trace/values/Date.hpp | 2 +- src/trace/values/Double.cpp | 2 +- src/trace/values/Hex.cpp | 2 +- src/trace/values/Integer.cpp | 2 +- src/trace/values/Value.hpp | 6 ++ 14 files changed, 198 insertions(+), 183 deletions(-) diff --git a/Doxyfile b/Doxyfile index 301904d9..3645cc44 100644 --- a/Doxyfile +++ b/Doxyfile @@ -31,7 +31,7 @@ PROJECT_NAME = ViTE # This could be handy for archiving the generated documentation or # if some version control system is used. -PROJECT_NUMBER = 1.0 +PROJECT_NUMBER = 2.0 # The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) # base path where the generated documentation will be put. @@ -513,7 +513,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = src/interface/ src/message/ src/parser/ src/trace/ src/trace/values/ main.cpp main_resource.hpp tests/interface tests/message tests/parser tests/trace +INPUT = src/interface/ src/message/ src/parser/ src/trace/ src/trace/values/ src/main.cpp src/main_resource.hpp tests/interface tests/message tests/parser tests/trace # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is diff --git a/src/interface/interface_console.cpp b/src/interface/interface_console.cpp index 21e7f3fe..8f9fd763 100644 --- a/src/interface/interface_console.cpp +++ b/src/interface/interface_console.cpp @@ -84,9 +84,14 @@ bool Interface_console::draw_trace(const string & filename, Render_area* render_ ParserPaje parser; DrawTrace<Render_area> drawing; - - parser.parse(filename, trace); - + try{ + parser.parse(filename, trace); + } + catch (const string &error){ + //message << "unable to parse " << filename << ende; + message << "Reason : " << error << ende; + return false; + } drawing.build(render_area, &trace); return true; diff --git a/src/parser/Errors.cpp b/src/parser/Errors.cpp index 98b2acd7..dbe4c186 100644 --- a/src/parser/Errors.cpp +++ b/src/parser/Errors.cpp @@ -18,80 +18,71 @@ const string Error::_EMPTY_DEF = "a definition line is empty"; const string Error::_INCOMPATIBLE_VALUE_IN_EVENT = "incompatible value : "; const string Error::_UNKNOWN_TYPE_IN_EVENT = "unknown type : "; const string Error::_UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND = "unknown field for the event. Should have been found on the definition parser : "; +const string Error::_UNKNOWN_LINE = "the content of the line is unknown, must quit"; +const string Error::_BAD_FILE_EXTENSION = "the extension of the file is not .trace"; +const string Error::_LINE_TOO_LONG_EVENT = "extra token(s)"; string Error::_content = ""; -void Error::set_error(string kind_of_error, unsigned int line_number){ - //_number_of_errors ++; - char line[10]; - sprintf(line, "%d", line_number); - Error::_content = kind_of_error + " on line " + line; - Error::_errors.push(Error::_content); +void Error::set(const string kind_of_error, const int priority){ + Error::_content = kind_of_error; + switch(priority){ + case _WARNING: + Error::_warnings.push(Error::_content); + break; + default: // Include the _ERROR + Error::_errors.push(Error::_content); + break; + } } -void Error::set_warning(string kind_of_error, unsigned int line_number){ - //_number_of_warnings ++; - char line[10]; - sprintf(line, "%d", line_number); - Error::_content = kind_of_error + " on line " + line; - Error::_warnings.push(Error::_content); +void Error::set_and_print(const string kind_of_error, const int priority){ + set(kind_of_error, priority); + print(priority); } -void Error::set_and_print_error(string kind_of_error, unsigned int line_number){ - set_error(kind_of_error, line_number); - print_error(); +void Error::set_and_print(const string kind_of_error, const unsigned int line_number, const int priority){ + char line[10]; + sprintf(line, "%d", line_number); + set(kind_of_error + " on line " + line, priority); + print(priority); } -void Error::set_and_print_warning(string kind_of_error, unsigned int line_number){ - set_warning(kind_of_error, line_number); - print_warning(); -} -bool Error::set_error_if(bool condition, string kind_of_error, unsigned int line_number){ +bool Error::set_if(bool condition, const string kind_of_error, const unsigned int line_number, const int priority){ if(condition){ - set_error(kind_of_error, line_number); + char line[10]; + sprintf(line, "%d", line_number); + set(kind_of_error + " on line " + line, priority); return true; } return false; } -bool Error::set_warning_if(bool condition, string kind_of_error, unsigned int line_number){ - if(condition){ - set_warning(kind_of_error, line_number); - return true; - } - return false; -} - -void Error::print_error(){ - message << _content << ende; -} -void Error::print_warning(){ - message << _content << endw; -} - -bool Error::set_and_print_error_if(bool condition, string kind_of_error, unsigned int line_number){ - if(condition){ - set_error(kind_of_error, line_number); - print_error(); - return true; +void Error::print(const int priority){ + message << _content ; + switch(priority){ + case _WARNING: + message << endw; + break; + default: // Include the _ERROR + message << ende; + break; } - return false; } -bool Error::set_and_print_warning_if(bool condition, string kind_of_error, unsigned int line_number){ - if(condition){ - set_warning(kind_of_error, line_number); - print_warning(); +bool Error::set_and_print_if(bool condition, const string kind_of_error, const unsigned int line_number, const int priority){ + if(set_if(condition, kind_of_error, line_number, priority)){ + print(priority); return true; } return false; } -void Error::print_numbers_of_warning_and_errors(){ - message << "Your trace has " << Error::_errors.size() << " errors and " << Error::_warnings.size() << " warnings."; +void Error::print_numbers(){ + message << "The trace has " << Error::_errors.size() << " errors and " << Error::_warnings.size() << " warnings."; if(Error::_warnings.size() == 0 && Error::_errors.size() == 0){ message << endi; } diff --git a/src/parser/Errors.hpp b/src/parser/Errors.hpp index 80d664b8..05f9a8d7 100644 --- a/src/parser/Errors.hpp +++ b/src/parser/Errors.hpp @@ -20,11 +20,19 @@ private: static std::queue<std::string> _errors; static std::queue<std::string> _warnings; - static unsigned int _number_of_warnings; - static unsigned int _number_of_errors; static std::string _content; public: + + /* + * Priorities of errors + */ + const static int _ERROR = 1; + const static int _WARNING = 2; + + /* + * Kind of errors known + */ const static std::string _EXPECT_END_DEF; const static std::string _EXPECT_EVENT_DEF; const static std::string _EXPECT_NAME_DEF; @@ -37,25 +45,25 @@ public: const static std::string _INCOMPATIBLE_VALUE_IN_EVENT; const static std::string _UNKNOWN_TYPE_IN_EVENT; const static std::string _UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND; - static void set_error(std::string kind_of_error, unsigned int line_number); - static void set_warning(std::string kind_of_error, unsigned int line_number); + const static std::string _UNKNOWN_LINE; + const static std::string _BAD_FILE_EXTENSION; + const static std::string _LINE_TOO_LONG_EVENT; + - static void set_and_print_error(std::string kind_of_error, unsigned int line_number); - static void set_and_print_warning(std::string kind_of_error, unsigned int line_number); + static void set(const std::string kind_of_error, const int priority); + static void set_and_print(const std::string kind_of_error, const int priority); + static void set_and_print(const std::string kind_of_error, const unsigned int line_number, const int priority); - static void print_error(); - static void print_warning(); + static void print(const int priority); - static bool set_error_if(bool condition, std::string kind_of_error, unsigned int line_number); - static bool set_warning_if(bool condition, std::string kind_of_error, unsigned int line_number); + static bool set_if(bool condition, const std::string kind_of_error, const unsigned int line_number, const int priority); - static bool set_and_print_error_if(bool condition, std::string kind_of_error, unsigned int line_number); - static bool set_and_print_warning_if(bool condition, std::string kind_of_error, unsigned int line_number); + static bool set_and_print_if(bool condition, const std::string kind_of_error, const unsigned int line_number, const int priority); - static void print_numbers_of_warning_and_errors(); + static void print_numbers(); static void flush_in_file(const std::string &filename); }; diff --git a/src/parser/ParserDefinitionDecoder.cpp b/src/parser/ParserDefinitionDecoder.cpp index 1902175d..3c7c8619 100644 --- a/src/parser/ParserDefinitionDecoder.cpp +++ b/src/parser/ParserDefinitionDecoder.cpp @@ -12,23 +12,24 @@ int ParserDefinitionDecoder::definitions_number(){ void ParserDefinitionDecoder::enter_definition(Line &line){ - Error::set_and_print_error_if(_state == _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count()); + Error::set_and_print_if(_state == _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count(), Error::_ERROR); string definition_name; - Error::set_and_print_error_if(!line.item(2, definition_name), Error::_EXPECT_NAME_DEF, line.get_line_count()); + Error::set_and_print_if(!line.item(2, definition_name), Error::_EXPECT_NAME_DEF, line.get_line_count(), Error::_ERROR); //DIE_IF(!line.item(2, definition_name), "Error : a definition need a name"); unsigned int definition_identity; string definition_identity_string; - if(Error::set_and_print_error_if(!line.item(3, definition_identity_string), Error::_EXPECT_ID_DEF, line.get_line_count())){ + if(Error::set_and_print_if(!line.item(3, definition_identity_string), + Error::_EXPECT_ID_DEF, line.get_line_count(), Error::_ERROR)){ return; } //DIE_IF(!line.item(3, definition_identity_string), "Error : definition has no identifier"); - if(Error::set_and_print_error_if(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1, - Error::_EXPECT_ID_DEF, line.get_line_count())){ + if(Error::set_and_print_if(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1, + Error::_EXPECT_ID_DEF, line.get_line_count(), Error::_ERROR)){ return; } //DIE_IF(sscanf(definition_identity_string.c_str(), "%d", &definition_identity) != 1, "Error : expected identifier for a definition"); @@ -37,7 +38,7 @@ void ParserDefinitionDecoder::enter_definition(Line &line){ _definitions.insert(pair<unsigned int,Definition>(definition_identity, current_definition)); _current_definition = definition_identity; - Error::set_and_print_warning_if(line.length() > 4, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count()); + Error::set_and_print_if(line.length() > 4, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count(), Error::_WARNING); //WARN_IF((line.length() > 4), Error::_EXPECT_EVENT_DEF + line_number); //"Warning : extra token in %EventDef on line << line_number << endl"); @@ -48,38 +49,41 @@ void ParserDefinitionDecoder::enter_definition(Line &line){ void ParserDefinitionDecoder::leave_definition(Line &line){ - if(Error::set_and_print_error_if(_state != _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count())){ + if(Error::set_and_print_if(_state != _IN_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count(), Error::_ERROR)){ return; } // DIE_IF(_state != _IN_A_DEFINITION, "Error : expected %EventDef"); _state = _OUT_A_DEFINITION; - if(Error::set_and_print_error_if(!_definitions[_current_definition].check_definition(), Error::_UNKNOWN_EVENT + _definitions[_current_definition].get_event_name(), line.get_line_count())){ + if(Error::set_and_print_if(!_definitions[_current_definition].check_definition(), + Error::_UNKNOWN_EVENT + _definitions[_current_definition].get_event_name(), + line.get_line_count(), Error::_ERROR)){ return; } //DIE_IF(!_definitions[_current_definition].check_definition(), "Error : unknown event : " + _definitions[_current_definition].get_event_name()); - Error::set_and_print_warning_if(line.length() > 2, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count()); + Error::set_and_print_if(line.length() > 2, Error::_EXTRA_TOKEN_IN_DEF, line.get_line_count(), Error::_WARNING); //WARN_IF(line.length() > 2, "Warning : extra token in %EventDef"); } void ParserDefinitionDecoder::add_field_to_definition(std::string& first_token,Line& line){ - if(Error::set_and_print_error_if(_state == _OUT_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count())){ + if(Error::set_and_print_if(_state == _OUT_A_DEFINITION, Error::_EXPECT_EVENT_DEF, line.get_line_count(), Error::_ERROR)){ return; } //DIE_IF(_state == _OUT_A_DEFINITION, "Error : expected %EventDef"); string field_type; - if(Error::set_and_print_error_if(!line.item(2, field_type), Error::_FIELD_TYPE_MISSING, line.get_line_count())){ + if(Error::set_and_print_if(!line.item(2, field_type), Error::_FIELD_TYPE_MISSING, line.get_line_count(), Error::_ERROR)){ return; } // DIE_IF(!line.item(2, field_type), "Error : a field type is missing"); - if(Error::set_and_print_error_if(field_type != "string" && field_type != "int" && + if(Error::set_and_print_if(field_type != "string" && field_type != "int" && field_type != "hex" && field_type != "date" && field_type != "double" && field_type != "color", - Error::_FIELD_TYPE_UNKNOWN, line.get_line_count())){ + Error::_FIELD_TYPE_UNKNOWN, line.get_line_count(), + Error::_ERROR)){ return; } // DIE_IF(field_type != "string" && field_type != "int" && @@ -93,7 +97,7 @@ void ParserDefinitionDecoder::add_field_to_definition(std::string& first_token,L void ParserDefinitionDecoder::store_definition(Line &line){ string first_token; - if(Error::set_and_print_warning_if(!line.item(1, first_token), Error::_EMPTY_DEF, line.get_line_count())){ + if(Error::set_and_print_if(!line.item(1, first_token), Error::_EMPTY_DEF, line.get_line_count(), Error::_WARNING)){ return; } //DIE_IF(!line.item(1, first_token), "Warning : a definition line is empty"); diff --git a/src/parser/ParserEventDecoder.cpp b/src/parser/ParserEventDecoder.cpp index 00965aac..f5035e35 100644 --- a/src/parser/ParserEventDecoder.cpp +++ b/src/parser/ParserEventDecoder.cpp @@ -35,8 +35,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T string current_value; if (!line.item(i, current_value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //message << "warning : missing values" << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } @@ -68,8 +67,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if (fields[i-1]._name == "Time") { if(!Date::instantiate(current_value, time)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } } @@ -85,8 +83,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if (fields[i-1]._name == "Value") { if(fields[i-1]._type == "double") { if(!Double::instantiate(current_value, value_double)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - // cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } } @@ -114,8 +111,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if(fields[i-1]._type == "double") { Double value; if(!Double::instantiate(current_value, value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } extra_fields[fields[i-1]._name] = new Double(value); @@ -123,8 +119,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if(fields[i-1]._type == "hex") { Hex value; if(!Hex::instantiate(current_value, value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } extra_fields[fields[i-1]._name] = new Hex(value); @@ -132,8 +127,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if(fields[i-1]._type == "date") { Date value; if(!Date::instantiate(current_value, value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } extra_fields[fields[i-1]._name] = new Date(value); @@ -141,8 +135,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if(fields[i-1]._type == "int") { Integer value; if(!Integer::instantiate(current_value, value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } extra_fields[fields[i-1]._name] = new Integer(value); @@ -150,15 +143,13 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T else if(fields[i-1]._type == "color") { Color value; if(!Color::instantiate(current_value, value)) { - Error::set_and_print_warning(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : incompatible value : " << current_value << endl; + Error::set_and_print(Error::_INCOMPATIBLE_VALUE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } extra_fields[fields[i-1]._name] = new Color(value); } else { - Error::set_and_print_warning(Error::_UNKNOWN_TYPE_IN_EVENT + current_value, line.get_line_count()); - //cerr << "warning : unknown type" << endl; + Error::set_and_print(Error::_UNKNOWN_TYPE_IN_EVENT + current_value, line.get_line_count(), Error::_WARNING); return; } } @@ -167,7 +158,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T } if(line.length() > i+1) { - cerr << "warning : extra value(s)" << endl; + Error::set_and_print(Error::_LINE_TOO_LONG_EVENT, line.get_line_count(), Error::_WARNING); } Name alias_name; @@ -251,8 +242,7 @@ void ParserEventDecoder::store_event(const Definition &definition, Line &line, T } else { - Error::set_and_print_warning(Error::_UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND + event_name, line.get_line_count()); - //cerr << "BUG PARSER BIS" << endl; + Error::set_and_print(Error::_UNKNOWN_EVENT_IN_PARSER_DEF_NOT_FOUND + event_name, line.get_line_count(), Error::_WARNING); return; } } diff --git a/src/parser/ParserPaje.cpp b/src/parser/ParserPaje.cpp index 6e06b88c..9da23c31 100644 --- a/src/parser/ParserPaje.cpp +++ b/src/parser/ParserPaje.cpp @@ -4,6 +4,14 @@ using namespace std; void ParserPaje::parse(string filename, Trace &trace){ + // If the file must have the extension .trace + // + const unsigned int position_of_dot = filename.find_last_of('.'); + if(position_of_dot == string::npos || filename.substr(position_of_dot) != ".trace"){ + Error::set_and_print(Error::_BAD_FILE_EXTENSION, Error::_WARNING); + //throw Error::_BAD_FILE_EXTENSION; + } + ParserDefinitionDecoder *parserdefinition = new ParserDefinitionDecoder(); ParserEventDecoder *parserevent = new ParserEventDecoder(); @@ -14,31 +22,32 @@ void ParserPaje::parse(string filename, Trace &trace){ unsigned int event_identity; while(!line.is_eof()){ - + line.newline(); if(line.starts_with(PERCENT)){ - parserdefinition->store_definition(line); + parserdefinition->store_definition(line); } else if (!line.item(0, event_identity_string)){ continue; // We have \n } else{ - if(Error::set_and_print_error_if (sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1, - Error::_EXPECT_ID_DEF, line.get_line_count())){ - return; - } - // DIE_IF(sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1, "expected identifier for a definition"); - parserevent->store_event( - parserdefinition->get_definition(event_identity), + // We check if we have an event identifier + if(Error::set_and_print_if (sscanf(event_identity_string.c_str(), "%d", &event_identity) != 1, + Error::_EXPECT_ID_DEF, line.get_line_count(), Error::_ERROR)){ + // The line can not be recognised by the parser Paje, we can not continue. + throw Error::_UNKNOWN_LINE; + //return ; + } + parserevent->store_event(parserdefinition->get_definition(event_identity), line, trace); } } - // We print the warnings - Error::print_numbers_of_warning_and_errors(); + // We print the warnings and errors + Error::print_numbers(); Error::flush_in_file("log.txt"); delete parserdefinition; diff --git a/src/trace/values/Color.cpp b/src/trace/values/Color.cpp index b6efcb5e..c1bebbd7 100644 --- a/src/trace/values/Color.cpp +++ b/src/trace/values/Color.cpp @@ -1,65 +1,65 @@ -#include "Color.hpp" - -Color::Color() { - _r = 0; - _g = 0; - _b = 0; -} - - -Color::Color(double r, double g, double b) { - _r = r; - _g = g; - _b = b; -} - -bool Color::instantiate(std::string &in, Color &out) { - double r, g, b; - - 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 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; - } -} - -std::string Color::to_string() const { - std::ostringstream oss; - 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; +#include "Color.hpp" + +Color::Color() { + _r = 0; + _g = 0; + _b = 0; +} + + +Color::Color(double r, double g, double b) { + _r = r; + _g = g; + _b = b; +} + +bool Color::instantiate(std::string &in, Color &out) { + double r, g, b; + + 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 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; + } +} + +std::string Color::to_string() const { + std::ostringstream oss; + oss << std::setprecision(Value::_PRECISION) << _r << " " << _g << " " << _b; + return oss.str(); +} + + +bool Color::replace_in_string(std::string &characters, char to_replace, char replace_by) { + bool has_change = false; + const int lenght = characters.size(); + for(int i = 0 ; i < lenght ; i ++){ + if(characters[i] == to_replace){ + characters[i] = replace_by; + has_change = true; + } + } + + return has_change; } double Color::get_red() const { @@ -72,4 +72,4 @@ double Color::get_green() const { double Color::get_blue() const { return _b; -} +} diff --git a/src/trace/values/Date.cpp b/src/trace/values/Date.cpp index ae715194..e8ad6f8f 100644 --- a/src/trace/values/Date.cpp +++ b/src/trace/values/Date.cpp @@ -1,6 +1,5 @@ #include "Date.hpp" - Date::Date(){ _value = 0.0; } @@ -8,19 +7,22 @@ Date::Date(){ Date::Date(double value){ _value = value; } + -bool Date::instantiate(const std::string &in, Date &out){ +bool Date::instantiate(std::string &in, Date &out){ double value = 0.0; + if(sscanf(in.c_str(), "%lf", &value) != 1){ return false; - }; + } + out = Date(value); return true; } std::string Date::to_string() const{ std::ostringstream oss; - oss << _value; + oss << std::setprecision(Value::_PRECISION) << _value; return oss.str(); } diff --git a/src/trace/values/Date.hpp b/src/trace/values/Date.hpp index c950055b..4793b7f9 100644 --- a/src/trace/values/Date.hpp +++ b/src/trace/values/Date.hpp @@ -44,7 +44,7 @@ public : * \return true, if the conversion succeeded * */ - static bool instantiate(const std::string &in, Date &out); + static bool instantiate(std::string &in, Date &out); /*! * diff --git a/src/trace/values/Double.cpp b/src/trace/values/Double.cpp index b5e11626..9d337a5b 100644 --- a/src/trace/values/Double.cpp +++ b/src/trace/values/Double.cpp @@ -20,7 +20,7 @@ bool Double::instantiate(const std::string &in, Double &out){ std::string Double::to_string() const{ std::ostringstream oss; - oss << _value; + oss << std::setprecision(Value::_PRECISION) << _value; return oss.str(); } diff --git a/src/trace/values/Hex.cpp b/src/trace/values/Hex.cpp index b69b0e52..d7c6996b 100644 --- a/src/trace/values/Hex.cpp +++ b/src/trace/values/Hex.cpp @@ -19,6 +19,6 @@ bool Hex::instantiate(const std::string &in, Hex &out) { std::string Hex::to_string() const { std::ostringstream oss; oss.flags(std::ios::hex); - oss << _value; + oss << std::setprecision(Value::_PRECISION) << _value; return oss.str(); } diff --git a/src/trace/values/Integer.cpp b/src/trace/values/Integer.cpp index eb5ca3df..966b4c48 100644 --- a/src/trace/values/Integer.cpp +++ b/src/trace/values/Integer.cpp @@ -18,6 +18,6 @@ bool Integer::instantiate(const std::string &in, Integer &out) { std::string Integer::to_string() const { std::ostringstream oss; - oss << _value; + oss << std::setprecision(Value::_PRECISION) << _value; return oss.str(); } diff --git a/src/trace/values/Value.hpp b/src/trace/values/Value.hpp index 69edd8c2..f5697f7a 100644 --- a/src/trace/values/Value.hpp +++ b/src/trace/values/Value.hpp @@ -12,6 +12,10 @@ #include <string> + +// For std::setprecision +#include <iomanip> + /*! * * \class Value @@ -28,6 +32,8 @@ public: * \return the value in a string format. * */ + + static const int _PRECISION = 15; virtual std::string to_string() const = 0; virtual ~Value(){}; }; -- GitLab