1
0

🏗️」 wip: started ex00

This commit is contained in:
2025-06-01 21:25:47 +02:00
parent effc30e6a1
commit 003d8663d0
9 changed files with 2000 additions and 4 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ obj/
compile_commands.json
.cache
*.d
btc

179
ex00/BitcoinExchange.cpp Normal file
View File

@ -0,0 +1,179 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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; }

101
ex00/BitcoinExchange.hpp Normal file
View File

@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 10:38:49 by adjoly #+# #+# */
/* Updated: 2025/05/30 20:31:26 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#define range(x) \
x.begin(); \
it != x.end(); \
it++
#define auto __auto_type
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
struct token {};
void _log(std::string emoji, std::string what, std::string who,
std::string str);
class Date {
public:
Date(void);
Date(std::string);
Date(const Date &);
~Date(void);
Date &operator=(const Date &);
uint32_t getYear(void) const;
uint16_t getMonth(void) const;
uint16_t getDay(void) const;
bool operator==(const Date &) const;
bool operator!=(const Date &) const;
bool operator<(const Date &) const;
bool operator>(const Date &) const;
bool operator<=(const Date &) const;
bool operator>=(const Date &) const;
protected:
private:
static const uint32_t _d_in_m[12];
bool _isMonthValid(uint16_t) const;
bool _isDayValid(uint16_t, uint16_t) const;
uint32_t _year;
uint16_t _month;
uint16_t _day;
};
std::ostream &operator<<(std::ostream &, const Date &);
class CsvParser {
public:
CsvParser(void);
CsvParser(const CsvParser &);
~CsvParser(void);
CsvParser &operator=(const CsvParser &);
std::map<Date, float> getCsv(void) const;
protected:
private:
std::ifstream _is;
std::string _filename;
std::map<Date, float> _csv;
};
class BitcoinExchange {
public:
BitcoinExchange(void);
BitcoinExchange(const BitcoinExchange &);
~BitcoinExchange(void);
BitcoinExchange &operator=(const BitcoinExchange &);
protected:
private:
void _nextToken(void);
std::string _peekToken(void) const;
};

View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/05/30 13:24:23 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = btc
CC = clang++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

BIN
ex00/btc Executable file

Binary file not shown.

1613
ex00/data.csv Normal file

File diff suppressed because it is too large Load Diff

10
ex00/input.txt Normal file
View File

@ -0,0 +1,10 @@
date | value
2011-01-03 | 3
2011-01-03 | 2
2011-01-03 | 1
2011-01-03 | 1.2
2011-01-09 | 1
2012-01-11 | -1
2001-42-42
2012-01-11 | 1
2012-01-11 | 2147483648

29
ex00/main.cpp Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 10:34:50 by adjoly #+# #+# */
/* Updated: 2025/05/30 20:30:28 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <BitcoinExchange.hpp>
#include <cstdlib>
#include <exception>
int main(int ac, char **av) {
(void)av;
if (ac > 1) {
CsvParser *parser;
try {
parser = new CsvParser();
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
} else {
std::cout << "Error: could not open file." << std::endl;
}
}

View File

@ -15,15 +15,17 @@
});
in {
devShells = forEachSupportedSystem ({ pkgs }: {
default = pkgs.mkShell.override
{}
default = pkgs.mkShell
{
nativeBuildInputs = with pkgs; [
llvmPackages_12.clang-tools
gcc11
clang_12
];
hardeningDisable = [ "all" ];
packages = with pkgs; [
git
gdb
gcc11
clang_12
valgrind
compiledb
norminette