This commit is contained in:
Camille Chauvet 2023-12-20 13:41:56 +00:00
parent ca8912c56e
commit 9699c0c218
2 changed files with 10 additions and 11 deletions

View File

@ -49,7 +49,7 @@ static int check_date(const std::string& line, unsigned int& out)
mounth = std::atoi(line.substr(5).c_str()); mounth = std::atoi(line.substr(5).c_str());
day = std::atoi(line.substr(8).c_str()); day = std::atoi(line.substr(8).c_str());
if (date_is_valid(day, mounth, year)) if (!date_is_valid(day, mounth, year))
return 1; return 1;
out = year * 10000 + mounth * 100 + day; out = year * 10000 + mounth * 100 + day;
return 0; return 0;
@ -79,7 +79,7 @@ static int check_value(const std::string& value_str, int flag, float& out)
static int check_line(const std::string& line, regex_t& reegex, const std::string& delimiter, int flag, std::pair<unsigned int, float>& out) static int check_line(const std::string& line, regex_t& reegex, const std::string& delimiter, int flag, std::pair<unsigned int, float>& out)
{ {
if (line == std::string("date") + delimiter + std::string(flag == DATABASE ? "exchange_rate" : "value")) if (line == std::string("date") + delimiter + (flag == DATABASE ? "exchange_rate" : "value"))
return 1; return 1;
if (regexec(&reegex, line.c_str(), 0, NULL, 0) == REG_NOMATCH) if (regexec(&reegex, line.c_str(), 0, NULL, 0) == REG_NOMATCH)
{ {
@ -88,8 +88,10 @@ static int check_line(const std::string& line, regex_t& reegex, const std::strin
} }
unsigned int date; unsigned int date;
if (check_date(line, date)) if (check_date(line, date))
{
std::cout << "Error: bad input => " << line << std::endl;
return 1; return 1;
}
float value; float value;
if (check_value(line.substr(10 + delimiter.length()), flag, value)) if (check_value(line.substr(10 + delimiter.length()), flag, value))
return 1; return 1;
@ -106,7 +108,7 @@ static int get_data(std::map<unsigned int, float>& out)
if (!file.good()) if (!file.good())
{ {
std::cout << "error: file: " << "data.csv" << std::endl; std::cout << "Error: could not open file." << std::endl;
return 1; return 1;
} }
@ -114,7 +116,7 @@ static int get_data(std::map<unsigned int, float>& out)
if (regcomp(&reegex, DATA_PATERN, REG_EXTENDED)) if (regcomp(&reegex, DATA_PATERN, REG_EXTENDED))
{ {
std::cout << "regex compilation fail" << std::endl; std::cout << "Error: regex compilation fail." << std::endl;
return 1; return 1;
} }
@ -185,11 +187,8 @@ static int get_input(const std::string& file_path, const std::map<unsigned int,
void get_price(const std::string& file_path) void get_price(const std::string& file_path)
{ {
static bool db_init = 0; std::map<unsigned int, float> database;
static std::map<unsigned int, float> database;
if (db_init == 0)
get_data(database); get_data(database);
get_input(file_path, database); get_input(file_path, database);
} }

View File

@ -5,7 +5,7 @@ int main(int ac, char** av)
{ {
if (ac != 2) if (ac != 2)
{ {
std::cout << "error: missing file" << std::endl; std::cout << "error: could not open file." << std::endl;
return 1; return 1;
} }
get_price(std::string(av[1])); get_price(std::string(av[1]));