/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* BitcoinExchange.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bchanot +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/16 19:29:59 by bchanot #+# #+# */ /* Updated: 2026/01/16 19:35:25 by bchanot ### ########.fr */ /* */ /* ************************************************************************** */ #include "BitcoinExchange.hpp" #include #include void ft_getcsv(std::map &Data) { std::string line; std::ifstream ifs; std::string token; float price; Date tmp; ifs.open("data.csv"); if (!ifs) { std::cout << "Error: could not open data.csv file." << std::endl; throw std::exception(); } std::getline(ifs, line); while (std::getline(ifs, line)) { token = line.substr(0, line.find(',')); tmp.year = atoi(token.substr(0, 4).c_str()); tmp.month = atoi(token.substr(5, 2).c_str()); tmp.day = atoi(token.substr(8, 2).c_str()); price = atof(line.substr(line.find(',') + 1, line.find('\n')).c_str()); if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) { std::cout << "Error : Error in csv dates." << std::endl; throw std::exception(); } if (price < 0 || price > INT_MAX) { std::cout << "Error : Error in csv price." << std::endl; throw std::exception(); } Data[tmp] = price; } } void ft_process(char **av, std::map &Data) { std::ifstream ifs; std::string line; std::string token; std::map::iterator it; float amount; Date tmp; ifs.open(av[1]); if (!ifs) { std::cout << "Error: could not open " << av[1] << "." << std::endl; throw std::exception(); } std::getline(ifs, line); while (std::getline(ifs, line)) { token = line.substr(0, line.find('|')); tmp.year = atoi(token.substr(0, 4).c_str()); tmp.month = atoi(token.substr(5, 2).c_str()); tmp.day = atoi(token.substr(8, 2).c_str()); amount = atof(line.substr(line.find('|') + 1, line.find('\n')).c_str()); if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) { std::cout << "Error : Bad date." << std::endl; continue ; } if (amount < 0 || amount > INT_MAX) { std::cout << "Error : Bad price." << std::endl; continue ; } it = Data.lower_bound(tmp); if (it->first != tmp) { if (it == Data.begin()) std::cout << "Cannot find a price at the value " << tmp << "." << std::endl; else std::cout << tmp << " => " << amount << " = " << (--it)->second * amount << std::endl; } else std::cout << it->first << " => " << amount << " = " << it->second * amount << std::endl; } }