180 lines
5.1 KiB
C++
180 lines
5.1 KiB
C++
/* ************************************************************************** */
|
||
/* */
|
||
/* ::: :::::::: */
|
||
/* BitcoinExchange.cpp :+: :+: :+: */
|
||
/* +:+ +:+ +:+ */
|
||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||
/* +#+#+#+#+#+ +#+ */
|
||
/* Created: 2025/05/21 10:39:45 by adjoly #+# #+# */
|
||
/* Updated: 2025/05/30 13:29:18 by adjoly ### ########.fr */
|
||
/* */
|
||
/* ************************************************************************** */
|
||
|
||
#include <BitcoinExchange.hpp>
|
||
#include <cstddef>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <ostream>
|
||
#include <sstream>
|
||
#include <stdexcept>
|
||
#include <string>
|
||
#include <utility>
|
||
|
||
const uint32_t Date::_d_in_m[12] = {31, 29, 31, 30, 31, 30,
|
||
31, 31, 30, 31, 30, 31};
|
||
|
||
void _log(std::string emoji, std::string what, std::string who,
|
||
std::string str) {
|
||
#ifdef VERBOSE
|
||
if (who.empty())
|
||
std::cout << "「" << emoji << "」" << what << ": " << str << std::endl;
|
||
else
|
||
std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str
|
||
<< std::endl;
|
||
#else
|
||
(void)emoji, (void)what, (void)who, (void)str;
|
||
#endif
|
||
}
|
||
|
||
bool Date::_isMonthValid(uint16_t month) const {
|
||
if (month >= 1 && month <= 12)
|
||
return true;
|
||
return false;
|
||
}
|
||
|
||
bool Date::_isDayValid(uint16_t day, uint16_t month) const {
|
||
if (day >= 1 && day <= _d_in_m[month - 1])
|
||
return true;
|
||
return false;
|
||
}
|
||
|
||
Date::Date(void) : _year(0), _month(1), _day(1) {
|
||
_log("➕", "Date", "", "default constructor called");
|
||
}
|
||
|
||
Date::Date(std::string date) {
|
||
_log("➕", "Date", "", "string constructor called");
|
||
std::istringstream iss(date);
|
||
char del1, del2;
|
||
uint32_t year;
|
||
uint16_t month, day;
|
||
|
||
if (iss >> year >> del1 >> month >> del2 >> day) {
|
||
if (del1 == '-' && del2 == '-' && iss.eof()) {
|
||
if (_isMonthValid(month) && _isDayValid(day, month)) {
|
||
_year = year;
|
||
_month = month;
|
||
_day = day;
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
throw std::runtime_error("Error: bad input => " + date);
|
||
}
|
||
|
||
Date::Date(const Date &cpy)
|
||
: _year(cpy._year), _month(cpy._month), _day(cpy._day) {
|
||
_log("➕", "Date", "", "copy constructor called");
|
||
}
|
||
|
||
Date::~Date(void) { _log("➖", "Date", "", "destructor called"); }
|
||
|
||
Date &Date::operator=(const Date &cpy) {
|
||
_log("➕", "Date", "", "copy assignement constructor called");
|
||
if (&cpy != this) {
|
||
_year = cpy._year;
|
||
_month = cpy._month;
|
||
_day = cpy._day;
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
uint32_t Date::getYear(void) const { return _year; }
|
||
uint16_t Date::getMonth(void) const { return _month; }
|
||
uint16_t Date::getDay(void) const { return _day; }
|
||
|
||
bool Date::operator==(const Date &other) const {
|
||
return other._year == _year && other._month == _month && other._day == _day;
|
||
}
|
||
|
||
bool Date::operator!=(const Date &other) const {
|
||
return other._year != _year || other._month != _month || other._day != _day;
|
||
}
|
||
|
||
bool Date::operator<(const Date &other) const {
|
||
if (_year != other._year)
|
||
return _year < other._year;
|
||
if (_month != other._month)
|
||
return _month < other._month;
|
||
return _day < other._day;
|
||
}
|
||
|
||
bool Date::operator>(const Date &other) const {
|
||
if (_year != other._year)
|
||
return _year > other._year;
|
||
if (_month != other._month)
|
||
return _month > other._month;
|
||
return _day > other._day;
|
||
}
|
||
|
||
bool Date::operator<=(const Date &other) const {
|
||
return (*this) < other || (*this) == other;
|
||
}
|
||
|
||
bool Date::operator>=(const Date &other) const {
|
||
return (*this) > other || (*this) == other;
|
||
}
|
||
|
||
std::ostream &operator<<(std::ostream &os, const Date &date) {
|
||
os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
|
||
return os;
|
||
}
|
||
|
||
CsvParser::CsvParser(void) : _is("data.csv") {
|
||
_log("➕", "CsvParser", "", "default constructor called");
|
||
|
||
if (_is.is_open() != true)
|
||
throw std::runtime_error("could not open database file");
|
||
std::string buf;
|
||
|
||
std::getline(_is, buf);
|
||
if (buf != "date,exchange_rate")
|
||
throw std::runtime_error("first line not good");
|
||
|
||
std::getline(_is, buf);
|
||
while (buf != "") {
|
||
size_t i = buf.find(",");
|
||
if (i == std::string::npos)
|
||
throw std::runtime_error("did not found ,");
|
||
Date date(buf.substr(0, i));
|
||
float nb;
|
||
if (buf.substr(i + 1, buf.length()) == "0")
|
||
nb = 0;
|
||
else {
|
||
nb = std::atof(buf.substr(i + 1, buf.length()).c_str());
|
||
if (nb == 0)
|
||
throw std::runtime_error("number not well formatted - " +
|
||
buf.substr(i + 1, buf.length()));
|
||
}
|
||
_csv.insert(std::make_pair(date, nb));
|
||
std::getline(_is, buf);
|
||
}
|
||
}
|
||
|
||
CsvParser::CsvParser(const CsvParser &cpy) {
|
||
_log("➕", "CsvParser", "", "copy constructor called");
|
||
if (&cpy != this) {
|
||
}
|
||
}
|
||
|
||
CsvParser::~CsvParser(void) { _log("➖", "CsvParser", "", "destructor called"); }
|
||
|
||
CsvParser &CsvParser::operator=(const CsvParser &cpy) {
|
||
_log("➕", "CsvParser", "", "copy assignement constructor called");
|
||
if (&cpy != this) {
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
std::map<Date, float> CsvParser::getCsv(void) const { return _csv; }
|